Hacker Newsnew | comments | show | ask | jobs | submit login

It's off-topic, but could anyone critique a rule for (not) naming things I came up with recently:

If something (an expression or function) is used more than once, give it a name. If it's used once, name it iff you would otherwise comment to explain it. Otherwise, inline it.




Sure, that's basically a rule for when to do the "introduce explaining variable" refactoring [1]. There's a bit of taste as to when to name things in this instance, but certainly if a name can replace a comment then it's better to use a name. You might additionally use a name for something you wouldn't comment, if the logic is straightforward but the code is long enough that it would otherwise be hard to read.

If you use something more than once, then you either have to give it a name or repeat it, and generally people like to avoid repeating things. Some people will accept two copies and get rid of the duplication when they get to three copies, though purists would dislike that.

[1] http://www.refactoring.com/catalog/introduceExplainingVariab...

-----




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: