Javascript Best Practices

## Javascript Best Practices

# Spaces around operators
Good:
5 + 4
Bad:
5+4

# Space after every comma
Good: 5, 6, 7
Bad: 5,6,7

# One statement per line
Good:
a+3
b+4
Bad:
a+3;b+4

# Always use braces for blocks
Good:
if a {
stuff;
}
Bad: if a
stuff();

# Use {‘s at the end of a line
Good:
if a {
Bad:
if a
{

# Use line comments within code
Good:
// these are
// comments
Bad:
/* these
are comments */

# Declare variables at the start of functions (function scope)
Good:
var x = 0;

x+=1
Bad:

x+=1

# For comparisons use === or !==(objects, same type, preferred use) or use == or !=(values, type doesn’t matter) instead of =
Good:
if a === b

if adr1 == adr2

Bad:
if a = b

if adr1 = adr2
..

# Always break out of of switch case statements
Good:
switch(expression) {
case n:
code block
break;
case n:
code block
break;

Bad:
switch(expression) {
case n:
code block
case n:
code block

Use a global variables to namespace an application

Good:
MyAmazoApp.recently_viewed = 1
Bad:
recently_viewed = 1

Advertisements

Improving my vim

Selecting:
:10,12m5 # Move (dp) lines 10,12 to line 5
:20,40co10 # Copy (yp) lines 20-40 to line 10
:.,$ d # Delete from currrent line to end of file
:20,30 m $ # Moves lines 20-30 to end of file
:,+20 y # yank current 20 lines

Commands(ex):
:x # write if modified and quit.
:100r y.y # read in file at line 100
:e file # edit another file
:e # # edit secondary file
:e % # edit primary file
:w %.backup # save a backup with [current filename].backup
[ctrl]^ # Switch between primary / secondary files !

Substitutions(ex):
:s/old/new/g # For all occurances, current line
:%s:ENTER:& # "wrap" ENTER in italics, entire file, first line instances
:%s/ */ / # collapse multiple any length to single spaces only
:%s/^/>--- # Add >-- at the start of all lines
:1,10s/old/new/g # For lines 1-10 of the file
:;+10s/old/new/gi # i = ignore initial case
:1,100s/a/b/gc # c = confirm each one (y[enter] or [enter])
:g/pattern/s/a/b/g # within lines that have /pattern/ ...
:g/overdue/s/$/ NOW! # within ... Append NOW! to lines with "overdue" in them

Patterns (regular expressions).
/p.p # pep pap pop
/p.*p # pep peep seeeeep soop pp
/^q # q quick quote qin
/^qn$ # qin
/^\^ # ^chr
/h[aeiu]ll.* # hall hell hill hull hall hilly ...
/h[ae][ae]l # heel
/house\|home # hightlights all instances of house or home & goes to first
/house.*home # Only lines with house followed later by home
/house.*home\|home.*hous # Lines with both (either house then home, or home then house)
/[house]\|[home] # ALL h's, i's, o's, m's, e's, u's
/[house|home] # ALL h's, i's, o's, m's, e's, u's

Window Splits (without tmux):
:new # New horiz pane with new file
:vnew # New vert pane with new file
:split # Same file, new h pane
:vsplit # Same file, new v pane

Other:
vi +100 x.x # open at line 100
vi +/command x.x # open at text "command"
"1yy yank to buffer 1
"1p put buffer 1
mx = mark x
`x = goto x
:!ls # unix shell and ls command
:r !ls # unix ls and pipe output into file.
:1,10!zsort # Apply unix sort command to lines 1,10 and re-write them
cc # Change whole line
:set paste/nopaste # Stop all the text indenting on paste!
vimdiff a.rb b.rb # Better than diff

awk 1sies

One Liner awkisms

printf "Matching character:\n"
awk '/2/ {print}' numbers.txt # all lines with character "2"
printf "\nField 2 matches '2':\n"
awk '$2 - /^2$/ {print}' numbers.txt # all lines with field#2 matching "2"
printf "\nlines 1-5:\n"
awk 'NR == 1, NR == 5 {print}' numbers.txt # lines 1-5 of file
printf "\nfield 2 > 10:\n"
awk '$2 > 10' numbers.txt # 2nd field > 2
printf "\nx\/y\n"
awk '{print $1/$2, $2/$3, $3/$4, $3/$7}' numbers.txt
printf "\nMatching '16'\n"
awk '/16/ {print}' numbers.txt # Entire row when there is a string match
printf "\nMatching '16' all fields, otherwise print NO - field4\n"
awk '/16/ {print;next} {print "NO - "$4}' numbers.txt # Entire row when there is a string match
printf "\nAdd ++++'s at the start of lines with \"16\" in them:\n"
awk '/16/{sub(/^/, "++++ "); print;next;}{print}' numbers.txt
printf "\nRandom Numbers:\n"
awk 'BEGIN { for (i = 1; i <= 7; i++) print int(101 * rand()) }'
printf "\nlong param 2's:\n"
awk 'length($2)>1' numbers.txt
printf "\nshort lines:\n"
awk 'length<21' numbers.txt
printf "\nSum up ALL the 2nd fields...\n"
printf "2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 =\n"
awk '{ sum += $2 } END { print sum }' numbers.txt
printf "\nLine Numbers:\n"
awk '{print FNR "\t" $0}' numbers.txt
printf "\nPrint line 5 and exit:\n"
awk 'NR==5 {print;exit}' numbers.txt
printf "\nCount lines (emulates wc -l)\n"
awk 'END{print NR}' numbers.txt
printf "\nAdd all fields in all lines and print the sum !\n"
awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s}' numbers.txt
printf "\n------------------------\n"
printf "\nPrint each rows total:\n"
awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}' numbers.txt
printf "\nPrint first 4 lines of file (emulates behavior of head)\n"
awk 'NR < 5' numbers.txt