Skip to content

Instantly share code, notes, and snippets.

@kranrao
Last active August 29, 2015 14:12
Coding best practices
///// Scopes /////
- As little variables in global scope as possible
- iffes (immediately invoked function) are useful for creating closure and private variables
- Do not mutate inputs. But if you need to, be absolutely clear about it in your documentation
///// Closures /////
- Functions create new closure scopes
- A closure is a function object that retains ongoing access to the variables of the context it was created in (even after the outer function calls it was created within have returned)
- Execution contexts (in-memory closures)
- Bind binds execution context to function
- callback: function(){fn.call(context)} === fn.bind(context)
- iffe is a self invocating function. Creates immediate closure. e.g. var a = 1, (function(b){console.log(b)})(a) // returns 1. function defined and a immediately passed in for invocation.
///// This /////
- 5 patterns to detemine what the keyword 'this' is bound to at call time
///// Debugging /////
- 'debugger' statement in code stops execution at that point
- use 'sources' tab in console to trace stack
- 'step-over' tells debugger that you are uninterested in the details of a function or line of code
- 'step-in' tells debugger that you are interested in the details of a function or line of code
- 'step-out' tells debugger that you are done with what you 'stepped-into'
- 'breakpoint' is better to use when you are unsure of where debugging process starts, i.e. large block of unformatted code
///// Scribe vs. Conjuror /////
- short functions: if over 10 lines, can it be shorter?
- do not go over 80 characters in a line
- do not mutate inputs
- think MVP. Can always refactor code later
- be both a conjuror (jumps in and uses intuition, ignoring complexity) and a scribe (understands whole system)
- encapsulation: the drawing together of related systems
- isolation: the space in between, i.e. how isolated each system
- modularity: the degree to which you do encapsulation. More modular systems have smaller encapsulation
- loose coupling: the breaking through between systems
- thin interface: make function signature as simple as possible. Expose as little of your system as possible, e.g. apis
- private implementation: do not expose the details of implementation. If outside world knows your queue class is built on an array they may build their code off of an array. Then you make a change and their code breaks
- abstraction: ignore complexity by making simpler to understand
- time complexity: want programs to run in fastest amount of time possible
///// Instantiation Patterns /////
- functional and pseudoclassical are the most prevalent
- browsers are optimized for pseudoclassical
- functional is easier to understand and ideal for a small number of instances of a class
///// Complexity /////
- strive for constant time
- think about big picture: algo at much larger scale
///// Other /////
- JavaScript has no concept of private variables, so sometimes API and implementation are both visible. To denote a private variable, prefix with a an '_'
///// Test Driven Development /////
- Red (write a failing test), Green (make test pass), Refactor (clean up and improve code)
- inverts software development process on its head: think about how you want to test, and then write code
- polarized whether TDD is practiced at a company or not
- always strive for TDD
///// Style /////
- read and follow style guide (everyone and every system has there own style guide)
- never use tab key
- always use '==='
- watch hoisting: (1) variable declarations are brought to the top of current scope before execution, but assignment of their values stay where it is in the code; (2) function declarations are hosted in their entirety
- declare variables at the top of a scope
- do not ever use 'with' statement
- do not ever use 'switch' statement (hard to debug)
- do not ever use anonymous functions (hard to debug)
- do not ever us primitive constructors, i.e. new String() (causes unexpected errors)
///// Algorithms /////
- a step-by-step list of directions to solve a problem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment