Ask HN: Codebase at my work is a complete mess, what should I do?
406 pointsAfter few days/weeks of getting to know the whole codebase I finally moved to a specific project I was suppose to work on and I found out the code is a complete mess.
Often I find parts of code that unintentionally affects other parts of code. Some parts are just copied and left there doing nothing. Even names of classes/variables are sometimes useless and project structure is unintuitive and seemingly without rules. It's spaghetti and relatively big spaghetti (tens of thousands of LOC).
I started "repairing" the project but there're no tests to check whether my adjustments are correct.
I'm so frustrated and depressed by it. The job basically turned into something I hate - I'm just rewriting the code!
The person in charge of this project was working on it alone and from the outside it all looks fine and it's working. So this makes the higher people think that it's all just fine.
I really don't know what to do.
Should I just go and basically say that this person did a bad job? I don't feel very comfortably doing that because (a) I'm a relatively new hire, (b) the person in charge is working there for about two years and (c) I'm much younger than she/he is both professionally and biologically.
I've already made some comments about rewriting it and the response was basically "ok".
Comments (333) 173 threads
Earlier commentsFirst, remember trust is normally earned, rather than assumed. There is a careful balancing act between getting to know the code base, the culture and the team vs being proactive from day 1. If they're completely lacking tests, write tests for the sections of code you're working on; the sections you fully understand. There's a few books on the subject, but they all boil down to don't try and tackle everything at once. Build an understanding and ensure new code is well tested.
Furthermore, make sure you understand the background. Is this a prototype that has been deployed to production? Is your colleague a "starter"; great at shipping new ideas, but stability and maintenance are not their strong points? Do they simply know no better? Why are things the way they are? The solution varies a lot.
Lastly, age, experience and knowledge are often confused in this industry; try to keep them separate. I manage a team of 6 and I'm the second youngest member (late twenties), but the most experienced in my product. It takes a mix of everything to make a good team.
I understand that you do not wish to offend your coworker who wrote poor code. That’s good, but it is also your responsibility to find a non-offensive way to communicate when their work is not ideal. I recommend using “code-first” language when talking about the code. For example, do not say “what you’re doing in this function is wrong”, say “this function is incorrect” (basically just never say “you” when discussing the code.) If they get defensive, remind them that your goal is code quality, and that you are not trying to be offensive. Be precise, informative, and most importantly: humble.
Don't refactor it right away. I'll take your word that the code is shit (frequently the case), but you don't know the idiosyncrasies well enough to know what to do with it.
After you've worked with the code a while, you'll be able to attempt a large refactor. Until then, gather low hanging fruit. Clean up confusing variable names. Add comments where needed. Write your new code as clearly and cleanly as possible. Use a tool like sonar to inspect your new code so that you don't introduce new problems.
Most importantly, ADD UNIT TESTS. That will make sure that your new code works with the old and it will allow you to attempt a larger refactor while being sure that you aren't breaking anything.
If necessary, you may need to add some integration tests as well.
I've been in your shoes. Bad code is no fun, but don't let it get you down. Focus on the happy parts of your job (team, culture, whatever). Good luck!
All comments shown.
Couldn't load more comments. Check your connection and try again.