Innovate
Your Software
Faster without Risk
Innovate Your Software Faster without Risk
Feature Branch vs. Feature Flag: Who is Ruining Your Development Process?
In a recent reddit post, a developer argued that feature flags are ruining the codebase. This post received over 300 upvotes supporting the idea. The main argument was that teams indiscriminately use feature flags throughout the codebase, complicating the code and making it hard to maintain.
Conversely, a reply to the post garnered over 750 upvotes, arguing that "There’s no silver bullet solution that’s great in all respects. There are choices with differing trade-offs. Nothing is going to perfectly solve this problem, except maybe ignoring business realities and going back to waterfall."
In this post, I will discuss some of my thoughts on feature branches and feature flags, and when to use each of them.
Conception
A feature branch is a separate branch in a version control system (such as Git) created specifically for implementing a new feature or fixing a bug. Feature branches allow developers to work on new features or bug fixes without affecting the main codebase. They enable parallel development, where multiple features can be worked on simultaneously by different developers.
A feature flag is a technique used to enable or disable features or sections of code at runtime (or build-time, compile-time) for a controlled group of users, without deploying new code. Feature flags allow developers (or non-engineer teams) to gradually roll out features, conduct testing in production, perform A/B testing, or quickly disable features if issues arise in production.
Cons of Feature Branch and Feature Flag
Cons of Feature Branch
In my opinion, the cons of using feature branches can be summarized in one main point: Long-lived branches, which lead to:
- Diverging significantly from the main branch over time, which increases the likelihood of integration issues when attempting to merge changes back into the main branch. Conflicts may arise between the changes made in the long-lived branch and those made in the main branch during its lifespan.
- The longer the branch exists, the more likely it is that the code will become outdated and require significant rework to align with the main branch.
- The more extensive the changes needed to merge back into the main branch, the more code review and testing will be required to ensure the validity of the changes made in the branch.
- The more maintenance and collaborative work a feature requires within the branch, the more likely it is that the feature will be abandoned or postponed.
This complicates your development process and creates bugs in production.
Cons of Feature Flag
- Increased Technical Debt: If not properly managed, feature flags can accumulate over time, leading to increased technical debt and maintenance overhead.
- Testing Overhead: Testing variations of features controlled by feature flags requires additional effort to ensure all code paths are thoroughly tested.
Solutions for Cons
Teams should not cease using feature branches or feature flags but should employ them wisely. Here are some tips for resolving the cons of feature branches:
- Short-lived branches: Keep feature branches short-lived and merge them into the main branch frequently to reduce integration issues and conflicts.
- Small changes: Break down features into smaller, manageable pieces that can be implemented and tested independently to reduce the complexity of merging.
- Regular code reviews: Conduct regular code reviews to ensure that changes in feature branches align with the main branch and meet the team's coding standards.
If a feature cannot be broken down into small runnable pieces, you can use a feature flag to wrap the sub-feature code and disable it by default. There are two ways to manage this kind of feature code:
- Runtime feature flag: Enable or disable features at runtime based on your rules. Enable the feature when it's ready to be tested and disable it when it's not.
- Build-time feature flag: Use build-time (or compile-time) feature flags to exclude or include feature code during the build process. This can minimize the impact on the main codebase and reduce the risk of conflicts when merging changes back.
For feature flags, you should also follow best practices to avoid cons:
- Specify lifecycle: Define the lifecycle of feature flags and remove them once they are no longer needed to prevent accumulation and reduce technical debt.
- Test variations thoroughly: Ensure that all code paths are thoroughly tested when using feature flags to identify any issues that may arise.
- Avoid using if/else blocks: Instead of using if/else blocks to control the code, create scalable "grammar sugar" to wrap the feature code. This makes the code more readable and maintainable.
// In front-end code
<FeatureFlag name="new-feature" enableValue="true">
<NewFeature />
</FeatureFlag>
// In back-end code
[FeatureFlag("new-feature", true)]
public IActionResult NewFeature()
{
return Ok("This is the new feature");
}
With code like this, you can easily find and remove the feature code by searching for the feature flag name and even using AI tools to do the job.
Practice example
- Estimate your story points, and break down the feature into small pieces, each with no more than 13 story points. Create a branch for each small feature and begin coding.
- If the feature is ready to be tested, merge the feature branch into the main branch. Use a feature flag to wrap the feature code and disable it by default.
- If the feature is suspended (or not ready to be tested) and won't be released in the upcoming deployment train, use a feature flag to wrap the feature code and disable it by default. Create a pull request to merge the feature branch into the main branch. To avoid long-lived branch issues.
- Once the feature is ready to be released, enable the feature flag and test the feature in production.
- If the feature is working as expected, you should remove the feature flag as soon as possible by following the team's routine.
Each team or industry has its own development process, so you should adjust these practices to fit your team's needs.
Conclusion
To better manage your code and meet business requirements as quickly as possible, feature branches and feature flags are essential technologies. Using them correctly can mitigate their cons and make your development process more efficient and less error-prone.