BT

InfoQ Homepage News Uber Open-Sources Tool to Automatically Clean Up Stale Code

Uber Open-Sources Tool to Automatically Clean Up Stale Code

This item in japanese

Bookmarks

Uber has open-sourced Piranha, their tool for automated clean up of stale code caused by feature flags that are no longer required. Piranha can be run within a pipeline to continually look for stale code to be removed. Currently Piranha supports Java, Swift, and Objective-C.

Using static analysis, Piranha performs three key tasks: deleting the code immediately surrounding the feature flag API, deleting the code that becomes unreachable as a result of the previous step, and finally deleting any tests related to the flag. To execute Piranha, three inputs are required: the flag under consideration, the treatment behavior, and the owner of the flag.

For example, consider the following Java snippet containing a feature flag:

public class MyClass {
  private XPTest expt;
  ...
  public void foo() {
    if(expt.flagEnabled(TestExperimentName.SAMPLE_STALE_FLAG)) {
        System.out.println("Hello World");
    }
  }

  public void bar() {
    if(expt.flagDisabled(TestExperimentName.SAMPLE_STALE_FLAG)) {
        System.out.println("Hi World");
    }
  }
}

Calling Piranha with the following arguments will cause it to leave the treatment condition (where the feature is enabled) and remove the control condition (where the feature is not enabled):

options.errorprone.errorproneArgs << "-XepOpt:Piranha:FlagName=SAMPLE_STALE_FLAG"
options.errorprone.errorproneArgs << "-XepOpt:Piranha:IsTreated=true"
options.errorprone.errorproneArgs << "-XepOpt:Piranha:Config=config/piranha.properties"

This will prepare a diff to change the code as follows:

public class MyClass {
  private XPTest expt;
  ...
  public void foo() {
     System.out.println("Hello World");
  }


  public void bar() {
  }
}

When creating this tool, the Uber engineers observed three types of flag APIs: Boolean APIs, update APIs, and parameter APIs. Boolean APIs are ones that return a Boolean value as in the example above. Update APIs update the feature flag value in the running system. Finally parameter APIs return a non-Boolean primitive value to correspond to an experimental value controlled from the back end. Piranha only handles the first two types "as the engineering effort required to address them was much larger and the frequency of their occurrence in the codebase much lower".

Relying on teams to proactively run Piranha may still result in stale code not being cleaned on a regular basis. The team created a workflow pipeline to run Piranha on a regular cadence. In Uber's case Piranha runs weekly on their codebase. The pipeline also automatically generates tasks within Uber's task management system for tracking the created pull requests.

Piranha workflow to automatically generate a list of stale flags for review

Piranha workflow to automatically generate a list of stale flags for review (Source: Uber)

 

The Piranha pipeline queries their flag management system for a list of stale flags. Each team has the ability to set their own time frame for what stale means. Once triggered, Piranha will generate a pull request and assign it to the flag's original author. To ensure the work is handled, the team also created PiranhaTidy, a reminder bot, to periodically notify users about open Piranha related tasks.

Piranha is open source under the Apache 2.0 license. It currently supports Java, Swift, and Objective-C. There are open issues to add support for additional languages such as Kotlin, .NET, and JavaScript. The team will have a research paper on Piranha in the upcoming 42nd International Conference of Software Engineering.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.