Tri Vuong's Blog

Rants about Javascript, Rails, user interface design and other things

Month: December, 2011

Javascript quirks


Inspired by this post I would like to compose a list of Javascript quirks for my self and anyone else interested. Here comes the bride…

(1, 2, 3) = 3

1 + + 1 = 2

1 + – + 1 = 0

1 – – 1 = 2

1 – – – 1 = 0

+ + + + 1 = 1

– – – 3 = -3

+[] = 0

2 + [] = “2”

2 + + [] = 2

1 + false = 1

1 + true = 2

a = 1; a+++1; alert(a) //2

1 + [1] = “11”

1 == [1] //true

Simple programming rules I like


It was while reading this post that I found a comment by Patrick Balleux which I really like and want to share with everyone

“Good coding style is often limited by the language and the time allowed to solve the problem.

Bad code is often generated with short dead lines and by dreamers. When I say “dreamers”, I’m thinking about those trying to prove the world that they can create an amazing new framework where their code will solve everything.

I follow those simple rules:

– make it work, then make it clean, then make it fast
– if it looks cool, this is a bad idea
– if it does everything, it will do nothing
– if it need comments, it’s badly coded

Code should be easy to understand. Would you read a book if you were required to use a dictionary to understand each line? Same applies to code.

The quality of code often represents the understanding of the issue to solve. The first step if to have a clean design, well documented. No coding should happen if you cannot understand the requirements first.

My 2 cents”
Patrick Balleux

Learning Scala: Array and List


Array

Define an array

  • var myArray = new Array[Int](3) (the type of myArray is inferred)
  • var myArray:Array[Int] = new Array[Int](3) (explicit type declaration)
  • var myArray = Array.apply(1, 2, 3)
  • var myArray = Array(1, 2, 3)

Accessing an element in an array

  • myArray(0)
  • myArray.apply(0)

Changing an element in an array

  • myArray(0) = 100
  • myArray.update(0, 100)

List

Define a list

  • var list = List(1, 2, 3)

Append an element to a list

  • list = 1 :: list

Some useful list methods

  • list.map(element => element + 1) (increments each list element by 1)
  • list.exists(element => element < 0) (determines whether the list contains a negative element)
  • list.filter(element => (element % 2) == 0) (filters even numbers)
  • list.isEmpty (self explanatory)
  • list.head (returns the first element of a list)
  • list.last (returns the last element of a list)
  • list.reverse (returns a list with its elements in reverse order)
  • list.remove(element => (element % 3) == 0) (remove all multiples of 3 from the list)