REMOVE BEFORE FLIGHT in Programming
Have you ever seen those red ribbons installed on parking airplanes and written Remove Before Flight? The ground crew uses it to protect those important components from unintentionally operations, bugs, sand or something else, but it could lead to an emergency landing or even a crash if they forget to remove them before flight. However, is RBF useful for computer programming?
Back to our routine work as a software developer, there are two major scenarios everybody faces every day, programming and debugging. Both of them need numerous fake data and logs. I would like to share my practice to avoid pushing such source code to your version control system.
Fake Data
Assume we need to implement a function which fetches news feed from server every 2 hours. Actually, nobody will wait for that long each time to test frontend implementation, we could just short the interval time to 3 seconds or even shorter like the following:
class Demo {
const int newsInterval = 3_000;
Timer timer = null;
public void fetchNewsFeed() {
if (timer !== null)
return;
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Fetch news feed
}
}, 0, this.newsInterval);
}
}
We know such short interval seconds cannot be published, but what if we forget to change it back? The colleague who’s responsible for reviewing our code may not be aware of this is a bug, he/she would just think it’s designed by demand. After the version is released, we have to publish a patch to fix this stupid issue and be criticized by our supervisor, bonus impacted as a possibility as well.
Here’s what I usually do, keep the real data, and indicate which is a fake one:
class Demo {
const int newsInterval = 3_000; // TODO: Remove Before Flight!
// const int newsInterval = 2 * 60 * 60 * 1000;
Timer timer = null;
public void fetchNewsFeed() {
// Refer to the previous sample code.
}
}
As the real variable is beneath the fake one directly, I suppose you could recall how to deal with both immediately.
Log
While debugging, we use a logger as a probe, especially for asynchronous functions. The more steps we need to monitor, the more logs it will print. Even though some compilers could turn log off under production mode, we still shall avoid committing those log printings which are for some special and temporary purpose, because it would have a bad affect on other collaborators.
Imagine our current task is fixing the bug with number 9371. I’ll show you what I do for this scenario:
class DemoA {
public static final String TAG_BUG_9371 = "Bug #9371"; // TODO: Remove Before Flight!
private final Logger logger = Logger.getLogger(DemoA.class.getName());
public void init() {
this.logger.log(Level.DEBUG, TAG_BUG_9371, "This is a test in init().");
}
}
class DemoB {
private final Logger logger = Logger.getLogger(DemoB.class.getName());
public int getNumber() {
this.logger.log(Level.DEBUG, DemoA.TAG_BUG_9371, "This is a test in getNumber().");
return 1;
}
public void setNumber(int number) {
this.logger.log(Level.DEBUG, DemoA.TAG_BUG_9371, "This is a test in setNumber().");
}
}
You might print logs in different functions and classes, with the recognizble tag name, you can easily filter relevant logs from the output console. Once you delete the declaration of TAG_BUG_9371, every related log() invocation will turn to a compilation error, remove all of them before your commit. And we need only one RBF comment by this approach.
Tips
- I highly recommend you to consider this RBF as a TODO comment if your IDE is able to list TODOs in a specific place;
- Ask your teammates to reject any of your check-ins which contain uncommented source code followed by RBF. Append a customized rule to the commit hook would be way better, but this depends on your lint tool;
- Customize code snippet settings in your IDE or editor (if it supports) to simplify input of RBF, for instance, you can configure VS Code at Settings (Perferences in Windows edition) → Configure User Snippets, choose a language like Java:
{
"Remove Before Flight": {
"prefix": "rbf",
"body": ["// TODO: Remove Before Flight!"],
"description": "Remove before flight."
}
}
I hope this could inspire you in programming, and warmly welcome any other useful and efficient tricks.