Trunk Based Development Workshop

By Eylon Malin

Agenda

  • Small changes
  • Branch by Abstraction
  • Feature Toggles

Small changes

We want to merge small changes into main branch with "just enough" functionality + test

Small changes Examples:


  • Method that save new user in DB
  • Service that validate user details
  • HTTP POST route that create the user (only call the service method)

Big change Example


Create new POST route that create the user,
Including all needed code in:

  • Http level (controller)
  • Service level
  • DAL level

Small changes advantages


  • Early feedback
  • Quick and efficient code review
  • Early code merge
  • Easy merges

Test come along with functionality


  • For any such small change we merge it's test as part of the changeset (Pull Request)
  • Sometimes the test is just unit test/s and sometimes it include component or even end-to-end tests

Test First Development

Write the tests before you write the code.


    Some advantages:
  • Test is already there when functionality is done
  • Making sure test really test something
    (failed when functionality is missing)
  • Declare method signature/API before coding it
  • Enabler for refactor

Top Down / Bottom Up

Use latest city weather - Bottom Up

  • Introduce FavoritesService with set/get last city
  • Use favorite service in WeatherService
  • (controller level - test that validate the functionality)
  • Change FE to consume weather REST API without city upon loading

Use latest city weather - Top Down

  • Change FE to consume weather REST API without city upon loading
  • Controller level - support REST API without city - return last city
  • Move last city functionality down to WeatherService
  • Introduce FavoritesService with set/get last city
  • Use favorite service in WeatherService for get/set the last city

Exercise 1 - small changes

  • fork trunk-base-workshop git
  • Checkout branch lesson1/exercise-1-start
  • Change the code so by default the user will get forecast of his current location
  • Commit and push little changes covered by tests
  • Choose strategy: top-down or bottom-up

Exercise 1 - some tips

  • Use openweathermap API for weather and forecast
  • You might use the following frontend code for getting the user location:
    
    function getCurrentLocation() {
      return new Promise(function(resolve, reject) {
        navigator.geolocation.getCurrentPosition(resolve, reject);
      });
    }
                        

Branch By Abstraction

Branch By Abstraction definition

"Branch by Abstraction" is a technique for making a large-scale change to a software system in gradual way that allows you to release the system regularly while the change is still in-progress.

Martin Fowler

Branch By Abstraction step by step

  • There is a piece of code you want to change, the code is consumed elsewhere in your system
  • Create an abstraction of this code (e.g. extract an interface)
  • Make the consumers of this code to consume the abstraction instead of concrete implementation
  • Create the change you want to make as another implementation (supplier). Implement it in small steps

Branch By Abstraction step by step - cont

  • Once the new implementation is ready switch the system to use it (or use feature toggle)
  • Clean leftover while needed (like removed previous implementation)

Illustration by Martin Fowler

Dependency Injection

  • Dependency injection is a technique in which an object receives other objects that it depends on.
  • These other objects are called dependencies.
  • In the typical "using" relationship the receiving object is called a client and the passed (that is, "injected") object is called a service.

Wikipedia

React Example
NestJs Example

Exercise 2 - react branch by abstraction

  • Checkout branch lesson2/exercise-2-start
  • Change the code so instead of weather icon user will see emojis icons
  • use emoji-css.afeld.me for that
  • merge small changes by using branch by abstraction

Feature Toggles

Feature Toggles definition

A feature toggle (also feature flag) is a technique in software development that attempts to provide an alternative to maintaining multiple branches in source code (known as feature branches), such that a software feature can be tested even before it is completed and ready for release.

Wikipedia

Feature Toggles definition - cont

A feature toggle is used to hide, enable or disable the feature during runtime. For example, during the development process, a developer can enable the feature for testing and disable it for other users.

Wikipedia

Feature Toggles alternate definition

A "if" statement in the code that wrap the feature's code

Eylon Malin

Feature Toggles benefits

  • Merge “Half Baked” Features / small commits
  • Agility of Delivering Feature
  • Test in CI from First Day
  • Early Feedback
  • Experimental Features
  • Handling Vague Release Program

new Feature (Toggle) Life cycle

  • Instead of creating new feature branch create a new feature toggle
  • Set the toggle state to off
  • Merge the new toggle code to main branch
  • Develop your code in small commits and merge it to main branch

new Feature (Toggle) Life cycle - cont

  • Once the feature is ready enough, enable the toggle in CI / test env
  • Once the feature is completely ready and the code is running in production - enable the toggle in production
  • Once the clients are happy with the feature, remove the toggle from code and from management tool

new Feature (Toggle) Life cycle

Feature Toggles Frameworks

Exercise 3 - final exercise

  • In this exercise you will work as a team on multiple tasks
  • Make sure you are using small commits for better interaction
  • Choose one the team member fork so you would work on the same fork
  • Checkout branch lesson3/exercise-3-start

Exercise 3 - final exercise - favorites per user

  • Add support to save the favorites in some persistent storage (file/mongo). You might use branch by abstraction for that
  • Support login to the app so you can identify each user
  • By default show to the user the latest place he used (instead of his location)
  • Bonus - add favorites option to the user and to store and show his favorites places