PR Review: Code Has Cost, Justify It
Writing good code is also a matter of justifying why you wrote that code. In this post, we take a look at an example of how and why this is important. Are you ready to justify your code in your next code review?
Join the DZone community and get the full member experience.Join For Free
There is a reason why people talk about idiomatic code. Code that is idiomatic to the language matches what it expects and is generally faster/easier to work with for both developers and the compiler/runtime.
During a PR review, I ran into this code:
The idiomatic manner for writing this code would have been any of:
“@id” == property
Constants.Documents.Metadata.Id == property
I can argue that the second option is the most idiomatic and that the third option can fail with Null Reference Exception if the property is null, but all of them are pretty clear.
Now, RavenDB has a lot of nonidiomatic code, usually when we need to get more performance. For example:
This is code that is doing very much what is done above, but it does this on the raw byte buffer, and it knows that it is accessing UTF8 characters. So we can do some nice optimizations there to compare by just doing two instructions.
Indeed, when queried, the developer answered, "Most of the time, it's going to be false, and comparing ints is cheaper than strings."
There are several problems with this. First, this particular piece of code isn’t in a part of the code that is extremely performance sensitive. The string buffer work above is for processing requests from the network, a piece of code that can be called tens and hundreds of thousands of times per second. Performance there matters, a lot. This code is meant to be called as part of streaming results to the user, so it is likely to handle very large volume of data. Performance there matters, for sure, but we need to consider how much it matters.
Second, let's peek into what will actually happen if we drop the
property.Length check. The call will end up calling to the native string routines in the CLR, and the relevant portion is:
In other words, this check is already going to happen. We didn’t really save anything from making it.
Third, and the most subtle of them all. This check is using a check against a constant whose value is
@id. It also checks that the
property.Length is equal to 3. The whole point of using a constant is that we need to replace it in just one location. But in this case, we will likely change the constant value, not realize that there is a hardcoded length elsewhere in the code, and fail miserably with hard to explain behavior.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.