42.1 F
Columbus
Tuesday, January 26, 2021

The story of the one line fix

This post was originally published on this site

Picture yourself, an engineer working at the hottest distributed microservices de jour, assigned to fix a bug. You jump into an unfamiliar codebase and quickly locate the line where the problem occurred. The fix is simple, just return early or substitute a default value in the case that one cannot be determined from your input. Boom, problem solved. Code compiles, fix goes out to production, and the card’s in the done pile in time for Tuesday drinks at the pub.

Now consider this alternative scenario. You find the problematic line and, before you make the fix, you decide to add a test so you will know that your fix worked, and hopefully someone will not accidentally revert it in the future.

You’ve figured out that some weird edge case causes that line to be called without being able to determine the right value for y. You can see it clearly, y ends up being zero so the program crashes. To write a test, you need to get the conditions that caused y to be zero to occur on command. There are only two parameters passed into this function, this shouldn’t be hard. Oh, but this is a method, not a function, so you need to construct an instance of the object in the right state to trigger the bug. Hmm, this language uses constructors to make sure people can’t just monkey up the bits of the object they need for a test, you’ll have to find, mock, stub or build instances of all the objects dependencies (and their dependencies, and so on) then run the object through the precise series of operations to put the it in the state that causes y to be empty. Then you can write the test.

At this point Tuesday night drinks are fading before you eyes. Without being able to write a test for your fix, how will you convince a reviewer that it actually fixed what it fixed? All you can prove was your “simple” change didn’t break any behaviour that was covered by existing tests. 

Fast forward to Friday and your one line change has finally been merged, along with a few new test cases to make sure it doesn’t happen again. Next week you’re going to sit down and try to figure out a bunch of weird crashes you caused while trying to stub out calls to the database. Eventually you gave up and copied some code from another test that uses a copy of production data. It’s much slower than it needs to be, but it worked, and was the only way to put a reasonable estimate on a job that had already consumed your entire week.

The moral of the story; was the test the reason it took nearly a week to make a one line fix?

Related Articles

Announcing the launch of our Slack V.3 integration

In 2018, we released an integration between Stack Overflow for Teams and Slack. It’s now one of the most popular ways for users to...

IntelliJ IDEA 2020.3.2 Is Available

Our second bug-fix release for IntelliJ IDEA 2020.3.2 is out! You can upgrade to v2020.3.2 from the IDE, with the Toolbox App, or by...

Answering Your Questions: AMA with the .NET Team on Reddit

Last week, we held our first Ask Me Anything (AMA) session on Reddit. We’d like to thank everyone who participated, and we hope you...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected

21,422FansLike
0FollowersFollow
0SubscribersSubscribe
- Advertisement -

Latest Articles

Announcing the launch of our Slack V.3 integration

In 2018, we released an integration between Stack Overflow for Teams and Slack. It’s now one of the most popular ways for users to...

IntelliJ IDEA 2020.3.2 Is Available

Our second bug-fix release for IntelliJ IDEA 2020.3.2 is out! You can upgrade to v2020.3.2 from the IDE, with the Toolbox App, or by...

Answering Your Questions: AMA with the .NET Team on Reddit

Last week, we held our first Ask Me Anything (AMA) session on Reddit. We’d like to thank everyone who participated, and we hope you...

WebStorm 2020.3.2 Is Available

WebStorm 2020.3.2, the second bug-fix update for WebStorm 2020.3, is now available! You can update to it using the Toolbox App or right from the...

Video: Refactoring Code With PhpStorm

Christoph Rumpel shows PhpStorm’s refactoring features. He goes through 6 refactorings to improve code readability and maintainability. https://youtube.com/watch?v=LamxHRmwK2Q PhpStorm tips with Christoph Rumpel is a video...