I am considering this method: bool Movies::trytoaddmovie(const std::string &name, const…
- User
I am considering this method: bool Movies::try_to_add_movie(const std::string &name, const std::string &rating, const int watched) { bool is_present{false}; bool added{false};
codefor (const Movie movie : movies) { if (movie.get_name() == name) { is_present = true; break; } } if (!is_present) { Movie movie{name, rating, watched}; movies.push_back(movie); added = true; } return added;} I think is_present is not necessary, because it is needed only one boolean to take care of this method. However, I do not know if there is any best practice for this case, like to insert a return directly instead of putting the break. Please provide feedback.
- Assistant
You're correct in thinking that the
is_presentvariable is unnecessary and that there are better ways to structure the code for clarity and efficiency. Instead of using a flag, you can directly return from the function when you find an existing movie. Here’s a refactored version of your method that eliminates theis_presentvariable and simplifies the logic:cpp