JavaScript Hacks for Hipsters
slideshare.net
slideshare.net
EDIT: this is "fake surprise" isn't it. I'm sorry HN. Everyone has to learn this stuff somewhere. But the title was kindof asking for it.
To any newly minted developers out there, the best way to learn this stuff is just to read other peoples code. Pick the library you enjoy using the most and read every file until you understand what all the constructions and bits of syntax are for. Then compare it to another library, etc etc. Develop a taste for some and a distaste for others. There's no excuse not to learn experimentally in js as it has a repl. Use the source luke!
Just because people used to punch holes in cards doesn't mean they didn't have l33t hax.
---------
Example using strings:
var obj = {
start: function(){
'use strict';
console.log('start');
},
stop: function(){
'use strict';
console.log('stop');
}
};
exports.onDummy = function(success){
'use strict';
var method = (success ? 'start' : 'stop');
obj[method]();
};
results in: var a={start:function(){console.log("start")},stop:function(){console.log("stop")}};exports.a=function(b){a[b?"start":"stop"]()};
---------Same thing, using if..else:
var obj = {
start: function(){
'use strict';
console.log('start');
},
stop: function(){
'use strict';
console.log('stop');
}
};
exports.onDummy = function(success){
'use strict';
if (success){
obj.start();
} else {
obj.stop();
}
};
results in the much shorter: exports.a=function(a){a?console.log("start"):console.log("stop")};1) Much less legible, and in some cases outright lazy 2) Extremely vulnerable to Javascript's fast-and-loose type checking.
However it's good to be AWARE of them, since you tend to see them a lot in team projects.
Not everyone understands why && and || are used outside of if statements. Not everyone knows about joining strings using an array.
This summarises the things you'll have to know to read code in our codebase.
The && though, I've never used in this context. And I'm not sure if I've seen it out in the wild or not. I knew what it did right away so I imagine if I have seen it before I wouldn't remember.
I use the || trick really often, but never the && one.
By the way, why not use a one-liner if instead?
if (condition) something;
I find more readable this way.
Also, for example I use a sublime plugin to automatically tidy my code, and that isn't an option in it, so I don't do it anymore.
What else would you join if not arrays?
You'll probably get a solution using loops and a check if adding a comma is needed or not.
I know, I asked my friends. Ask your new teammates.
This may impugn my credibility as a professional developer, but I genuinely can't tell if this article is sarcastic or sincere.
Some seem like good ideas and some like really bad ones...
I have used the name || "no name" operator a lot and thought to myself that it looked cool on first impression. But having something like: isAwesome() && alert("yay"); Already makes it harder to read I think. I'm curious to what others think of this one? Maybe its just because it is new to me..
To be honest, I like and use some of those, especially when writing experimental/disposable code, but otherwise I try to be gentle to less experienced developers and my future self.
As of #5 - I used to do and love this when writing unit tests. At the end of the work day, if I'm in the middle of implementing a feature, I'd commit and then write the next unit test (inevitably) sitting in my head and leave it failing. Really helps when it's Monday.
C is particularly dangerous because it will normally short-circuit, but the standard doesn't require it and in rare cases the compiler will optimize it out.
http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/
1) do obj[if success then 'start' else 'stop']
2) ['milk', 'coffee', 'sugar'].join ', '
3) doStuff = (options = {}) ->
4) isThisAwesome?()
5) ### ... ###
6-8) ...
9) template = "Hi, my name is #{firstName} and my twitter screen name is @#{screenName}"
[1] http://coffeescript.org/#try:do%20obj%5Bif%20success%20then%...http://berzniz.com/post/68001735765/javascript-hacks-for-hip...
Makes for a nicer read than slideshare imo.
As I understand it its basically a low quality beer supposedly drunk by US hipsters in order to cultivate a kind of working class legitimacy. It's long since become a meme in itself though.
Is there any programmer since the 1970s that needs to be told this?
I've just started reading Clean Code, and there is a lot of emphasis on writhing readable code, because the philosophy is that code is only written once but read many times.
Stick to standards, so ease-of-communication is simpler! The reality is that collaboration is harder than programming!
In sublime for example, Ctrl+F2 marks a line, and F2 toggles between marked lines. (Ctrl+F2 removes the mark as well).
I like using .filter, .forEach and .map native methods on arrays. Also making use of Underscore.js where possible.
aCoolFunction && aCoolFunction();
What is the purpose of this?
If a function is coming up undefined that I expect to be defined, that's an issue - not one I want to fail silently - because that is no fun to debug.
If something I expect to be defined isn't, that should throw an error and stop execution.
There are some edge cases where it may not matter and there is a legit reason why a function may sometimes be undefined, but I've never written code like that and can't think of anything that doesn't have a much better alternative.
getJSON(); // See how I don't care about the result