Lurker News

Comments (156) 28 threads

Earlier comments
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.
libria12 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.

Continue this thread — 12 replies

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.
andrewstuart3 replies#
Really not React in 33 lines at all.
Jean-Philipe1 reply#
Care to elaborate?
anonytrary1 reply#
It's a very, very minimal implementation. The diff algorithm they use does not really have the necessary optimizations implemented.
capableweb1 reply#
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 :)

Continue this thread — 1 reply

emmelaich#
And curses!

> 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)

dang#
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

tudorizer2 replies#
This also doesn't mix pseudo-HTML (JSX?) with JS code, so that's cool.

As someone who has sort of lost contact with the front-end world, it's nice to see a coding TL;DR.

frosted-flakes#
Why is it good? JSX is just a declarative syntax for creating DOM elements (usually), and in practice it works really well.
s_y_n_t_a_x#
JSX is syntactic sugar for nested function calls, it is JavaScript.

That's why you can do anything (declarative) in them. You can loop, have conditionals, etc. without having to learn a template syntax.

fnord774 replies#
OT this is the first time I've seen the arrow function syntax in ES6. My god it is horrible.

    const Cell = (value, i, j)=>m('button.cell',
        {onclick: ()=>move(value, i, j)}, value
    )

I can see why people are using things like clojurescript instead.
fulafel#
Re the Clojure comparison, it's not much of a consolation as the function binding problem doesn't exist there.
pgt#
Wait till you see what it does to scope!
TheRealPomax1 reply#
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".
fnord771 reply#
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)}))
TheRealPomax#
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.

hombre_fatal#

    add = (a, b) => a + b

    xs.filter(x => x > 10)
Are so elementary I'm not sure how they can look new to you unless you're new to programming.

All comments shown.