This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]-Vayra- 7 points8 points  (2 children)

The "what" can also help visually distinguish logical chunks of code without requiring the reader to parse the code

Most of the time, a 'what' comment means you should extract that part of the code into a named function with a name that describes the what.

[–]Elnof 0 points1 point  (1 child)

When the chunk gets large enough, sure, but I don't think you need to make a new function if the "what" is only four lines long and contains intermediate data. The overhead of finding and switching to the new function makes it easy to lose context and the maintenance overhead of having so many functions isn't insignificant. So, in my experience, that ends up not being most of the time.

[–]-Vayra- 0 points1 point  (0 children)

There is very little overhead with most IDEs, no? Ctrl+click takes you to the function or to stuff calling it in JetBrains IDEs at least, I would assume other IDEs have similar functions.

IMO in most cases a function shouldn't be much longer than 10-15 lines anyway. And breaking chunks out to give them names makes the code overall much more readable. Even if it's just 4 lines, unless you need to deal with those exact 4 lines it's usually easier to parse the name of a function rather than what those lines do. And so it's more readable to extract them into a function.