You Are Not Obligated to Reuse Code

Once upon a time, this JavaScript module thingy contained many large functions, nested in each other, sometimes with the exact same function names. A lack of function composition and the use of repeated function names is hard to read. Do not attempt to fix any code until it’s refactored into a readable form, or at least tried. This is not always possible in large projects.

Now, the code also attempted to restart a socket connection within the socket close event handler. This could result in recursive behaviour. If for some reason the client tries to open a socket, establishes a connection, but is then rejected and closed (for any reason) from the server, then it will immediately attempt to reconnect, ad nauseum. The initial code had oversights in other ways to the result that it could not execute the onclose handler, masking a problem. Existing bugs that mask other bugs is always a challenge to find and repair.

This code is largely derived from another project, with many changes. Code reuse is itself not a problem, but we are not obligated to reuse code always, this is a judgement call. With NO/OP blocks of code, shared codebases are not always easy to maintain. On one extreme, appropriating a whole project as a template for a different product is not generally good practice. This can generate forces that shoebox developers into trying to understand how the existing design should work, rather than building the new product with the philosophy of how it can work.

Probably almost every first or second year computer science or software engineering student is familiar with the Ariane 5 or Therac-25 disasters – software projects on a completely different scale. Regardless, there is a common line of narrative – never assume that just because the same code generated a set of results for one project that it will exhibit same behaviour for another.

Code reuse can be risky. Sure, we’re talking web development here, not rocket launches, but if we can’t learn from high profile mistakes of the past, then what can we learn from? “It’s only web development” is not an excuse for bad practices. Existing designs should only ever inform and inspire other ones, but not mandate design or architecture.

I would continue with this story but sadly I’m out of energy today pending recharge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.