Your development job is making changes in your software. Writing, testing and debugging those changes takes some time.
If your job is anywhere as hectic as mine, you will have to fix and deploy urgent patches, even when your application code is in a half-written, half-debugged state because of the feature of the month.
This is what branches are for. You keep two versions of the code, one of which is called the trunk and is always ready for deployment, and another which holds the changes that you are working on.
When your feature is done, you merge the two versions together. You want to keep the merge operation painless. To do so, you have several kinds of branches available.
The repository branch is built into your SourceSafe/subversion/git/whatever. It creates two independent copies, and you need to migrate changes from the trunk to every branch out there as soon as possible, or the merge will make you wish for a sweet and merciful death.
By the way, changeset-oriented tools (like git or mercurial) make this easer, while revision-oriented tools (like subversion) make it harder.
The feature branch is done using programming logic. The code you deploy to production supports the new feature, but it is turned off for everyone except yourself. This technique is great for adding features, but inefficient when changing existing ones.
A side effect of the feature branch is that you can stress-test new code by rolling it out to increasing numbers of users progressively.
The comment branch is an odd gambit. It involves ripping out an entire module and replacing it with another that has a different interface. This will involve large amounts of re-wiring all over the code base, and these will take hours or days before they can be compiled, let alone tested.
Use a comment structure such as this one:
/*[*/ old code /*|* new code *]*/
It is trivial to build a text-replacement macro that turns the above into the code below and back:
/*[* old code *|*/ new code /*]*/
Use the macro to switch between development mode (when you write new code and desperately try to get it to compile) and fix mode (when you edit the old code and deploy it). For consistency, always commit the old version to the repository.
Why use comment branches instead of repository branches ? Maybe your source control tool sucks at branches. I use Subversion. Yes, I know. Legacy, pain and unlikely hopes of a brighter future.
When a trunk change occurs in a part that has been erased or reworked in the branch, that change will cause a conflict that will require manual intervention. Even with git or mercurial. For a large number of small changes sprinkled over a large codebase that is routinely involving many small updates, repository branches turn into a merge minefield.
Does your branch involve a small number of well-defined files ?
Then you should use repository branches, because conflicts will only happen in those files, and will usually be easy to fix.
Does your branch involve many changes in many files everywhere in the project ?
Then use comment branches.
Last and possibly least, there is the TODO-branch. This involves non-breaking, purely cosmetic changes. 25% of my project uses this syntax for historical reasons:
Table.get id |-> function | None -> return 0 | Some value -> return value.count
Then, a convention change happened, and this is used instead:
let! value_opt = breathe (Table.get id) in match value_opt with | None -> return 0 | Some value -> return value.count
Then, another convention change happened, and this should be used instead
let! value = breathe_req_or (return 0) (Table.get id) in return value.count
And then, there’s the current version:
let! value = breathe_req_or (return 0) $ Table.get id in return value.count
Whenever I change coding conventions, I do not spend the time to reformat the tens of thousands of lines of code in my application. That would have been wasteful. Instead, every time a piece of code is refactored, it is refactored to the most recent style.
The same happens when using an old and a new version of a given API. My code uses two libraries for handling HTML forms, uses both Javascript and Coffeescript, and a variety of similar two-hammers-one-nail situations.
These are, for all practical purposes, branches. They are work that is being performed for long durations. The benefit of TODO-branches is that code in the middle of such changes is still compatible with the trunk. It all happens in the head of the developer, who remembers what changes should be done the next time a piece of code is rewritten.
Article Image © Dominic Alves — Flickr






Hi. I'm Victor Nicollet,
Recent Comments