I was just near the end of the lecture and you start to grab certain parts of the main function and break them into their own functions. I was wondering what the point of breaking code in your main function into different functions when you can just do comments to stay organized?
Because descriptive function/variables names are vastly superior to comments.
Comments can quickly become out of date, code was updated but not the comment.
Code can more often than not be clearer as to what is happening than a comment
“Here the code is clear enough that I don’t need comments, but the comments are so unclear that I’m glad the code is here to help explain them.” - Remy Porter
When you can’t explain what’s happening in code is when comments are beneficial, or when you’re purposely doing something a little weird e.g. for performance reasons or it’s deceptively more complex than it looks and could lead to a refactor to make it “obviously simpler” but doing so would actually be incorrect and you want to prevent that refactor from happening.
Video where I got that quote from which might be helpful for you:
How Not To Write Comments
Functions can also be reused/accessed from elsewhere.
Let’s say you wanted to use just the “introduction” part of main() in another segment of code or possibly even another .cpp file.
How would you do that? You would need to copy-paste code over again.
But you can instead just call the function in a single line, and it can (with the right settings) be accessed from other files too.
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.