Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

"1. Substitutes comments for accurate and specific function names. Why better? Because comments can get out of sync with the code."

Can you explain this a bit better? In my experience function names can just as easily get out of sync with the code. I've worked on many codebases that were full of small functions with misleading names.



Function names certainly can get out of sync, just as comments can. But my experience is that it's less of a problem.

Actually, there is an additional and probably much more important reason that I prefer "functions over comments", and that is the functions can be nested.

With comments, I often find myself needing a big comment that describes the next several chunks of things, and each of those chunks has some comments, too. So call those "level 1" and "level 2" comments.

People tend to distinguish these cases like:

  /* ***************************
     level-one comment
     *************************** */
and then

  /* level-two comment */
But what if you have something that really "should" be a level-two comment (because it's just one small thing that doesn't have subdivisions), but it comes after a level-one comment? Now you need an end delimeter for level-one comments, like this:

  /* *****************
    done with that
  ******************* */
Now at this point, your code is nasty and non-readable and it's not obvious that things have been kept in sync over time. We could just avoid all this ambiguity by using nested functions that are nonetheless only called from one place and thus _could have_ been manually inlined in theory (but, per my practice, are not).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: