Tools to scan and secure your website

SSL server test:

Mozilla Observatory:

Firefox plugin to generate content-security-policy (simply browse your website for it to work):

Sample nginx configuration for good security

Javascript idioms

From Zoltan Kochan, author of pnpm


Double exclamation

Prefixing anything with !! converts it to a boolean.

var foo = 0
//> false

Essentially it is a shorter way to write Boolean(foo).

Converting arguments to array

The arguments object can be used to access the arguments passed to the function. However, it is not an Array so it doesn’t have Array properties except length. The idiom is used very frequently to convert the argument’s object to an actual array.

(function() {
  console.log(arguments instanceof Array)
  //> false

  var args =
  console.log(args instanceof Array)
  //> true

Assigning default values

function foo(opts) {
  var msg = opts.message || 'Hello world!'

// instead of
function foo(opts) {
  var msg = opts.message ? opts.message : 'Hello world!'

More examples of interesting || and && usages can be found in the 12 Simple (Yet Powerful) JavaScript Tips article.

Converting to array if not already

var totallyArray = [].concat(value)

//instead of
var totallyArray = value instanceof Array ? value : [value]

Converting strings to number

var foo = +'12.2'
var bar = +'12'

// instead of
var foo = parseFloat('12.2')
var bar = parseInt('12')

Checking if an array includes an element

if (~[1, 2, 3].indexOf(2)) { console.log('includes') }

// instead of
if ([1, 2, 3].indexOf(2) > -1) { console.log('includes') }

There are some other usage examples for the tilde operator as well in The Great Mystery of the Tilde(~).

Writing multi-line strings

var multiStr = [
  "This is the first line",
  "This is the second line",
  "This is more..."

// instead of
var multiStr = "This is the first line\n" +
  "This is the second line\n" +
  "This is more...";

Looping through an array

It can be used if order is not important

for (var i = arr.length; i--;) {
  // ...

// instead of
for (var i = 0; i < arr.length; i++) {
  // ...

setTimeout(func, 0)

JavaScript code runs on one thread. Calling setTimeout with 0 allows to schedule a function to run after the current event loop tick.

setTimeout(function() {
  console.log('log message from next tick')
}, 0)

console.log('Hello world!')
//> Hello world!
//> log message from next tick

Comparison between class and traits in Scala


Singleton Can create new instance Can have a constructor Can be inherited
Class No Yes Yes Yes
Abstract class No No Yes Yes
Trait No No No Yes
Object Yes No No No

Also a small trick: Since an object cannot be inherited, we need to create a trait (a trait make sure the object won’t have a constructor) with the common values and functions of the objects we wanted to create, then both object would inherit the trait, with one object expanding on the trait.

This construction can be applied to perform Enumeration inheritance in Scala (since Enums by convention are created as objects)

Load data into controls in

Usually when working on windows form, I use this to do the work

        DropDownList1.DataSource = new ApplicationTableAdapter().GetDataBySystemID(1);
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "ID";

But surprisingly, when switched to ASP.NET, the above code no longer works, at first I thought it was a problem with the paradigm so I tried to store the data as a session variable but that didn’t work either, until I tried this

        DropDownList1.DataSource = new ApplicationTableAdapter().GetDataBySystemID(1);
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "ID";

One extra line.