Lurker News

Comments (156) 28 threads

Show all comments

Showing thread 25 of 28

leetrout10 replies#
Code like this is interesting to me whenever I see it:

const g = [['', '', ''], ['', '', ''], ['', '', '']] // grid
Why not just be descriptive and call it `const grid`? It's like we're all playing some game to be as terse and obscure as possible with variable names in all languages.

jonplackett#
So many code examples have things like this and worse.

One I saw the other day had about 10 variables created in one part and then these were all placed into another object that had the same names as all the variables.
You also see a lot of examples with loads of other irrelevant code in them.
Just keep it simple please!

rpunkfu1 reply#
cries in golang
leetrout1 reply#
For small functions / blocks and descriptive types I’m ok with it in Go BUT I always use descriptive names anyway. So much easier to keep context in my head.
libria3 replies#
I thought the whole single-char-var thing was for very short-lived variables.

I very much prefer

    for c in internalProductCategories
       p = fetchProductsInCategory(c.id)
       m = getAccountManagerName(c.id)
       results.append({categoryName: c.name, products: p, manager: m.name})
to

    for internalProductCategory in internalProductCategories
       products = fetchProductsInCategory(internalProductCategory.id)
       manager = getAccountManagerName(internalProductCategory.id)
       results.append({categoryName: internalProductCategory.name, products: products, manager: manager.name})
They're both clear, neither needs comments, but one is unnecessarily verbose.
Kaze4044 replies#
The latter requires you to read the assignment to figure out what the variables mean.

Continue this thread — 4 replies

ysavir5 replies#
While that's definitely better than single letter variables everywhere, I still think an explicit variable name is better.

In general, I follow a rule of "If I can't search the file for this variable and find it easily, it's a bad name".

Continue this thread — 5 replies

ppeetteerr#
Strongly disagree there. If there was a crash at line 4, in the first example you would only see this:

> results.append({categoryName: c.name, products: p, manager: m.name})
In the second example you would see this:
> results.append({categoryName: internalProductCategory.name, products: products, manager: manager.name})
Do you know what the error would be? Probably that you're assigning the manager's name to a property called `manager` ;)

JustSomeNobody#
It wouldn't annoy me so much if they hadn't put the comment. I can clearly see it's a grid.
brudgers1 reply#
Aesthetically, it's consistent with the article.

Technically it's consistent with ordinary practice. e.g.

  import naughtsAndCrosses as nan
gtf211 reply#
I'm not sure this is "ordinary practice", especially as `nan` often refers to "not a number" as an error category.

Typing full words doesn't take that much longer than acronyms, and results in more readable code (although this is well enough established at this point that I don't think I need to go over it).

brudgers#
Bugs are ordinary practice. Importing naughts and crosses is not. I giggled mischeviously when I wrote it. Particularly since it looked Pythonic. YMMV.
pier251 reply#
Totally agree.

Code should be stupidly obvious so that reading it consumes as less cognitive resources as possible.

bonestamp2#
Yep, let the minifiers and obfuscators handle the minification and obfuscation.
leontrolski1 reply#
Using m or h as VDOM constructors are mithril/hyperscript-isms, I guess that's not obvious if you not worked with those or similar libraries, sorry!
jackewiehose#
yep, I figured that out by reading the actual article instead of straight jumping to the source and my edit was too slow. I'm sorry ;-)
leontrolski1 reply#
Author here - I did put a warning in guys :-)

  Lots of the code looks pretty code-golfy - I promise I don't do stuff like this at work, neither should you
Happy to s/g/grid/ - writing the whole exercise felt a bit golfy TBH.
galaxyLogic#
>> if you change the state, it runs the function again, this returns a new virtual DOM

What do you mean by "change the state"? How do you do that? By calling some function, or perhaps by assigning a value to some property of a state-object? OR by calling the function again with a different state-object?
Thanks

grawprog#
If that grid was going to be used in a single loop or something I may write it like that but any larger scope and it would definitely have a descriptive name.
caymanjim1 reply#
Comments in code are almost always an indication of poor naming choices. I don't get it either.
Supermancho#
He saw grid in his head and thought g was fine since he was doing a minimal poc. It's not something that's hard to understand.