DZone
Agile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Agile Zone > PR Review: Code Has Cost, Justify It

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?

Oren Eini user avatar by
Oren Eini
·
Oct. 23, 17 · Agile Zone · Opinion
Like (3)
Save
Tweet
5.04K Views

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:

image

The idiomatic manner for writing this code would have been any of:

  • “@id” == property
  • Constants.Documents.Metadata.Id == property 
  • property.Equals(Constants.Documents.Metadata.Id) 
  • Constants.Documents.Metadata.Id.Equals(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:

image

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:

image

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.

IT

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is URL Rewriting? | Java Servlets
  • The Evolution of Configuration Management: IaC vs. GitOps
  • How to Optimize MySQL Queries for Speed and Performance
  • Role of Development Team in an Agile Environment

Comments

Agile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo