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.
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!
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.
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.
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).
Bugs are ordinary practice. Importing naughts and crosses is not. I giggled mischeviously when I wrote it. Particularly since it looked Pythonic. YMMV.
I have no problem with abbreviations over a limited scope but
looking at this code[1] I can't figure out what the author meant
by 'm' and that makes me wonder if the author does know what he's
doing.
>> 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
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.
Not all applications have a requirement around "necessary optimizations". Sometimes if the application is simple enough, something like this submission could be good enough.
Or the constraints could be different where the bundle size vs performance implications could be worth trading. Depends on context :)
> The programmer sets up the desired appearance of each window, then tells the curses package to update the screen. The library determines a minimal set of changes that are needed to update the display and then executes these using the terminal's specific capabilities and control sequences. https://en.wikipedia.org/wiki/Curses_(programming_library)
The submitted title was "React in 33 lines". We've since changed it. This is a case where rewriting the title did the article a disservice. To my ear there is a subtle but significant difference between that and "33 line React", which is what the author actually said.
"React in 33 lines" sounds reductionist. With the actual title, there's an implicit "a" (i.e. an indefinite article) at the start, i.e. "A 33-line React". In other words: a short exercise exploring the core idea of React. Not a claim of total equivalence. Submitters: please read the site guidelines. They include "Please use the original title, unless it is misleading or linkbait; don't editorialize." Much of the time, when people rewrite a title, they make it more misleading or linkbait, even if they didn't mean to. https://news.ycombinator.com/newsguidelines.html
We all hate new syntax that we feel isn't "samey" enough to what we already know. That doesn't mean it's gross, it just means you learned something about your own preconceptions of what you thought JS was supposed to look and feel like.
Thankfully, most of the time we learn that's just us, and move past that. Arrow functions have been around for nearly five years now: they're fine. You learn that they're just part of JS, and you see them everywhere in modern code, just with normal indentation and sensible white spacing to keep them perfectly readable (because minification will solve the "the code as written is too verbose" part of the dev cycle). The important thing is what they do: bind declaration context as execution context, so `this` keeps meaning the same thing, which drastically simplifies a lot code. In this case, there is simply no reason to use arrow functions at all, because we don't need to preserve the declaration context (there is no `this` that needs to be preserved), so if this were real code you'd probably use a normal function, and your comment should probably be "why not this?"
function makeCell(value, i, j) {
function onclick() {
playMove(value, i, j);
}
return build('button.cell', { onclick }, value);
}
Or even, "actually, why not this?"
/**
* ...
*/
function clickCell(evt) {
const t = evt.target,
d = t.dataset;
playMove(t.textContent, d.i, d.j);
}
/**
* ...
*/
function makeCell(value, i, j) {
const e = build('button.cell', value);
e.setData({ i, j });
e.click(clickCell);
return e;
}
To which the answer of course is "this isn't production code, arrow functions are fine. I mean come on, it's just a fun little JS exercise, stop trying to turn fun into work".
I don't use javascript regularly so I have no notice of "samey". But I've been exposed to enough of a variety of programming languages from APL to C to Rust to Scala that I have a pretty good idea when some construct looks awkward or unergonomic. ES6 looks like a nightmare. Even compared to the nightmare that is Scala. And remember, Javascript's roots are scheme.
Here's the same thing in clojurescript. Tighter, balanced and no chaining of symbols.
(defn Cell [value i j]
(m 'button.cell' {:onclick #(move value, i, j)}))
I've never used clojurescript (but have used perl, php, java, prolog, oocaml, js, python, ...), so to me that: looks like a clusterfuck.
But of course, if I had a need where clojurescript was the best fit, and I had to use it daily for an appreciable amount of time, I'd acclimatize to it and consider this perfectly fine syntax. The same goes for ES6: it's objectively just syntax. If you think it's a nightmare, that's because you're the one doing the judging. Not because it is.
Comments (156) 28 threads
Earlier commentsconst 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.
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!
https://en.wikipedia.org/wiki/Code_golf
I very much prefer
to They're both clear, neither needs comments, but one is unnecessarily verbose.Continue this thread — 12 replies
Technically it's consistent with ordinary practice. e.g.
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).
Code should be stupidly obvious so that reading it consumes as less cognitive resources as possible.
[1]: https://github.com/leontrolski/leontrolski.github.io/blob/ma...
edit: Ok, I should have read the article first. It's probably for https://mithril.js.org/
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
Or the constraints could be different where the bundle size vs performance implications could be worth trading.
Depends on context :)
Continue this thread — 1 reply
The "diff & patch" pattern is super common, e.g. Git and Incremental by Jane Street: https://github.com/janestreet/incremental
> The programmer sets up the desired appearance of each window, then tells the curses package to update the screen. The library determines a minimal set of changes that are needed to update the display and then executes these using the terminal's specific capabilities and control sequences.
https://en.wikipedia.org/wiki/Curses_(programming_library)
"React in 33 lines" sounds reductionist. With the actual title, there's an implicit "a" (i.e. an indefinite article) at the start, i.e. "A 33-line React". In other words: a short exercise exploring the core idea of React. Not a claim of total equivalence.
Submitters: please read the site guidelines. They include "Please use the original title, unless it is misleading or linkbait; don't editorialize." Much of the time, when people rewrite a title, they make it more misleading or linkbait, even if they didn't mean to.
https://news.ycombinator.com/newsguidelines.html
As someone who has sort of lost contact with the front-end world, it's nice to see a coding TL;DR.
That's why you can do anything (declarative) in them. You can loop, have conditionals, etc. without having to learn a template syntax.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Looks weird at first but you get used it pretty quickly. And I gotta say the auto binding thing is awesome.
Thankfully, most of the time we learn that's just us, and move past that. Arrow functions have been around for nearly five years now: they're fine. You learn that they're just part of JS, and you see them everywhere in modern code, just with normal indentation and sensible white spacing to keep them perfectly readable (because minification will solve the "the code as written is too verbose" part of the dev cycle).
Or even, "actually, why not this?" To which the answer of course is "this isn't production code, arrow functions are fine. I mean come on, it's just a fun little JS exercise, stop trying to turn fun into work".The important thing is what they do: bind declaration context as execution context, so `this` keeps meaning the same thing, which drastically simplifies a lot code.
In this case, there is simply no reason to use arrow functions at all, because we don't need to preserve the declaration context (there is no `this` that needs to be preserved), so if this were real code you'd probably use a normal function, and your comment should probably be "why not this?"
Here's the same thing in clojurescript. Tighter, balanced and no chaining of symbols.
But of course, if I had a need where clojurescript was the best fit, and I had to use it daily for an appreciable amount of time, I'd acclimatize to it and consider this perfectly fine syntax. The same goes for ES6: it's objectively just syntax. If you think it's a nightmare, that's because you're the one doing the judging. Not because it is.
All comments shown.
Couldn't load more comments. Check your connection and try again.