Live data from Hacker News

How true hackers write JavaScript

news.ycombinator.com

41–50 of 342 posts

Re: How true hackers write JavaScript

#42
This style is very similar to one which is used by expert C programmers, it seems that most people that write like this have had experience writing parsers and compilers too. It's not their fault that most people don't just think purely in terms of function composition :P

Re: How true hackers write JavaScript

#43
post #4

That might be one of the most readable pieces of code that I've ever read.

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 here? Not very much in my opinion.

Re: How true hackers write JavaScript

#44
post #4

That might be one of the most readable pieces of code that I've ever read.

It seems some of the one-liners are simply renaming JavaScript built-ins like forEach to aeach and slice to acut. Maybe this is to make it look more like the Lisp backend? But taken on its own it does not really improve readability.

Re: How true hackers write JavaScript

#46
post #35

Earlier quoted context omitted.

Maybe it's neat but this is not how anyone would write it today. This is the legacy code still left from the time before ajax was invented.

Hacker News was launched 2007. XMLHttpRequest has been around for about 7 years then and using it was called Ajax for about 2 years already.

Not to mention the "ajax" function in the script...

Re: How true hackers write JavaScript

#47
post #41

function aeach (fn, a) { return Array.prototype.forEach.call(a, fn) } Does this win anything over using forEach directly?

A lot of things like this will work on both arrays (thta have a .forEach method) and things that behave enough like arrays such that Array.prototype.forEach works for them.

The downside of this approach (if you subscribe to the ideology of OO) is that if you make something completely unlike an array internally, like a linked list, you can’t use this function on it. Whereas, if you wrote a .forEach method for it, you could use it interchangeably with arrays anywhere the code calls .forEach.

Without trying it, I’ll guess that this is used on the arguments magic parameter. It’s notorious for being array-like but not an array.

Re: How true hackers write JavaScript

#48

Earlier quoted context omitted.

Really? Plenty of needlessly abbreviated function names IMO. At a quick glance is it immediately obvious what posf or arem are without looking into the context in which they're being used? Don't get me wrong it's a nice piece of code but super-readable it isn't.

Well, I disagree. It is abbreviated, but since the whole file is self-contained and the definitions are put at the beginning, there is enough information to infer the function's semantics without being excessively specific. I think well defined organization of abstractions is an often overlooked but surprisingly effective way to achieve readability

> there is enough information to infer the function's semantics without being excessively specific

You mean you can just read the function definition to see what it does?

Re: How true hackers write JavaScript

#49

https://twitter.com/triskweline/status/798443082740023296 > Valve's Steam Store renders on the server, uses ancient jQuery 1.8, loads 12 unminified JavaScripts. > It moved 3.5 billion dollars in 2015.

I'm not sure of what it's supposed to prove, who knows how many clients and how much money Steam lost because of slow loading/painting times or just plain broken JS. From experience I can tell that I gave up buying a game on Steam at least once because of broken jQuery.

There are real metrics from top industry leaders[1][2] to prove that loading time has a clear impact on conversion rates.

>Consider this when you need 5000 npm modules and a team of 10 to compile your startup's login form.

Unminified JavaScripts files and bloated JS are both wrong, once again: not sure of what's the point of comparing both.

[1] https://medium.com/@vikigreen/impact-of-slow-page-load-time-... [2] http://glinden.blogspot.com/2006/11/marissa-mayer-at-web-20....

Re: How true hackers write JavaScript

#50
post #41

function aeach (fn, a) { return Array.prototype.forEach.call(a, fn) } Does this win anything over using forEach directly?

This function could be used to iterate over the `arguments` array like object, but I do not think it is used for that purpose here.
Post reply on HN