Tuesday, March 20, 2012

Don't play Jenga® with the code (Robert's Rule #20)

[Tweeted 2011-06-17]

Often code is developed in concert between many developers. In times like this we all hope that the code is well-documented and is written in a style that is easily readable. Unfortunately, this is not always the case, but even when it is the case there is still a danger because seldom is production code so clean or simple that every developer understands all aspects of it. A good example of this is code for web applications, especially front-end code in JavaScript.

JavaScript is deceptive for a number of reasons. First, it's similar enough to Java that engineers who work in both languages will sometimes get confused. Second, it's strengths (or weaknesses, depending on your perspective) allow poor writing. Third, while it's possible to simulate multi-threaded programming, there isn't anything that's considered thread-safe. This last point has, in my experience, been the most challenging because it has the potential to create race conditions...on steroids.

Because of its deceptive nature, to say that debugging front-end web code can be a challenge is a bit of an understatement. One approach, often used with linear programming, is to comment out code while stepping through it to see where the defect lies. Unfortunately, this plays right into the language's deception by creating the equivalent of a red herring, and with it the potential for disaster.

In those cases, it is always best to not only keep exact notes, including a time stamp, when attempting to reproduce the defect. After completing several tests, search the code to find the handlers for the actions taken while simultaneously examining the logic within the event flows. Finally, make sure that at least one person on your team is intimately familiar with each codebase so they can act as a resource. It is only by pursuing a course of action similar to this that you follow Robert's Rule #20, don't play Jenga® with the code by pulling out pieces that you believe to be the problem only to have the application crash to the ground.

No comments:

Post a Comment