Live data from Hacker News

How true hackers write JavaScript

news.ycombinator.com

171–180 of 342 posts

Re: How true hackers write JavaScript

#171
post #98

I don't understand why people are dissing this. It does what it's designed to do, and fills a specific need for one website. It's not there as a teaching aid, nor is it meant to be shared for other people to use elsewhere. Not everything has to be gold-standard code full of perfect variable names, extensive comments and good whitespacing. If you have a day job that isn't primarily writing code, and/or you are likely…

The code is readable. There is no problem.

> As long as the code works, anything else is just gravy.

How did you end up with this opinion? That's an unpopular opinion even when considering the qualifiers you've listed in the paragraph above.

Re: How true hackers write JavaScript

#172

Earlier quoted context omitted.

Perhaps this why HN still uses tables for presentation - nobody dares to change it? There's also simply no reason to change it, it works.

> There's also simply no reason to change it, it works. It works, and it's arguably simpler than the div-soup with extensive styling, which is the "kosher" way of doing this.

Well using divs would at least make you able to group the elements semantically so you could remove a single node rather than having a for loop which removes exactly three nodes. This would be significantly simpler in my opinion. And it would be less fragile since now you can redesign everything inside this node without the JavaScript breaking.

Re: How true hackers write JavaScript

#173
I thought I was smart...

BEFORE

function vis(el, on) { if (el) { on ? remClass(el, 'nosee') : addClass(el, 'nosee') } }

AFTER

function vis(el, on) { if (el) { window[on ? 'remClass' : 'addClass'](el, 'nosee') } }

...sadly we have to add window when using the square brackets so it's not much shorter. Oh well.

Re: How true hackers write JavaScript

#174
post #116

There are some polarized opinions in this thread. Someone wrote That might be one of the most readable pieces of code that I've ever read. Apparently it improves readability immensely to rename 'forEach' to 'aeach'. To be honest the code is not very readable, but it is very simple and self contained. It would be very easy to dive into to fix a bug because there are no external dependencies or frameworks you have to u…

Perhaps this why HN still uses tables for presentation - nobody dares to change it? There's also simply no reason to change it, it works.

Sure, don't fix it if it ain't broken. Today there is no reason to use tables for presentation, but that was probably not the case when HN was initially designed many years ago.

Re: How true hackers write JavaScript

#175
"var", "function", one explicit "return" at the end of function body, working directly with the DOM? I still do this sometimes and the Angular/React/TypeScript kids look at me like at some alien wizard, while Java/.NET folks still think "it's just a dumb website".

Re: How true hackers write JavaScript

#176

I thought I was smart... BEFORE function vis(el, on) { if (el) { on ? remClass(el, 'nosee') : addClass(el, 'nosee') } } AFTER function vis(el, on) { if (el) { window[on ? 'remClass' : 'addClass'](el, 'nosee') } } ...sadly we have to add window when using the square brackets so it's not much shorter. Oh well.

Why not:

function vis(el, on) { if (el) { (on ? remClass : addClass)(el, 'nosee') } }

?

Re: How true hackers write JavaScript

#177

Earlier quoted context omitted.

Really? It has horrendous naming and it isn't even using es6 function byClass (el, cl) { return el ? el.getElementsByClassName(cl) : [] } const byClass = (el, cl) => el ? el.getElementsByClassName(cl) : []

Your code is not more readable, it is just shorter. In fact, using the online babel transpiler, it transpiles the es6 version you provided precisely into hn's version: https://bit.ly/2MPmjK7 To use es6 to achieve what this tiny piece of javascript could already do, you probably need to introduce some tremendous dependency like babel just to be compatible with all kinds of weird browsers out there. What does es6 give…

It's 2018. Unless you're using Internet Explorer arrow functions are supported natively.

Re: How true hackers write JavaScript

#178

Um, is this supposed to be a criticism? Because this is MUCH better than sites loading megabytes worth of scripts to do nothing more than render text, have dozens of floating elements everywhere, auto playing videos that follow you, "use our app" buttons, etc... JS is the assembly of the web, do you also criticize games written in assembly?

macOS just notified me that one tab in Safari was using so much memory it was threatening performance of my laptop. The website in question? https://idioms.thefreedictionary.com/ It was close to 2Gb RAM usage. Bunch of ad-tracking stuff, I assume, as Firefox and its related processes seem to be using less than 1250mb with 6 tabs open, including the site mentioned in 2 tabs. My firefox has uBlock origin installed. See…

Last week my bandwidth grounded to a halt which is usually the result of someone uploading on my network.

I went to my girlfriend's computer to see if she was syncing with Dropbox or something. Nope. She quit all of her applications just to be sure. Except for a single browser tab opened to a recipe for pita bread.

Yep, turned out that a recipe for pita bread was saturating our router because what looked to be buggy error-handling for ad-related network code. I think her ad-blocker only partially maimed it, so it went apeshit.

Re: How true hackers write JavaScript

#179
post #97

It works... every criticism beyond that is just an argument about taste, although personally I think it's unnecessarily terse. I've written uglier code and gotten paid for it. ¯\_(ツ)_/¯

Not necessarily. Sure, all code has to work, but unless you're going to ship a product and then never, ever return to it, it has to be maintainable as well.

There are a few arguments in this thread about poor maintainability of this code, when coupled to the HTML it affects (which always must be considered, otherwise this code is pointless), and I tend to agree with them.

Personally, I wouldn't mind some less terse names and some more semantic groupings of HTML nodes.

Thankfully, it is simple. I'm pretty sure doing that would be the task of an afternoon, though I would definitely want to accomplish that before trying to change anything else on the page.

Re: How true hackers write JavaScript

#180
He/she could have put much of the function in the top under the $() to make a super mini jquery. That's close to what I use with Vue. It's made even easier to write considering I almost never find a need for jquery's default behaviour of running its methods on multiple nodes. 99% of the time in my projects I just need to do something with one node.
Post reply on HN