Document that code!

Document that code!

Q: Show me a piece of code you're proud of ...

A: Here's a 10 line comment describing a single constant declaration :)

No alt text provided for this image

When we write code, we often tend not to properly document the core insight / intention /intuition behind optimizations. When another person (or for that matter the programmer themselves) revisits the code a while later, it seems like a non-sequitur.

The reason I'm proud of this bit of code is precisely because of that. This was an optimization for a "GeoPoint contains check for a circle with a (lat, lon) center and a radius in meters" logic coded into a geospatial index system I wrote at FB (which powers Maps/Reverse Geocoding/Local Search/Marketplace/Nearby Friends etc.).

No alt text provided for this image
Fig 1. The inscribed/superscribed square approximation for a GeoPoint + Radius Circle


The core insight was that instead of doing an expensive great circle / haversine distance check for each candidate, we can accept everything inside the inscribed square of the circle (which is a much cheaper Box check) and discard everything outside the superscribed square (again the cheap Box check).

No alt text provided for this image
Fig 2. The Haversine Distance formula for overland / great circle distance between two GeoPoints (Source: Wikipedia)


This leaves us a much reduced set of candidates in between the two squares and we can do the more expensive great circle / haversine check for only those points. For a service doing billions of queries per second, this small optimization helped save a ton of computing.

I took this screenshot a year or so after writing that code, when I was revisiting that module for a feature addition. I patted the one year ago me on the back for thinking about the older / more crankier me and documenting the logic in detail. This made understanding the core intuition in the code that much easier.

That's why I cherish the comment I wrote more than the actual optimization (which was pretty nifty in its own right)!

#programminglife #geospatial #code #c++ #comment


PS: This post would be amiss without a call out to my great "co-pilots" on the Geospatial Index project. We made a great team :) #Loki4Ever Kevin Ventullo / Tina Park / Vladimir / Shannon T. / Lihan Bin

Nishchay Sinha

Software Engineering at Bloomberg LP

2 年

What's Yo Dawg-ing. Very neat!

回复

要查看或添加评论,请登录

Saurav Mohapatra的更多文章

  • Working with Ambiguity (Step 1: The Three Bricklayers)

    Working with Ambiguity (Step 1: The Three Bricklayers)

    Over the last ten years or so, one of the most frequent topics on which mentees seek advice or guidance on is…

    3 条评论
  • The Myth of the Master Molecule

    The Myth of the Master Molecule

    Previous Post: On Mentoring We love heroics! Something in the romantic image of a singular achievement trumping…

    1 条评论
  • On Mentoring

    On Mentoring

    Next: "The Myth of the Master Molecule" “Learning is finding out what you already know. Doing is demonstrating that you…

    1 条评论

社区洞察

其他会员也浏览了