How do you cope with bad code?

twitter logo github logo ・1 min read

I don't really know how to put it without sounding like a crybaby but let's say I currently need to work on some really bad code (both in quality and structure) and I don't have to time (deadline) to fix/change/refactor anything. I end up implementing some things by writing really dirty code (because it needs to be adapted to the already existing code) and I absolutely hate it and feel bad about it.

Anyway, not asking for a solution or anything, I know I'll just have to make do, it's part of the job, but for those of you who are/were in a similar situation: how do you cope with this? How do you stop caring too much about quality and just do what must be done?

twitter logo DISCUSS (6)
markdown guide
 

Bad code is like a piece of text, with vulgar language, wrong grammar and having chapters, paragraphs and sentences ordered in the wrong way. Trying to get the message across from the text is really hard to do this way.

Saying the text contains the information to get the message across, so it's "working text" is almost the same thing as bad code.

Explaining the state of the code to the stakeholder in this metaphor should be possible quite well.

If they would get a compendium or a series of books full of vulgar language, wrong grammar and the chapters, paragraphs and sentences in the wrong way, would they accept it? How much time would they allow you to improve it?

Would you only get the time to add/remove sentences without improving the readability? The information is in there, but are they willing to read the book(s)?

The simple answer is: NO. Why should you be put up with this problem, while you are not the one responsible for the cause of it?

You should not be forced to feel this pain all by yourself. Try to make a plan to improve this and put across that they can not expect you to do the impossible.

I'm from the Netherlands, near Amsterdam. Nobody can ask me to get to New York, in two hours, with just a swimming short and $100. Don't even start, because it doesn't make sense to waste time in that situation. You didn't ask for the bad state of the project.

If you can not find support to improve the situation, let them eat their own poo.

 

The code we read is not the code that was written.

Someone probably didn't decide to write the code you are working on now. It's probably years of modifications of people making the best decision they could with the context and information they had at the time. So I remember that everyone did the best they could with what they are given, just like I am now.

Secondly, I make whatever improvements I can along the way. Even if its leaving comments to help me understand what is going on. Refactoring in small bites can help you move faster, so the argument about having a deadline is moot.

Thirdly, maybe it doesn't matter. If the code works and is providing business value, then code in kind and ship it. Everyone will happier with a working pile of spaghetti over a nicely manicured and late submission.

 

Thanks for the reply.

I'd usually agree with what you say but here the code isn't "bad" because it's the result of years (it's a few months old) of modifications... but because it was done by someone who isn't a developer. So yeah, it's just bad code. And to be clear I'm not blaming anyone, all things considered the code runs well, but it is absolutely unmaintainable, which is my main issue with it... because I do have to maintain and extend it.

Also sorry but no, my argument about having a deadline isn't "moot". The client basically gave me a week to do something they weren't able to do in over a month... so respecting the deadline is almost impossible even by barely doing the minimum, let alone refactoring any part of the code.

I don't know, maybe I'm at fault here, maybe I should be able to work with such a code, and it's not the first time I have to work with legacy code, but in this case I really can't get anything of it.

 

I do what I can to get the job done as fast as I can and spend any remaining time refactoring what I can.

And throughout the whole time complain to my lead dev so he feels my pain as well.

 

I remember reading this line a while ago (not ad-verbatim):
Legacy code may be offensive to you, but remember that it's working code. Business always favors working over pretty or elegant. What you need to do is refactor only code that depends on your current task, do not go on a crusade to reshape the whole codebase.

I follows this advice to heart. I refactor only what I'm working on (taking care of not breaking anything).

 
Classic DEV Post from Feb 5

Clean up your code with these tips!

Learning new patterns to make your code smaller, and more clear.

Quentin Sonrel profile image
Working as a consultant in full stack web development. Also : Gamer & aspiring streamer / Cinema & music lover / Amateur cook / Beer & tea connoisseur

Customize DEV by logging in

  • Follow users
  • Follow tags
  • Dark mode
  • Font style (like sans serif)
  • Notifications
Get Started