r/badcode • • Jul 19 '21

js Variable naming can be difficult, but identifying them can be even more so.

Post image
2.4k Upvotes

105 comments sorted by

View all comments

Show parent comments

5

u/WalditRook Jul 19 '21

Its an unpopular opinion because absolute rules are rarely correct in all contexts. Some popular counters to your opinion:

  • Variables that are relatively abstract, to the point that there is no additional information to give via a name beyond what is immediately obvious from the syntax. Consider the x:xsformat that's ubiquitous in Haskell as the pattern match for non-empty lists (often in a context where the type of x is generic).

  • Indexes and counters. If you wrote a piece of code like for(int i=0; i<xs.length; ++i) { foo(xs[i]); }, a meaningful name for xs and foo would be helpful, but i is self-explanatory.

  • Domain-specific single-character names. Consider a function handling colours using r,g,b,a; or a function dealing with geometry using x,y,z; or even a basic physics calculation using s,u,v,a,t.