Branch or not to branch
That is a question? Yes, it is a question which you should ask yourself before to create a new branch or reuse an existing branch. There are other practices or possibilities to create new features in an existing software, which I will show in this blog post. Also there are some issues with branches and modern software engineering practices, which I will discuss here too.
First I show you two techniques how to avoid branches: Branch by abstraction (BBA) and Feature toggle.
Branch by abstraction
If you never heard of branch by abstraction it doesn’t mean implicitly that you have never done it before. It is a quite simple approach: First create an abstraction of the existing code which you want to change (or refactor). Then create a new implementation of the abstraction. After you created the new implementation, declare the old implementation as obsolete (or deprecated). After a while you can delete the old implementation and maybe you could even remove the abstraction. All this actions happen in the main branch (or trunk). There is no need to create a new branch for that.
It sounds quite easy and you don’t loose the features of the continuous integration. But there are some issues with this technique: It isn’t always possible to introduce an abstraction after a released version. And it isn’t very beautiful to deploy duplicate behaviour to your customers when they should know yet the new implementation.
Feature toggle
Another technique to avoid the creation of a branch are feature toggles. You create the new feature in your main branch (or trunk), but you disable the feature by a toggle in your configuration file until the feature is ready to use. Maybe you have even a database driven menu management system or a complex user right management system in your application. If so, then you maybe use already feature toggles.
Also this technique has the same issue as the branch by abstraction approach: During you develop the feature and you have to deploy new versions of your software, you will also deploy the current unfinished version of your new feature. Only the toggle prevents that the customer see your unfinished feature. So it is important, that the customer can’t manage those toggles.
Isolate as little as possible
Branch by abstraction and feature toggle are techniques which help you to avoid creating branches for little and fast changes or adding new little features. But if you aren’t sure if you’ll brake something or adding side-effects then a new feature branch would be the better solution.
The argument that you have to create a feature branch because you couldn’t be sure that you can do all the work until the release deadline, is not always a good reason to create a new branch. Ask yourself why there is a time problem. If you have the possibility to improve the process with the project manager or with the product owner, then try to refine the features, optimize the task assignments in the team to deliver a feature until the deadline.