There are some apparently strange behaviours when working with Backbone models and properties.
This can easily be solved with a proper initialization method
Sometimes is useful to extend a class with a mixin rather than keeping deriving a subclass to add functionalities (Mixin). A mixin could be useful also to keep your class thin.
In Backbone this is quite easy
Then it may happens that we need to initialise the mixin, for example to setup some variables (in this example the initial value of the property is a simple number but this could lead to some problem in case the property is a variable passed by reference). The temptation is to call the initialize method of the mixin right in the extended class
But this is not a good practice since the extended class now depends on the mixin class. One big advantage of the mixin is that we can add the some methods to different classes without having to derive a subclass from each of them, a kind of simple multiple inheritance.
This works if the mixin is loosely coupled and my general rule of thumb is “if a method refers to something in the mixin, it must be in the mixin”.
We can solve it with a little bit of conventions and meta programming
Basically the initialize method of the Backbone class to be extended searches for methods like initialize* and execute them. With this convention we saved our loosely coupling (if we remove, add or change our mixin, nothing changes in the class to be extended).
I realized that I was compulsively hitting that button even for small changes.
When re-starting the project is a cost in terms of time – like in Titanium (consider the compilation and the time needed to navigate to the right feature) – it’s mandatory to write more code between reloads.
It turned out that now I write better code.
How many lines of code do you write on average between a reload?