When code turns into a horror story

Image by Aphinya Dechalert.

Titled: Monsters of our code.

When code turns into a horror storylessons learned from 5 incidents from the pastAphinya DechalertBlockedUnblockFollowFollowingMay 8We all have horror stories when it comes to code.

It starts off innocently until it turns morphs into Stephen King’s level of horror, turning the most optimistic among us into killer clowns.

While I can encourage you not to be that person, we still do it anyway or find ourselves knee deep into something we didn’t exactly sign up for.

Here are 5 chilling tales from my past and associated morals learned from the incidents.

Commenting like I know what’s goingThe story:Once upon a time, I encountered the code below in a real live, in production project.

//made a change here because Jay said soThe ensuing code made no sense as it used obscure x, y and zz values instead of actual names.

I knew who Jay was but he left the company three months before I chanced upon the code.

The moral:The point of code is to act as a communication tool between developers.

Machine’s ability understand the code is also part of the equation but humans are the ones that consume code in its current linguistic state.

Good code is said to be self containing and self describing.

But some times comments are required because it enriches the current code.

It expands on context if it is not clear due to constraints and abstractions.

It is a statement of fact that is not obscure when it comes to reading it.

The most logical pyramid of doomThe story:It started off innocently with an if else statement inside a function.

It didn’t take the developer behind the code long begin nesting inside of another if else statement.

The code then got passed around and another developer decided to contribute to the growing nest rather than refactor it out properly.

By the time I got hold of it, most of the left hand side had been indented to a point where the actual code lived on the side scroll.

It was bad but it made sense — 30 minutes of tracing and back tracking later.

The moral:Pyramids of Doom are called pyramids of doom because it’s built on unstable ground.

SOLID principles are ignored and are often the first signs of lazy coding.

When code begins to nest, one needs to address it right away — even if it’s not your code.

If you’re working on it, then you are responsible by association.

If you’re not quite sure how to refactor out deeply nested code or prevent yourself from writing one, here’s a quick and concise guide.

When real names matterThe story:During my second winter as a fully fledged developer, I encountered a strange variable called cusboolmemtra which was inside a function called tracalentNames like these were littered throughout with no comments or references to what they actually really meant.

A few days later and only after having lunch with the analyst did it start to make sense — they were abbreviations for parts of the database.

The moral:Write your code like how you would talk to a real person.

Call it what it is.

Not everyone has insider knowledge.

Good code is explicit in what it wants to achieve and what it represents.

We are supposed to be talking in the same language after all.

Code isn’t meant to be a secret language that only the initiated is allowed to understand.

Code is a communication tool so use it to communicate clearly and succinctly.

Make it as flexible as possible!The story:The boss didn’t know what he truly wanted.

Marketing didn’t know exactly what they wanted either.

In fact, no one knew what they wanted but we just had to start coding — because coding takes the most time.

So we started tapping away with all the possible outcomes in mind.

The idea of keeping everything as flexible as possible sounded great at first, until it came time to use the actual thing we created.

The code became too unfocused and eventually started to crack under the pressure to be as ‘flexible as possible’.

The moral:Flexibility isn’t a bad thing but code is a translation of rules.

When rules are unclear, code is often unclear — which leads to leakage and potential holes in the expected output.

When code becomes too ‘flexible’, it has no clear meaning or purpose.

Building a vague application will result in a vague outcome that doesn’t achieve anything much in particular.

Names become obscure, classes become open for all and result in security risks, potential bugs and breakages.

I’ll come back to it laterThe story:The boss told us that we’ll have time to come back to it later but deadlines needed to be met and products had to be pushed out.

It didn’t matter if the code was perfect or not, just that it was functional and that it worked.

Efficiency wasn’t part the equation.

3 years later, the application turned into a hack job that was built on promises of time that never materialized.

The moral:Don’t trust anyone that promises you time.

Treat the project like cleaning your house.

Always put things away and chuck the things you don’t need.

Refactor as you go so it speeds up your overall workflow rather than write shoddy code in hopes that you’ll get time to think it out at the end.

Because there is no end.

A business will always want to keep moving forward and as a developer, you also need to make it work for the code too.

Lets test everythingThe story:Someone got wind of test driven design and decided to make everyone write testing units.

Except the team didn’t really understand the concept of test driven design.

There was the testing part and the designing but the driven was missing.

Everyone wrote tests that didn’t really do anything in particular except for testing if inputs equated to outputs.

It was rather boring and did nothing to the overall workflow except take up more time.

The moral:TDD is more than just writing tests.

The core concept behind TDD is to test scenarios — that is, real life scenarios that are not rooted in a particular input or output.

Code’s purpose is to translate real life rules into ‘computer speak’ for machines.

It is these rules that one should be testing in TDD — not if a function will spit back a certain return.

Write tests that supports the overall business rules that you’re trying to implement — not the other way around.

Final wordsEvery developer who’s been in the field long enough has some sort of horror story.

Over the years, I found that approaching each tale with a calm and non-judgmental mind can help make or break relationships, along with reduce or increase resistance from the dev that wrote the code.

It takes collaboration, it takes time and it takes understanding to create a true masterpiece.

It also takes a good amount of emotional detachment from code to create anything that is truly solid in its grounding and construction.

Not everyone writes perfect code the first time round — nor is there such a thing as perfect code.

Just take a look at the code you wrote 5 years ago.

Chances are, they were horror stories for someone too.

So don’t be too hard on your juniors or those that wrote the code before you.

Take it in your strides.

Enjoy the horror and be the solution it needs to end the story on a happier note for the developer that follows after you.

Let’s stay connected and join my weekly awesome web digest newsletter list.

Thank you for reading.

❤Aphinya.

. More details

Leave a Reply