WHY SHOULD YOU APPLY TRUNK BASED DEVELOPMENT INSTEAD OF FEATURE BRANCHING?
In the software development process, minimizing development time and deploying in small modules is crucial and can significantly save costs. According to the Pyramid model, we can write more Unit tests and Integration tests instead of relying solely on Manual tests to save costs. Integrating CI/CD into the development process also greatly reduces product deployment time. However, what other methods can help save time during the software development process?
Today, I will discuss a software development approach called Trunk Based Development. So, what is Trunk Based Development? How does it work? How does it help reduce product development time? And most importantly, how can you minimize risks when applying this method? Let's explore these questions in this article.
Feature Branching and Its Limitations
During my time working at various companies, I've observed that most of them apply the "Feature Branching" method. According to this method, we start with the main branch called "master" and from there create a "develop" branch. From the develop branch, we continue to create feature branches to develop specific functionalities. Each feature branch can be further divided into smaller branches to handle individual tasks. Once the features are completed, the code from the feature branches is merged into the release branch, and from there, it is merged into the master branch.
We can imagine this like a tree canopy: if a project is large enough, say around 10 teams, with each team having about 5 features to develop, what would this tree canopy look like?
When a developer completes a feature and wants to merge it into the main branch, they must create a pull request. This pull request will be reviewed and evaluated by other developers before it can be merged into the main branch. This process can take anywhere from 1 to 2 days, as it often requires input from those who are well-versed in the business logic or have higher expertise to approve it. Additionally, the developer responsible for the feature may need to make adjustments based on feedback and wait even longer for the pull request to be reviewed again. All of these factors can significantly extend the time required to complete a feature.
Merging the release branch into the master, especially when it contains hundreds of new features, can pose significant risks. When large amounts of code from different feature branches are combined, the risk of code conflicts is very high, making the process more complex to manage. This situation can lead to hundreds of errors and potential bugs, affecting existing functionalities and causing serious harm to customers. These risks not only increase the workload but can also have a substantial impact on the product's quality.
So, how can we solve this problem? Trunk Based Development can be considered a solution in this case.
What is Trunk based development?
Trunk Based Development (TBD) is a software development method where all developers continuously integrate their code into a single main branch, commonly referred to as the "trunk" or "main." This approach aims to reduce complexity and enhance system stability by limiting the number of branches and frequently integrating small changes.
According to [trunkbaseddevelopment.com](https://trunkbaseddevelopment.com/), in large projects, short-lived feature branches are created, where a developer works on a branch for a few days before merging it into the trunk or master branch.
For smaller projects, each committer will commit directly to the trunk or master branch. With this approach, developers commit small pieces of code incrementally, reducing the risk of conflicts when merging large branches, while also saving time on pull request reviews and handling conflicts and bugs.
领英推荐
Minimizing the Risks of Trunk Based Development
Trunk Based Development (TBD) might seem like a risky method, but in reality, committing code directly to the main branch without adequate checks can lead to serious issues. To minimize these risks, it is essential to implement testing and quality assurance practices.
So, what are the methods that can be applied?
Automated Testing:
Unit Tests: Write and maintain unit tests to ensure that each part of the code functions correctly.
Integration Tests: Perform integration tests to verify that the system's components work well together.
End-to-End Tests: Conduct comprehensive tests to ensure the system behaves according to user requirements.
Continuous Integration (CI):
Use CI tools to automatically build and test the code whenever changes are committed to the main branch. This helps in early detection of errors and reduces risks.
Code Review:
Ensure that all changes are reviewed by other team members before being merged into the main branch. Code reviews can take place during Pair Programming, Desk Checks, etc. This process helps to identify potential issues and improves code quality.
Feature Flags:
Use feature flags to enable or disable new features without having to merge them into the main branch immediately. This allows features to be tested and deployed more safely.
In addition to the methods mentioned, adopting Pair Programming can also enhance code quality and ensure its optimization. This practice not only helps in early error detection but also improves code quality through continuous collaboration and evaluation.
Test Driven Development (TDD) is another effective method to enhance code quality. In TDD, tests are written before the code is developed. This approach helps developers improve their thinking around code quality.
During development, the involvement of QA can reduce the risks associated with Trunk Based Development by:
Combining these methods with Trunk Based Development creates a robust software development process, enhances code quality, minimizes risks, and ensures that features are deployed safely and effectively.
C++ Developer at Vietbando
7 个月Insightful
QA/QC | ITSQB
7 个月Very informative ??
Talent Acquisition, APAC at Thoughtworks
7 个月Nice sharing Thuc Hoang ??
Hello, Tech experts! We are excited to welcome you to our Thoughtworks team.
7 个月Great article!