Skip to content

Instantly share code, notes, and snippets.

View NickHeiner's full-sized avatar
💭
Wubba lubba dub dub!!

Nick Heiner NickHeiner

💭
Wubba lubba dub dub!!
View GitHub Profile
@NickHeiner
NickHeiner / task-slow-bad.js
Last active August 29, 2015 14:15
A task that loads slowly
'use strict';
// This code gets run no matter what,
// so if this dep is slow to load,
// you will pay that tax every time you run `grunt`.
var bigDep = require('big-dependency');
module.exports = function(grunt) {
grunt.registerTask('my-task', function myTaskFn() {
bigDep();
@NickHeiner
NickHeiner / task.js
Last active August 29, 2015 14:15
Grunt task control flow - the real way
grunt.registerTask(‘task-a’, function() {
var result = doStuff();
grunt.registerTask(‘after-task-b’, function() {
doMoreStuff(result);
});
grunt.task.run(‘task-b’);
grunt.task.run(‘after-task-b’);
});
@NickHeiner
NickHeiner / task.js
Created February 17, 2015 23:54
Grunt task control flow - a nicer way
grunt.registerTask(‘task-a’, function() {
var result = doStuff();
grunt.task.run(‘task-b’).then(function() {
doMoreStuff();
});
});
@NickHeiner
NickHeiner / Gruntfile.js
Created February 17, 2015 23:37
A sample gruntfile
'use strict';
var sdk = require('internal-sdk');
module.exports = function(grunt) {
sdk(grunt, {
'project-type': 'widget'
});
};
@NickHeiner
NickHeiner / bower
Created February 13, 2015 23:41
Single entry of a bower json
"lodash": "^2.4.1"
@NickHeiner
NickHeiner / deps.txt
Created February 13, 2015 23:38
sample dependency tree
A
└── B
│ └── E
└── C
│ └── E’
└── D
└── E’’
@NickHeiner
NickHeiner / .bowerrc
Created February 13, 2015 23:37
A sample .bowerrc
{
"endpoint": "http://bower.opower.it/",
"searchpath": [
"https://bower.herokuapp.com"
],
"registry": {
"search": [
"http://bower.opower.it/",
"https://bower.herokuapp.com"
],

High level style in javascript.

Opinions are like assholes, every one has got one.

This one is mine.

Punctuation: who cares?

Punctuation is a bikeshed. Put your semicolons, whitespace, and commas where you like them.

Is this worth it?

I've been editing this in response to comments below, but I will preserve the full revision history.

Referencing the function execution context via this is fairly common in JavaScript. I suspect some people like it because they come from a language like Java, and it’s familiar. However, I would argue that it in some cases introduces brittleness into one's code without bringing much benefit.

For example, let’s consider a contrived logger module:

function CountingLogger() {
 if (!(this instanceof CountingLogger)) return new CountingLogger;
module.exports = function(grunt) {
grunt.initConfig({
foo: {
options: {
bar: {
minify: true,
scripts: ['a', 'b', 'c']
}