Lurker News
Recently I joined a relatively small company (50-100 employees) as a medium level software developer. For a while I was very excited about this opportunity.

After 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

Show all comments

Showing thread 134 of 173

yebyen#
I'm going to embarrass my friend and plug his leanpub book, Scary Large Code Surgery with Pivots, because it was so similar to your story, except he wasn't new in his career.

Step 1 of rewriting a code base that doesn't have any tests: commit to not rewriting anything until you've covered it with tests and understand all the moving parts well enough to be sure you know what you've broken.
Chorn asked me to make sure that nobody pays for this book, so I (...won't provide the link... edit: free link below), it's on Leanpub. The idea of a "pivot" is to invent a mechanism that lets you treat each line of code the same. You need to have a process that you can use to catalog, understand, and test everything objectively and without getting hung up. If you removed any arbitrary line of code, or changed it to call a different method, that should cause a test to fail. The most important part (and it's been a while since I read the book, but...) commit that you are not going to fix anything until you have tested everything.
It will be grueling. The net effect of this hell should be that you will never question, or allow anyone to question, the value of automated testing at build-time ever again. The tests are what allow you to refactor with confidence.
If the management is on board with this strategy, then you have already won half the battle.
They may already know they did a bad thing, but your outlook on a piece of code is different before it has been made successful and people are using it. "What am I gonna need that test for? That shit is a waste of my time, I don't even know if this is gonna work in the first place." That might not even have been wrong at the time. There are plenty of ways that people can convince themselves not to write tests. Some are external pressures. If you build a great piece of code and are late to market, beat there by your competitors, that could be an even more serious problem. If the code is poorly designed, but it is working and people are using it, now you have a different problem.
The main idea of the book is that when your code has become successful and is in use by hundreds of people, the game changes. You can't responsibly make major changes to code you don't understand fully. Take the time, that's the job! And good luck.
Edit: I found the coupon link, coupon price $0.00
https://leanpub.com/scary-large-code-surgery/c/ohai