Test-driven design
startuplab.googleventures.com
startuplab.googleventures.com
Testing is a good tool for QA, but I find it's even better as a tool for design.
Writing code that's designed to be testable helps clarify responsibilities, dependencies, interfaces, and relationships.
The bit where you actually run the tests is less interesting to me. :-P
TDD is necessary for dynamic languages with weak types. In other cases unit tests are good to ensure some properties, but TDD is not the good tool at all.
Fundamental limitations in the various forms of static analysis means that TDD is often quicker to work with (agility) and often way more expressive. Completeness that proofs depend on when the design is being formed is also often very elusive, which is why Haskell isn't such a great rapid prototyping language while LISP is.
The practical approach is to test the complicated stuff, and design your code so that you can test it if you need to. This leads to better modularity and easier reusability across the board.
At the very least, it completely changed the way I think about and write code.
I'm not very familiar with Javascript but : The design used to solve test problems here is to pass the useful variables in the parameters of the function instead of using global variables.
Is it not slowing down all the program by passing all these variables by value ?
http://stackoverflow.com/questions/518000/is-javascript-a-pa...
Also all the expensive values are objects, and only references to them are copied, not the objects themselves.
Passing references is quick, even by value: it's just passing around pointers. Here's basically how it would look in say, Go, with pointers:
package main
func main() {
var x = 10
test(&x)
println(x)
}
func test(ptr *int) {
var y = 30
ptr = &y
}
And the equivalent JS: function main() {
var x = 10;
test(x);
console.log(x);
}
function test(ptr) {
var y = 30;
ptr = y;
}
// depending on environment, either call main() or run on onload or DOM ready
The main difference between the two is that in JS, x and y were already pointers; in Go, x is the actual value 10, and y is the actual value 30. However in both cases the test function takes a pointer that is itself passed by value.The programs, naturally, both print 10 and not 30. The pointer was passed by value, and modifying the value of the pointer only changes the pointer, not the referenced variable. To change the referenced variable you'd have to dereference the pointer — and since JS doesn't provide access to raw pointers, that's impossible in JS. You can, however, mutate the variable using array accessors or dot notation; for example, in Go:
package main
type Vertex struct {
X, Y int
}
func main() {
var z = Vertex{X: 1, Y: 2}
println(z.X)
test(&z)
println(z.X)
}
func test(ptr *Vertex) {
ptr.X = 10
}
And once again the equivalent JS: function main() {
var z = { X: 1, Y: 2 };
console.log(z.X);
test(z);
console.log(z.X);
}
function test(ptr) {
ptr.X = 10;
}
// depending on environment, either call main() or run on onload or DOM ready
As expected, the programs will first print 1, and then print 10.As you can see, pass-by-value in JS is generally the same as pass-by-reference in other languages; it's all pointers under the hood.
Also, when you write code this way, you don't end up calling the constructors very often. You end up with a big up-front construction/wiring-up phase, after which your objects/functions go about their business handling user input and making server requests.
I've found that this, combined with an MVC or MVVM methodology, essentially breaks my code down into functions and data records, with the two rarely intertwining. You basically only use the "new" operator either at the start of the program's lifetime, or when you're converting data from one data type to another.
So it's OK as long as you use objects and not a simple integer as a counter for instance.