This is something that I come across fairly often, and there are a couple ways to proceed, but never know which is the best way. I usually pick one way at random. I'm looking for a framework that I can apply each time I am faced with this problem.
The problem is this:
I am working on a bug. In this case, the bug is due to assuming user input is in a certain format, but it can come as a different format. We come up with the solution to normalize input to the expected format.
I find a function which does exactly this. Its description explicitly says that it normalizes input to our desired format. However it looks like it has a bug and the output value is invalid. This function is used in quite a few places in our application. I don't see how this could be working correctly everywhere it is used. I think because the bug is "invisible". It doesn't produce a fatal error and only causes a hard-to-detect visual bug.
Another kicker is that this function has test coverage and it explicitly expects the invalid value.
For a practical example:
Let's say the function normalizes a CSS color value to hex color values, but neglects to prepend the #
sign in front. Maybe something like this:
function normalizeCssColor(color) {
// ... logic here which parses the value and gets the rgb values in hex
// note it doesn't prepend the color with #
return red + green + blue
}
Note this is just a trivial example, the real code is slightly more complex.
At this point I have a couple options:
- Directly update the method, then test everywhere it is used. This isn't really possible because the bug ticket wasn't estimated to take all this testing into account.
- Add a boolean flag to the existing function, which defaults to false, and pass true in the place where I am going to call the function. Like this:
With this approach, we can also investigate later and see if this boolean is needed, and make prepending thefunction normalizeCssColor(color, bool addHash = false) { // ... logic here which parses the value and gets the rgb values in hex if (addHash) { return '#' + red + green + blue } return red + green + blue }
#
default behavior. - Just create a separate function which does what I need (i.e.
return '#' + red + green + blue
). And then create a bug ticket to investigate if the other places where the old function is used are truly broken and if the function needs to be updated to call my new function.
This isn't really possible because the bug ticket wasn't estimated to take all this testing into account.
Sometimes (most times?) estimates are wrong...