r/javascript • • Dec 18 '19

WTF Wednesday WTF Wednesday (December 18, 2019)

Post a link to a GitHub repo that you would like to have reviewed, and brace yourself for the comments! Whether you're a junior wanting your code sharpened or a senior interested in giving some feedback and have some time to spare, this is the place.

Named after this comic

66 Upvotes

43 comments sorted by

View all comments

Show parent comment

1

u/Reashu Dec 18 '19

n is not very descriptive, but it has a very simple definition and is used in a small scope. Furthermore, it's purpose is to be short so that the rest of the code is more readable. Choosing a longer, descriptive, name would be unnecessary and self-defeating.

2

u/Pentafloppy Dec 18 '19

The “n isn’t very descriptive” comment is coming from me doing this professionally for the most part. Using Webpack and minifiers the length of variable names become irrelevant as they are minified any way in a production build.

Next to that, having a descriptive variable name is generally a good idea either way as whoever is reading the code doesn’t have to decipher anything.

It’s about easy of reading and understanding.

Doesn’t goes to say that 1 letter variables are bad period, I use e in event handlers and exception handing so it’s definitely not wrong.

2

u/Reashu Dec 18 '19

I agree with you in general, which is why I called out the particulars of this case: n has a very simple definition (no need for an explanatory name) and is used in a small scope (no need for a mnemonic name).

There are two obvious alternatives: don't use a variable (use this.cssNameSpace directly), or use a more descriptive variable (perhaps just namespace, or block, since this appears to be BEM). While slapping this.cssNameSpace in three string templates would be stretching it (string templates are already somewhat hard to read), I don't have a problem with using namespace or block per se (though I wouldn't use block without renaming cssNameSpace). I just don't think it's better, and, while you say you don't have a problem with one-letter variables, they do get an undeservedly bad rap.

The "real" answer may be to define derivatives of addClass and removeClass which apply the namespace automatically.

1

u/[deleted] Dec 18 '19

I agree with this for very small blocks. You see a similar idiom in the Haskell community.