406 points · by anonR4nd0m · 8y ago · 333 comments
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".
My advice is don't complain and don't attempt any significant rewrites for the first 6 months or so. Your job at first is simply to understand the code and demonstrate that you are able make improvements to it without breaking everything. Doing so will win you the trust and respect of your teammates, whereas if you come right out of the gate trying to rewrite everything there is a very good chance you will make yourself look like an ass by spending a lot of time and energy introducing bugs into a previously-stable area of the code. Once you have a decent understanding of how and why the code is the way it is, and the trust of your teammates, then you can invest effort into fixing the highest-priority issues with the code.
Being able to read and work effectively in legacy code is an essential skill for any software engineer. For now you should just try to focus on fixing bugs and implementing new features, and take pleasure in the fact that you are improving the project and developing a handful of hard and soft professional skills.
That being said - there should be a limit to how much bullshit you put up with. If after six months to a year you still find that you hate the job, and a better opportunity comes along, then go ahead and take it.
This is key advise. It makes the difference between a person who enjoys the art of coding and a professional who get paid for work. Basically you have to first proof why the invest of rewriting brings any financial benefit. Till then, be professional and maintain it.
I mean, you are offering good advice for a person that wants to fit in as his highest priority, no doubt about that. But there are people (like me) who don't enjoy working deep down in complicated shitty code to the degree that I don't even want to be a programmer in such environments.
There are ways to mitigate this. Join companies that care about clean code and micro services. Work in the cloud. Avoid enterprise. Those advices can be hugely important for someone who just hates those kind of gigs where you feel like you don't want to be there even.
Any codebase that sees real usage will accumulate cruft, odd behavior and legacy code over time. Dealing with that and assessing what’s worth refactoring and what’s best cut or even just left as it is to reach a business goal is the skill that differentiates a seasoned engineer from a code monkey. That shitty legacy code is often what brings in the money to sustain the company, otherwise it would have been turned off long ago.
Microservices don’t help much in that regard: each micro service can look squeaky clean from a code perspective and the whole system can still behave in shitty, unpredictable ways. (Been there, seen that)
It’s also not about fitting in: taking on a shitty codebase and transforming it into something better is an outstanding skill and requires constant communication with all stakeholders, especially when you want to cut legacy stuff. It’s certainly not something for people that prefer a quiet corner.
How is this even related? Worst code I've seen was in financial sector, but the project is so huge that is no chance of upgrading it without complete rewrite, 65% of code when I left it was still in Java5 with no option to upgrade to 7 any time soon, it's just too big, so we can understand why it's shit. Second worst code was in a startup, micro-service oriented code, k8s, dockers, javaEE, angularJS and angularIO + TypeScript. There was literally shit, HTML injection in chat-box, >200 JSON endpoints with something that mimics REST (it was closer to CURD than to REST), SQL injection using JSON objects was possible, deployment of new version of a microservice usually took 4-5hours, only if any of model classes haven't been changed. Now I work on medical software, the only split we have is frontend has own repo, backend has own repo, two monoliths. Best code I've ever seen, perfect organisation, good class and field names, easy navigation. I see completely no relation between code quality and microservices/monoliths, what I see it that sane people write good code.
Sure, enterprise is slower with moving with technologies, but that's because they're bigger and decision on upgrading Tomcat to the latest version might cost thousands monthly if there is a single performance regression. It doesn't mean that code is shit because they still use jQuery instead ReactJS.
>Work in the cloud
How is that supposed to help? Moderns devs already have to know JSON, XML, XSD, 2 IDEs, 10 testing framework, at least 2 frontend frameworks and languages, at least 2 backend frameworks and languages, so here, have this k8s YAML configuration and this docker-compose and Dockerfile so you can... avoid shitty code? Also, have this microservice oriented stack, learn how to do distributed logging, autoscaling, self-healing architecture, deploy chaos monkeys in your cluster.
I agree that work should produce happiness. And I agree there are places where the work is not everyone's fit (e.g. bureaucratic customizations). But being not able to cope with bad code is a no go in my opinion.
That said, any codebase that has seen a few years of maintenance and further development is going to look complicated, especially if you're too young and inexperienced to recognize that each of the annoying edge cases was put there to fix real production issues. Rewriting sounds like a good fix, but it will take a very long time and in the best case you'll end up with a newer codebase that looks just as complicated to a person just entering the business as the one you were attempting to replace.
Also I want to add that allocating time for a refactoring or fixing legacy code is hard to justify most of the time. Instead I try to follow the "boy scout rule":
On every bug fix or new feature you work on "Leave Things BETTER than you found them". An small change like properly naming a class or a variable, or just writing a readme file explaining something will help you and your team in the future.
https://medium.com/@biratkirat/step-8-the-boy-scout-rule-rob...
I would agree with this, you need to be careful. I've seen a friend start at a new company with a horrendous code base, bring up the issues and be let go because they hurt / offended the existing developers.
SOme people are really precious about their shitty code and can't take criticism, esepcially in small companies.
So get through your probation period then you can maybe start making noises.
It's very easy to criticize, often not easy to understand the situation where the bad code was written. Maybe the hastily-written code was the thing that saved a company from bankruptcy and was written by dedicated engineers, and bringing your friend to fix it was the plan all along, and what they needed was someone to fix the code and not decry everybody as stupid.
[I'm not saying this is what actually happened to your friend. I don't know your friend :)]
A lot of people seem to think that criticism of the code is the same as criticism of the person who wrote the code. They are not the same thing. Root knows I've written plenty of crappy code over the years for very valid reasons. You are not your code, saying the code sucks doesn't mean I think you suck.
I'm not sure if I agree. As a programmer I feel my primary responsibility is towards the code, not the company, not my colleagues feelings, not the customer. There are other roles who speak for them (sales, product owner, managers, etc.), I speak for the code.
When you get into the office in the morning, you hang both your coat and your ego on the coat rack, you can pick them back up when you leave. If someone can improve the code by tossing away 3 years of my work, then by all means. Doesn't matter if the person who suggested it is a senior with 20 years experience or the fresh out of college guy/girl who started just last week. You judge the idea on it's own merits, not by who suggested it.
I really don't get the tendency of allistic people to involve emotions in parts of their life where it's not helpful at all.
If someone approaches things politely and professionally then no one should have a problem with constructive suggestions for improvement. But people sometimes approach things in a combative or disrespectful manner and that's when you run into problems.
But how do you objectively measure "improvement of the code"?
Maybe it's something fairly obvious like better performance, less bugs, etc, but a lot of times it's very hard to tell if there was actually an improvement or just a different way of doing the same thing, except at the added cost of re-implementing those 3 years of work.
And for the record, I agree with you in that you (we) should leave your ego at the door, and if some young whippersnapper can do better than you, then you should be able to leverage that and increase the productivity/performance of the team/company.
However I've seen it happen many times where someone thinks they will improve the code but in reality (like others have said) they don't have the correct context and forget about a lot of stuff that their "improvement" should take into account but doesn't... and then once they do the "aha!" moment kicks in and they realize why it wasn't as easy to improve as they first thought.
>I really don't get the tendency of allistic people to involve emotions in parts of their life where it's not helpful at all.
I complete agree
You're absolutely right: if someone has a good idea for improvement, it should be adopted regardless of who they are.
The difficulty is that the suggestion of a total rewrite is not-infrequently prompted by a mostly-emotional reaction. It may not be taking all the important objective factors into account, so you have to be careful to assess whether it truly is a good idea.
The directors and the team leader appreciate the honesty and my initiative to make the codebase healthier.
Like Ray Dalio says, 'don't tolerate shit'.
P.D: I rewrote a huge section of the application on Vue2 with SFC's. The other three developers who wrote backend mostly, tried to do the FrontEnd, but made a complete mess.
If it's really a rewrite job, you'll end up with a lot of entries in there of varying size and complexity. You can start moving those issues across to whatever issue tracker your boss does keep an eye on as you get to them.
That way, you're not just walking in, announcing that the entire lot is garbage, and wasting time rewriting it all (which is what the business will think you're doing if the current implementation is at least vaguely functional), but you're gaining familiarity with the code and compiling a decent sized todo list that you can present at the appropriate time, as well as a list of fairly quick wins that you can get done when you have some downtime.
In my mind, unit tests is not what you want to start with with a messy legacy codebase. It takes a huge amount of effort testing lots of already working code. Add unit tests for any new code, but try and implement tests for the entire system, functional/integration tests are also easier to argue for in terms for direct benefit for the customer. Unit tests are more for the developer and is harder to argue for.
Adding tests makes it easier to confidently refactor the code later on.
Start with areas that are the most valuable to the business. Not only does this more easily show the (business) value of testing, but it helps keep you from introducing a very visible and very bad looking regression. Anything dealing with money (especially billing) is probably a good indicator. Be warned, here be dragons.
That being said, don't verify your testing setup with business critical tests. Verify your setup (to yourself and other devs) with the quickest thing you can accomplish. The business critical ones are your (team's) short term goal.
Pick out the X most important logic paths (e.g. customer checkout or some subsection of that). Work on regression testing that over the next few months.
Make sure your tests are both automated and actually get run. The latter is more important than the former.
Ideally, your tests will prevent a nasty bug from going in and will immediately prove the business value of your testing efforts. This will establish the trust you need to continue testing and refactoring.
The first step is to stop the bleeding so begin adding unit tests for any new code you write and be sure to factor this effort in with your sprint estimations.
In parallel work top down with high level integration tests written against feature flows. E.g., As a user when I select this then I expect this. This doesn't mean you need to actually use BDD gherkin library for your test runner but at least frame the context of these in that regard since you don't care about the minute details of the underlying code, just the experience of the user story.
And lastly, don't just write tests to write tests, try to understand what is important to cover against because there is nothing worse than maintaining barely useful tests wired up to legacy data fixtures.
Without that, IMO trying to internalize large amounts of badly written code can negatively affect your brain's ability to come up with good, well written code. All those bad ideas and patterns become the first things that pop into your mind when you try and solve problems.
“When you stare into the void, the void stares back into you” sort of thing...
I don't usually change the details of nodes that I already worked on, but rather write what has changed in a new node with the same name but under a later date node. I find that it becomes a mess if I try and keep every node updated to what I currently think about it, because too much information builds up to efficiently do so. Also, a lot of what I write only make sense in the context of what I wrote on other subjects during the same time period, so if I change one, I end up having to rewrite more and more, which eventually becomes a big mess.
The main benefit is that I can go through my notes quickly, skipping minutia of topics I'm not interested in simply by not reading the details of a node. I used to use a text file, and that was a very big problem, because not only would the amount of text that I'd have to go through became daunting. Also, I sometimes found it hard to interpret what I wrote, which means I wouldn't know if it applied to the problem I was working on or not.
However, in contrast to the above, I do try and make a definitive structure if it's a small topic or a quick idea for a new project or something simple like vacation plans.
BTW, it also works very well for taking notes for a class or self-study.
Edit: "you" = OP
Plus, to my mind, there's no better way to start understanding a new codebase than to start with tests, either writing them or reading the existing suite.
Even if I didnt do anything, the code keeps breaking!
Ok, more seriously: that’s the way existing code is about 90% of the time - it’s the reality of working in our field.
It may or may not reflect on the original developer. Maybe they were learning a new framework. Maybe they were rushed. Maybe it was originally intended as a prototype but ended up in production. Maybe the requirements have grown or changed significantly since it was implemented and the original design didn’t scale well (this is to blame for most of the awful code where I work). Maybe they’re just not very good.
If you’re going to take this to management, I would not bad-mouth the original developer. I would suggest framing the issue as the code not scaling well to the new requirements.
Then give them a few options with trade-offs. It could be that management is willing to spend the next few years playing but whack-a-mole to avoid an extra couple weeks on the project to put it under test and refactor a bit. It could be they think the fix up is worthwhile now if it’ll save them a month’s worth of development time in the next few years, but not if it’ll only save a couple days. It could be that they intend to rewrite the entire area of the product in 6 months and only want to invest the minimum effort in changes until then.
This. If there are deficiencies in the code base, focus on those, their effects, and the gains from fixing them. Blaming people is rarely a good idea, for many reasons.
If the original dev worked on the code base for a long time, they're probably already aware of them. God knows I'm all too aware of the deficiencies in code I wrote years ago, but that's frequently how things look after years of updates and feature creep. Everyone on our team has code like that, and when new guys come in and point them out, we just sigh and say "yeah it would be great to rewrite that knowing what we know today". But budgeting time for that is not easy.
Edit: Also, like others said, I think you need to adjust your expectations. Working with code like that is life, and you should appreciate the fact that you, with your outside perspective and youthful energy can make a change for the better. But it's going to involve lots of refactoring and test writing.
The "Legacy Code" book referenced in another post is a great overview of this. I'd say of all programming books I've read, it's one of the ones that are most relevant to my day to day work.
Adding on, the original developer is almost certainly aware of definciencies in the code. My guess is that they are largely a result of rushed deadline, changing scope, and feature creep than they are the result of bad engineering. When you need to get a new build out at 11pm because you have an angry client on the phone, it can be tempting to say “I know this is bad practice, but I’m just throwing this in the controller to get it working for now. I’ll fix it in the morning.” Especially at a small company without solid engineering practices, this happens enough times and you’ve eventually got an application with no meaningful organization or architecture.
Having been in charge of an app that started to look like this, i know that I would have loved a new dev to come in and say “I want to familiarize myself with this codebase by writing some tests, and seeing if there is any basic refactoring I can do. Where is a good place to get started?” The original developer probably wants to write those tests himself, but either has too much on this plate, or doesn’t realize how much time he’s wasting by working on a fragile codebase.
All in all, as other posters have mentioned, stay positive, and see how you can clean up and improve your little corner of the world.
One last point, I might take this as a small red flag. Don’t quit over this, but keep it in mind. If management isn’t willing or able to put engineering practices first, or to let engineers do their jobs the way they’re supposed to be done, it could signal other issues.
https://www.amazon.com/Working-Effectively-Legacy-Michael-Fe...
Well said. There are many possible causes behind a poor codebase, and it's rarely useful (and almost never persuasive) to go into finger-pointing mode. The issue for the OP is: from the business's perspective, is the poor code a problem? If it's not, can they live with continuing to work on this code in this job? And if it is: what's the next step, and what can they contribute to making it happen?
It's always tempting to use the word "rewrite" but most of the time you just want some assurance that your code doesn't break things, and a rewrite is usually overkill.
I would suggest you to add in additional time for refactoring into your new feature development. This has 2 benefits - you're not rewriting the whole project, just the parts that are relevant to what you are currently working on, and you're still pushing out features.
It sounds like most of your refactoring work is in code isolation / scope management and creating tests.
I had (well, I’m in the middle of it, so have) to fix a few bugs in a project that are the by-products of a deeply flawed architecture. I came to my boss and explained a handful of ways that I could potentially solve the problems, and made it really clear that the best way to go would be to completely redo this one part. He agreed with me, though he wasn’t happy about how much work was involved. ¯\_(ツ)_/¯ Hopefully your managers will appreciate you giving them options.
Take this one stage further, taking solutions to your manager is worth 10x taking problems to this manager.
Do not be tempted into 'rewrites' because you're setting yourself up for failure.
It's a long play, yes. If you're feeling the way you describe just from the current state, you're probably not senior enough to do it. It requires much more than just technical skills - those are abundant, it's not what you can build a career on. It's a bit Machiavellan, I guess, but in the end, everyone wins - but you need to have the Vision that others are not able to understand, so you have to find ways to make it happen anyway, in adversarial circumstances.
Good luck.
Of course it depends on the circumstances. If you're on your own trying to maintain a test suite on the code of 50 other people, none of whom work on these tests and think you're just trying to slow them down for no good reason, there's no way that will work. But if there's only a few people working on the code and you can maintain tests for a more or less well defined part of it - it's possible (if you're experienced enough; if you have to spend 2 weeks reading up on how to do unit testing, it's not a viable strategy of course.)
Damn that's motivating. I'll be quoting you on this one.
If moving is not an option, then follow the parent's advice. Add all the missing tests as fast as you can. Explain what you are doing to your manager. And set up CI to email your team whenever anyone checks in breaking changes. This will motivate your teammate to start running your tests. If they are particularly narrow-minded, they will be unhappy about this. If this happens, reconsider your decision to stay.
Even after you write a lot of tests, coverage will be low. You will likely code some bugs that negatively impact the business. I urge you to find a mentor inside the company, a senior engineer or engineering manager, and show them code and explain the technical debt to them. Try not to blame your teammate for the situation, just explain to your mentor that you are concerned about introducing bugs. Ask them to keep your discussion confidential. Your mentor can back you up when your bugs negatively impact the business.
Definitely: this will help you to better understand what the code does and the dependencies involved. Climb up the dependencies tree and add tests there as well.
Every software company has these projects and many developers have been in your position. As long as the software is still functional and is profitable then management don't care how bad things are under the hood (try explaining it to someone who only sees a working/functional UI). The fact a new resource (you) has been assigned suggests it's still a valuable project.
Unless management are the ones giving the go-ahead on the rewrite then I wouldn't bother. Rewrites are very risky, time-consuming and make no money for the company. However long you think it will take...double it. You're a mid-level developer, a rewrite needs to be planned and led by an experienced senior developer.
Your best bet might be patience...try ride it out and hopefully be moved onto other projects. In the meantime you get experience. It's how you handle the adversity that could prove more valuable.
It can also put you in an awkward position -- especially if the prior developer has a grudge against their code being rewritten -- as any bugs that come up can then be blamed on the rewrite effort, whether or not that was actually the cause.
Spaghetti code eventually collapses on its own, as every change starts taking longer and longer than expected, as well as introduces more and more bugs (and fixes for those introduce more, and so on). Once this starts happening, that can be the impetus to actually do a real rewrite effort, but one with a business case, management and hopefully the other developer(s) behind it.
Until then, a rewrite is a lot of effort that, at best, if you do a great job, is basically invisible (beyond the code level), and at worst either becomes a scapegoat for or the cause of many other problems.
I don't work at a small company but refactoring and especially rewriting code is difficult and error prone. It's a crazy thought but in many cases, it can be better to just leave the legacy code alone. It's all about ROI of your time and the opportunity cost of what you're working on.
If you're going to be making lots of changes to a specific part of the code base, then it very well may make sense to invest in improving it so r hat you can move faster after. But if the code isnt going to be modified much, it may be better to just get in, make the changes you need, and get out.
As a mid level developer, nobody is expecting you to come in and rebuilt the thing immediately. Focus on learning it well first.
This should at least give you the opportunity to contain technical debt to the most complex requirements.
To say "everything is complex therefore we cannot rewrite anything" is pretty implausible and mediocre.
"The idea that new code is better than old is patently absurd. Old code has been used. It has been tested. Lots of bugs have been found, and they’ve been fixed. There’s nothing wrong with it." - Joel Spolsky
You don't want to live in this hell during the rewrite, trust me!
(Oh, and another tip: if your management authorizes the rewrite you propose easily, it means you are SEVERELY UNDERPAID... the time of good devs always costs to much for rewrites to be worth it, unless the dev cut a really bad deal on hiring)
Chromium for instance has code from the original KHTML written in the 90s, most of that remaining code is reasonable and makes sense today. If it didn't someone would have replaced it.
... and also don't think that your "double of original estimate" is anywhere near a maximum of what it can take to actually make the code base better and functionally correct.
It is often true that someone with very strong domain knowledge may have had this project in their head for a long time, and one day, all-of-a-sudden, has to code it all in a rush. I have had this, where external factors shifted the companies priorities.
In this case you can 'brain dump' a lot of code very quickly, get it functionally correct and make intelligent judgement calls about the threats of edge cases... especially if it is internal company software.
However, coming at the problem from cold, without the benefit of this knowledge requires significant analysis and engineering effort to get equivalent results. So tread very carefully.
But yes, add tests
I am not familiar with this lingo. Why exactly is the OP a resource? Is this usage of "resource" common in business speak?
In rare cases a piece of software is so bad, that rewriting it makes sense, because it means you’ll save development resources in the long run, but in my experience that’s rarely the case.
The best developers write good clean code with low dependencies and tests for things that matter, most developers aren great developers though, and things can almost always be made better, even if they are.
So it’s really about time vs results. A good example is RPA, it’s some of the most simple code around, but it’s often very hard to get it to take over 100% of a process. Your developer spends a few days to take over 80% of a manual process, but the last 20% would take your developer months of small adjustments.
That’s often not worth it, and refactoring is often the same.
If the system isn’t slow, and you can still add new features to it without breaking anything, then it’s not worth a refactor.
This doesn’t just apply to developers by the way, I manage a lot of different types of people, but they are all resources that I need to fit within my budget to meet the goals given to me by my management, who in term see me as a resource to meet their goals. We’re all cogs in the machine.
Had a client where the employees where stored in a table called "resources". Also felt very much like a resource there..
It is the culture of the company who makes the difference.
Its not art, it's a job. (to me)
This is critical if you want the business to support your effort. It demonstrates pragmatism. It will shrink the scope of your changes and make the problem less daunting. You can prove to your boss and your coworkers that it's possible to fix the problem without slowing down new development efforts.
Be careful how you express your dissatisfaction with the code. Many of the people who wrote it are likely still there. Approach every change from a positive angle, so you don't alienate your peers. Get them to help you make the changes, and help them to learn how to become better developers. Don't assume they don't know better; they may have inherited a mess too, and they may have been under pressure to deliver quickly at any cost--or maybe they just don't know any better, but want to improve.
Read these books:
_Working Effectively with Legacy Code_ by Michael Feathers
_Refactoring: Improving the Design of Existing Code_ by Martin Fowler
_Clean Code_ by Uncle Bob
These are invaluable resources. There are many others, but these three are particularly germane to your situation.
As a professional programmer, your job is not to produce beautiful code. Your job is to make money.
There is a commercial decision for the organisation to make about paying back the tech debt in this code base. That's not your decision to make. Likewise, it's not your decision to create more tech debt if you can avoid it (sometimes you can't because deadlines are more important than tech debt).
As the parent said, your best course is to implement the new features, and refactor where it makes sense to do that to support the new features. But without any test suite, you risk breaking the existing features by doing anything more than the absolute minimum of refactoring. That's bad.
Good luck. And welcome to the profession hehe
Working in an unstable environment can drive you crazy as a developer and lower your productivity enormously. So if you don't talk about the issues in the code base with the management, they might wonder why you have lower output then others and classify you as a slow programmer.
Instead, I think you should bring the facts to the table (slow down factor (number of bugs, time to trace them down), risks (likelihood of production issues)), in terms of how much time it costs to work within the current code base and let the management decide on how to proceed (make sure to bring a recommendation). If they are willing to let you spend time on improving the quality, try to modularize it so that if the rewrite will be stopped at any time, the time you spend refactoring it helps you in the future. While doing so, show respect to the programmer that worked with that code base in the past (even if she/he create that mess). You don't know under which conditions she/he had to work, how she/he learned to programme or what her/his life looks like outside the job.
The management basically has these options:
1. just minor changes to the code base: no rewrite, no hidden quality enhancements (might be a valid option if an alternative software is being developed already). 2. hidden improvement: no rewrite, but you are allowed to spend some extra time to improve the parts you are working on. 3. restructuring of the code base and systematically writing tests for different modules (recommendation). 4. complete rewrite: unlikely to happen unless it is cheaper than 2 and 3.
If the management decides to continue with option one or two, do the best you can under the circumstances be pragmatic as caymanjim wrote. Make sure to include option four so that the management sees the full spectrum of options at hand.
To be fair, I put all three of those in my must read category.
Here's what actually happens.
By that point in a project's lifecycle, that's not what they usually ask of the new hires. People are usually tasked with fixing bugs/maintenance, and implementing new business logic. Both of which are extremely challenging and stressful when working on a project that comes with no tests, or documentation.
Your actual task as the new hire is to start generating more value for the company, but that's so much more challenging when you have no way of validating your solutions, no infrastructure to rely on, and no metrics or documentation to consult. At that point, being the new hire, you're taking a lot of heat for not getting things done on time - mostly through no fault of your own. At that point, "wanting to throw you under the bus to your superiors" is a very natural response - it's a defense mechanism, you're (rightfully) frustrated. You're between a rock and a hard place. At that point you're no longer doing software engineering, it turns to politics/coverups/fire-extinguishing on a daily basis. Can you not relate to that?
Start with high level integration tests of the most critical business processes. Then, work your way down to frequently used functions/classes/modules. After the whole thing is covered you can start cleaning/repairing, and I’d suggest doing so incrementally/as you’re adding new other features (make sure all new features come with tests).
In our case, I think we didn't write a lot of tests, because we didn't know they're so useful then. But much later I did a few ports of FOSS software between programming languages, and comprehensive test suites were absolutely godsend in those cases. So, if you can, try to write tests as first thing indeed.
Other than that, we took a kind of "organism fighting a cancer" approach. In a spaghetti codebase, we tried to find some smallest possible islands of the most isolated code (a lot of detectivistic work). Then try to improve the isolation even more — slowly engulfing the "tumor" in a more reasonable API. Once we had a somewhat acceptable API, with mostly well understood semantics, rewrite the internals of the tumor from scratch. Rinse and repeat.
Notably, as mentioned by JohnBooty, we did talk this with management. It made our product release late by ~1yr. The argument was that there was a particular critical bug, which would result in lost consumer data. This one bug was found very close to original release date, and opened our eyes to the horrors in the codebase (actually, a big part written by a subcontractor).
Sorry if the reply is somewhat chaotic, I didn't have much time to write it.
EDIT: Also, I believe the term "technical debt" is reasonably good when talking with management/owners, as it has correct connotation of an investment at early stage, which however has to be repaid later at a cost.
Next, the deployment process. Are tests run on all deploys? Are there canary deploys to minimize impact when bad code goes out? Easy rollbacks?
After that, I’d address the dev environment. Can you run it easily locally? Can you easily attach a debugger?
After that, add end-to-end, integration, then unit tests, in that order. Only then, once you understand the code well, and have it nicely instrumented and tested, should you consider significant refactors. By that point, you may even realize that a lot of it is good enough and rarely touched, and doesn’t really need to be refactored.
If any of the above steps are already well covered, skip them. And don’t spend all your time doing this - talk to your manager(s), try to get buy in to spend say 1/3 of your time on the health/quality of the code, 2/3 on features/bug fixes/etc. Justify WHY - in this case it sounds like the code would be hard for anyone but the original author to modify with confidence, to make sure the code scales past this one person without the product becoming full of bugs and instability, it’s necessary to devote significant time each sprint (or whatever) to improving quality. Can frame it as “original author was just trying to ship quickly, and could keep track of all the hacks because they wrote them, but to bring more people onto the project it has to be made safer/easier to modify for non-experts”. Maybe wait until you’ve been there a month or three before you start pushing this.
At the end of the day, the code doesn’t have to be perfect, it just has to be decently easy to modify with reasonable confidence. Tests should catch most errors, monitoring should quickly catch the rest to minimize the impact. Work towards that, over time, without killing yourself trying to sneak in refactors that your manager(s) don’t know about.
I've found it's also hard to test code that isn't modular and split up: before you can test anything you need to mock 2 database connections, one external API endpoint and reading/writing from 3 different folder locations. Either that or you split them up yourself and risk breaking something.
If you feel the code is messy it is likely you are missing some essential business logic as well when blindly rebuilding it.
I got it a while back when working on a similar sounding codebase and it was very useful. It's got chapter headings like "My Application Has No Structure" and "I'm Changing the Same Code All Over the Place".
If your company has a books budget get them to buy it, but I'd personally drop the £35 it costs on amazon given how useful this has been to me over the years.
Do you mean global variables? I guess it entirely depends on what language and framework you're using, but trying to refactor things to have less global state is generally for the best.
If the answers to those questions are no. Then you the problem and you should look for employment elsewhere. You are unhappy in that job, and you rewriting the codebase would potentially make the above questions answer to a YES, inflicting damages in the company.
Now, if the above questions have yes as an answer. Talk to your manager, explain how it won't scale, how it is hackable, and the bugs you found - and get the blessing to make things better!
Do not badmouth the previous maintainer/author you do not know the circumstances that led them into creating such a codebase.
I'm not saying that you have an option in a do-or-die situation where it needs to be done in a couple days. I'm also not saying that you should subvert or defy management to execute a massive overhaul or total rewrite. I am saying that if you understand a system, know that it's fundamentally flawed, and have a window to fix it (even if it's a really tiny one), but choose not to, you are part of the problem. The standards that software developers are held to in terms of quality are an absolute joke, and the "it hasn't completely imploded our business operation, so don't even think about fixing it" mentality is where those standards stem from.
I like to think I'm pretty good at programming, development, and generally architecting solutions. I don't have a lot of experience, but I have a bit of talent and spent a lot of free time learning. Solving problems with computers is something I'm passionate about.
I started this particular project without any idea of what it was (okay, they told me what it was, but it turned out to be something completely different). When we did a kickoff it was myself, two other developers (who both said during the kickoff they didn't want to be on this team, and wanted to go back to admin'ing MSSQL databases), and a non-technical TPM (last time he touched a terminal was a decade ago, and he wasn't particularly interested even back then).
I ended up doing pretty much all the work. My PM would CC me on emails, I'd write the stories, he'd help prioritize them, and I'd do them. The first few weeks they wanted three services stood up and managed in an automated fashion. I got it done, it wasn't pretty. Hell I hadn't even figured out a DNS scheme, but this had to get done so other teams would be unblocked; it was the development account so I wasn't too worried about rushed work.
Then we got more requests from other teams, and vague requirements from the infosec and governance teams. Then the board that reviews new technologies shot down our (my) requests to introduce automation services like Ansible Tower. Then the AWS team shot down our request for multiple accounts, one per environment.
At this point the Cloudformation isn't scaling, I beg and plead for some time to freeze feature development and move to Terraform. I asked for 3-4 weeks, and was given 4 days. This was the one chance to move to Terraform though, otherwise we'd be using Cloudformation for the foreseeable future. So I took it, deciding I could rush through the Terraform rewrite, because then at least we could refactor it later during downtime.
Next thing you know a year has gone by and we're going into production. Everything on the devops end goes pretty smoothly. My velocity is crap though, due in large part because of the shitty rushed work, which it seemed was all I was capable of.
A few months after go-live, we finally get a real TPM, which was a godsend. I was moving to another team at this point, and so his first order of business was to replace me with two new guys. I spent a lot of nights interviewing and a month afterwards helping them get up to speed. I and the new TPM gave them cover that month so they could embark on rewriting the Terraform.
They ignored my suggestions on how to refactor it, which is okay, I was leaving the team, it was their code as now. But it was hard, hearing them laugh at the code behind my back, saying how bad it was. Never giving me a chance to justify it, or even reading the commit messages, to understand why it was done that way.
I'm off the team now, and while I recognize I left a lot of legacy code behind (which I'm not proud of), it wasn't just me who created it. It was also the business who said time and again development speed was the most important thing.
That's how one legacy codebase was created. There were a lot of things I might have been able to do differently (mostly politically), but I truly believe I did the best I could with what I had. But what do I know? My new TPM seemed to agree with me, but maybe he just didn't want to make an enemy. Perhaps I could have headed it off if I'd said the right words to the right people. I don't know, will never know.
This experience is why I don't make fun of others solutions. Somebody put effort and time and thought into it, and I guarantee (in most cases) I don't have that context to properly evaluate it.
> Never giving me a chance to justify it, or even reading the commit messages, to understand why it was done that way.
This is a surefire way to re-introduce bugs that were fixed ages ago. Understanding why things are the way they are is the first order of business when working within a legacy code base. Was it done this way because the obvious way has a non-obvious edge case? Was it a requirement from the users? Or was it just the fastest way to do it at the time?
You have to have a certain mindset to effectively work on legacy code, and this isn't it. Most more junior developers I've known have a "rewrite" oriented mindset, meaning they don't know how to work within the constraints of what exists and refactor piecemeal. It's a hard to learn skill, generally learnt the hard way.
Open question: how does one fight against what's been described in this comment? What specific sorts of communication skills? One thing that comes to mind is learning when and how hard to push-back, and how to argue with force when necessary.
The reason I ask such a specific question is because I'm not great at confrontations. I don't dread locking horns, but I'm not good at it. The more force I tend to apply, the more crass and less refined (okay, rude/arrogant) I come across, entirely unintentionally. Diplomacy seems to me to equate to "die a bit more inside, burn some more of your own wick to deal with the frustration, don't say anything, and wait and see how things work out". So I end up not engaging at all. I know I'm missing something huge here and I don't know what it is.
This would be worth its own Ask HN, if it hasn't been posted before.
that's assuming the original authors used version control, committed often enough to leave meaningful commit messages, and bothered to explain their decisions.
pretty much every code base i've taken over in my current role have consisted of a single posting commit with zero documentation.
And outside of the one place where I was the person to introduce source control to the organization, I've found commit messages to be pretty telling. Most developers tend to let their feelings pop out in their commit messages, and too when and how often they're committing.
In your particular case, sounds like either a junior developer, legacy systems, or a sole developer.
But what about:
- maintainability, extensibility, reusability, configuration
- mean time to detection / mean time to repair during an incident
- bus factor, cost of adding more developers, documentation
Code can still be problematic in terms of those and I think that would be closer to what the OP described.
Consider you have 2 cars: both are reliable, but during maintenance one of them requires you to buy rare and expensive replacement parts that you need to import yourself, only one shop in the entire city can work with it and it takes 2 weeks to get the work done. Which car would you rather have after going the process I described 3 times?
Always loved this quote.
2. Relate your impressions to the person in charge: "The code doesn't seem to follow best practice and appears fragile. You think there may be technical debt that needs to be addressed sooner than later."
3. Until you're confident enough that you can pitch a comprehensive redesign and rewrite, wait for the person in charge to direct you towards specific repairs. When they direct you towards these specific repairs, they will anticipate regressions and instability. They'll see you as working through issues, not causing them.
4. Repeat step 2 periodically and with greater detail until you're ready to make the pitch in Step 3. Don't rush it.
The alternate solution: leave and let somebody else deal with the problem. Some people are comfortable working around fragile and wonky code; some people get really stressed out. You don't need to work in a job that's going to perpetually stress you out.
Yep, because I have worked with very bad codebases over the past 25 years, I actually enjoy the challenge. For me it is more rewarding than working with pristine and well managed code bases.
I was explaining greenfield vs. brownfield to a co-worker (non-developer) between periods of fixing a previous, junior, developers code that was essentially creating a seemingly infinite number of pages on our site.
Keeping in mind the above, I also know that deadlines, changing requirements, and lack of a clear goal (due to the previously mentioned items), lead to this. Had I not worked with them, my decade and a half of experience would have strongly suggested that such was the case.
When it comes down to it, some people prefer working on new projects, and some people prefer supporting and polishing those once-new projects. But to really succeed you've got to be able to do both, to some extent.
Greenfield development or maintaining a legacy code base that isn't complete shit is a luxury. Leave things better than you found them but you have to earn respect/influence before you can start attempting sweeping changes.
PS - https://blog.codinghorror.com/the-ten-commandments-of-egoles...
If the environment itself is good, and you like the people you work with, then you should see it for the great opportunity that it is; learn how to slowly evolve a large software project for the better. There's none of the stress of trying to figure out the "right" architecture, no worrying about making bad decisions. You can just chip away tidying things up.
Things that are probably a bad idea: Trying to rewrite (will almost certainly fail), Blaming others (it's no one's fault, it just is what it is), Walking away (negative signal for future employers).
Start there. That book changed my career. Just look at the chapter names here: https://amzn.to/2wnPHfi
Chapter 6: I dont have much time and I have to make a change
Chapter 7: It takes forever to make a change
Chapter 13: I need to make a change but don't know what tests to write
Chapter 16: I dont understand the code well enough to change it
Chapter 17: My application has no structure
Chapter 22: I need to change a monster method and I can't write tests for it
Chapter 23: How do I know that I'm not breaking anything?
All the stuff is gold. It applies to every language, every work environment and every client I've ever been at.
This is the #1 book I have my teams read.
edit: Formatting and addl chapters
> I've already made some comments about rewriting it and the response was basically "ok".
This means that the person in charge of the code is probably aware of its shortcomings.
> The person in charge of this project was working on it alone
This suggests that probably the codebase started out in a neat form and became messy over time due to understaffing/bad planning/feature creep. There is only so much he/she could do as an overworked single dev having to manage a large system.
Your senior is probably very happy to have another dev helping with this. I wish I had someone helping me. Take it as an opportunity, dealing with legacy is a challenge but you can grow a lot as a developer!
I agree, but it's fraught with risk, and could also turn into an opportunity to spend several months or even years being miserable, and thought a mediocre to poor developer because you aren't able to deliver fast enough. This becomes especially true when the original developer is there and can probably do things 10x faster (which will make you look bad).
You'll need to make sure you're having regular one on ones and keeping your team lead/manager up to date on the work you're doing. Perception is more important than anything. If you save the world but do it quietly, nobody will know, and they'll decide you aren't productive enough.
I agree, I think OP should find a way to present his work as a "project" that makes sense to management... A good example, someone else in this comments thread suggested that OP could create an API around the legacy core. Creating an API is something that can be "sold" well to management.
Do it.
Gradually.
Rename. Simplify. Reduce. Enhance. Test. Slice and dice.
Embrace the shitiness. Accept it. Own it.
It's your playground. Sharpen your skills, and lead them to the promised land.
It could be worse. You could face major resistance and people issues. Appreciate it.
This is an opportunity for you to kick a billion miles of ass...
Also see if you can make a to-do list. This will help make your request concrete to management. As you cross items off it will help them see that you're actually getting work done. And, it could help to motivate you if you feel like you're "just rewriting the code".
That's not likely to get good results; you'll want to do more prep work to ground "did a bad job" in data. That data will also be useful in making the "rewrite everything" decision, should things come to that.
Start off at the organizational level: how did this code get into production? You're not going to get anywhere arguing for code quality at a place that doesn't value it. Is the rest of the codebase like this? If it is, you may be facing a larger company-culture issue.
Making real improvements is going to be seriously difficult without automated testing, so you'll probably need to add testing incrementally. Maybe start off by reproducing a production bug in a test and then fixing it - this delivers immediate business-visible value faster than stopping the world to write tests. Whenever you make changes, wrap the affected part in some more tests. Are there manual acceptance tests? Consider automating some of them.
Finally, be prepared to bail if nothing works - some places suffer from the "Market for Lemons" problem, where good developers who join figure out it's a dumpster fire and leave behind only bad ones.
You might be the exception, but when you've been working on code for a while, your code is probably like this to an outsider too.
The difference is that you have a ton of insider knowledge in your head that makes the code understandable. The person who wrote the code you now hate probably does too.
I've learned to enjoy fixing up projects like these. You can learn an enormous amount from fixing bad code, and I truly have! Unfortunately it's probably something you have to learn for yourself. Just try to have a good attitude about it OK?
Next pick a component which is particularly problematic, or which is a focus of business attention at the moment. Look at which lines/conditions are covered by the tests – are the critical parts of the code, especially the success cases, being covered? If not, write tests for them. (Covering error cases is less critical, although user errors – bad user input – are more important to cover than things like IO or network errors.) Don't get hung up on a numeric target – which bits of code are being covered is more important than a number.
Once your test suite covers the important parts of that component, start slowly refactoring it and cleaning it up. Then, once you are satisfied it is in a better state, choose another component to move on to.
Of course, that assumes you can get your management on board with you doing the above. But if they aren't keen, you could try to phrase it as "Can I spend 20% of my time improving quality, and the other 80% on new features and fixing reported bugs?"
Sorry if this isn‘t helping, but have some respect for a codebase that seems to be working. And write lots of tests before touching it.
This right here is the best advice I've seen in the thread. Lot's of people are giving good advice here.
Definately write lots of tests before touching it. As you write the tests you may even start to see a pattern or "method to the madness".
No. This has the disadvantage of possibly making you look naive. The code might actually be fine and you might not have enough experience working in unfamiliar codebases or reading other people's code. Moreover, original developers will have made choices for a particular reason, often without commenting and you might change something unintentionally if it doesn't read straightforward the first time through.
If it's mostly style issues, forget it. Unless it's highly trafficked code, the computer doesn't care if you use PascalCase or snake_case or awkward indentation, etc.
Report that you might be wrong, but the code seems overly verbose/incorrect/hard-to-read in some areas and that you are carefully refactoring, but that you are still getting familiar with the company's product and would be excited to work on a different area of the codebase as soon as this project is complete.
I think the only reason that you should be concerned with "this person", is to influence them (or better, the work environment) to discourage the behaviour that led to the problems you're struggling with. For that to be effective, you need to stay positive.
Remember that it's much more powerful when someone learns something themselves, compared to being told that same thing. If the code is broken/fragile, then you'll be presented with opportunities to show positive examples of how your work is improving things. Folks who know the history of the code will hopefully see what's going on.
The codebase isn't going to be magicked in to a better state through political moves, so assuming that you stay on, you'll need to come to terms with it.
You might claim it is spaghetti code because you believe in some dogma that your predecessor did not adhere to. Maybe hold back for a while and try to understand what's going on first?
I've been there as well, but after going through quite a few 'paradigms' I have to say that I prefer spaghetti to
lasagne (layers and layers and more layers of crap)
calzone (looks nice on the outside and has test cases for every line but thats because the logic is so contorted and mutable state is everywhere)
the mafia that tells me I can't use this keyword or other, how long lines can be, that I can't use a single letter variable name, etc pp
You're not going to be there for the rest of your life. One year is enough. Take it easy. I went through this multiple times, there are lots of places with a shitty code. The worst thing that can happen - you'll will start blaming yourself. But if you just relax and let things go, you'll get salary, 9 to 5, job security. Don't try to fix everything in one day. File stories, fix issues one by one. Eventually you will like it, and this is the real software engineering, the real world.
Yes, there are better places, but you will never know until you get there. So if you can live with it for about a year, it's good. If not, start looking, and remember - it's not your fault, and it's very hard to avoid that. You need to work in a company for a couple of weeks to understand how things are going there. So you new job can be the same or even worse (it also can be better). But there are no any guarantees.
No matter how insane a code base looks, (unless it is totally inoperable) it is meeting a lot of conditions that are more important than whatever you are fixing but won't be recognized as important until you break them.
Those conditions built up over time and were changed irrationally by business decisions and hit or miss communication. A lot of demoralizing for anyone who was trying to keep standards while meeting requests.. Don't beat up the past maintainers for a past you can't know in much detail.
Maintaining old code can be fun though and is a more interesting puzzle game than most new development. Just try to stay emotionally neutral and unattached to anything about the code besides the specific challenges as they need tackling.
This is easier to deal with both in terms of selling it to management and making sure you don't actually break the whole system compared to a full scale rewrite. I've made that mistake before and it wasn't fun. As others have said, code that's in production and works and is making money has value. The "cleanness of the code" is not something business owners care about, unless it causes problems.
I joined a small company 8 months ago. The code at first felt somewhat sane, but the more I worked with it the more I understood that the whole project is a huge copypaste mom's spaghetti. I started rewriting everything I "legally"(somewhat within the scope of my tasks) could. I also kept asking the lead coder about the poor quality and poor decisions up to the point where all we did was argue. Management saw this and it turns out they were not satisfied with his performance for quite some time. He gets kicked out(after 4 years). During the 8 months I have deleted 1/3rd of the whole codebase, replacing it with much better code both quality and performance wise. I did that because I don't want to spend the next couple of years suffering from someone's bad decisions and complete lack of experience. Now I get to work on much more interesting tasks, everything gets delivered faster. Some of the stuff I wrote gets used across other teams. Only things that still keep me getting stuck in debugger are those I haven't rewritten and everyone agrees we have to rewrite them in near future.
I know I made a risky decision, but it was well worth it.
- get assigned a task/project
- when your changes interact with existing code, take some time to write tests
- refactor as necessary so that you aren't making things worse
This is what you should do day to day as a software developer. Don't ask for permission to refactor, just do it (using your best judgment) as you also deliver features.Is it typical to be able to look at a company's code before signing up? After having a similar experience, I wish I could make that a standard part of my interviewing process. (Brief backstory: I would never in 1000 years have taken my first development job if I had seen the code first, but HR and the carefully selected engineers who could tell positive stories gave me a false impression of what was going on.)
We now have to demand to see the codebases we are going to work with/against first. Even if we only touch the API in some cases; if the API is built on quicksand then we risk spending a lot of time building something that is not viable but that might not be immediately clear.
I guess it is not typical, but at one company I interviewed for I had a day for implementing something (a small feature and some unittests) which allowed me to check their codebase. I liked what I saw and worked there for some months.
Start here. Start writing tests. Lots of tests. This is part of what makes it engineering and not just craftsmanship.
Once you have enough code coverage (this will take a long time), start rewriting small parts of the system. You will have your tests to fall back on to verify you new design is correct. For each part of the old system, once you are confident, replace with your new code.
This all needs to be communicated up front as a long term goal. It's not about a rewrite. It's about the tests and having high quality software.
Something you need to understand, they may not want or need high quality software. Quality costs money. Sometimes good enough is all they can afford right now, even though we know the tech debt will accumulate interest. A part of your pitch is to communicate how important quality is to the long term viability of the software.
Think about other engineering disciplines - "okay" engineers can build new bridges, great engineers can cheaply fix existing bridges so they never fail again. It is the same with software. Good engineers make new programs, and whenever they see a problem, they start from scratch, wasting time and money. Great engineers massage existing code into something amazing, increasing efficiency without breaking the bank.
As a side effect, if you make something crappy more reliable, it makes the creator look bad, as they should. Otherwise, you will look bad.
What you _can_ do is separate large swaths of code into methods, even if they are only called once. This will make debugging much faster and more effective. Pretty much anytime you have a section of code that starts with a comment "// Foo the bar", you should consider isolating it into a function "fooBar(int a, char *b)" which you'll notice is even more informative that the comment, and can easily be stepped over when debugging with only the final return value confirmed to be as expected.
Back in the 90's and early 2000's, almost nothing had automated tests. Miraculously, things still got done by manually testing.
On the other hand, I worked at shops where every getter and setter had a test. Not much got done there.
That is still very normal and not very miraculously considering it happens all the time and very large companies run on this practice.
Although I am a fan of automated tests, I work with enough codebases that are robust and have been working for over 10 years that have no tests and others with a lot of test that are still fragile and miserable to work with. It is not all that black and white but you imply that as well with the getter/setter tests.
The best advice I can give is DO NOT start a stop the world rewrite! and to look at the SOLID pattern and implement it using the STABLE strategy.
-Smell your code -Tiny changes first -Augment your tests -Back up -Leave it better than you found it -Expect good reasons
Full credit to Sarah Mei
https://markheath.net/post/stable-tactics-for-writing-solid-...
Do not do the cardinal sin of writing unit test for every goddamn function in the codebase like a rookie.
Start fixing code, one by one only if it can be tested by Unit tests.
Remember you need to first learn the code base and context because harping "complete mess". Maybe they did not have time, maybe timelines were more imp. That is what industry is about especially when you do not have enough Engineers. Plus this new wave of PMs who want features at any cost, doesn't make things any easy.
Good luck
1. Try to get the political will to change it, then lead a change. This is essentially impossible unless you're a respected senior developer, and even there it is hard. There is a lot of advice out there, but the primary thing I've seen work is this: Complete feature freeze. API-ify the backend with heavy tests through the API. Have the presentation layer slowly shift to the new API. Once the API is complete, delete all the other backend code. Refactor the presentation layer with tests as you go.
2. Accept that the codebase is shit and look at your situation realistically. If you can't switch jobs (visa issues, etc) you need to accept that this is the way it is at this company and don't grumble about it. Spend 20% of your time making tools that help you compensate for the shit quality of the codebase; admittedly this is easier on non-Windows projects. Even so, making a small tools for disorganized projects really helps make you look super pro, even if the code is doing most of the work.
3. Quit and work somewhere else with better people and a better codebase.
Out of all the reasons I advocate TDD, from cybersecurity to QA, they pale in comparison to employee retention. Keeping a codebase clean over time requires tests. It just does. If you don't have tests things ossify. I don't write tests for ad-hoc querying data, but I always write them for long lived projects and I refuse to work on established projects that don't have test suites (or direction from on-high to add them).
Even if you're wrapping the entire application.
We're not looking for "does this code do the right thing", just "does this code do the same thing" (as the initial code).
At which point you'll at least have some more confidence that you aren't damaging the codebase due to having not understood a part of it.
- don't be rude but make it clear you think the code is messy and you can clean it up. Explain it to non-programmers in terms of time needed to add new features and the much greater chance of bugs and side effects
- get a few style rules going (do A and not B, group regions/routes/modules -- with only comments at first, if needed) to apply ONLY to new code going in.
- every time you have to touch old code, bring it up to style, even if your changes don't directly address spaghettiness
- slowly (and more confidently now that you're more familiar with the code) refactor parts of the code
- get at least some sort of test structure in there
It'll take a long time but it'll mostly work and you'll probably find it very satisfying. I know I do. Be polite but firm. Believe me that everyone knows the code is a mess and if someone is willing to put in the legwork, every developer worth their salt will respect you for it. You'll have the authority you need to enforce your stylistic choices and the future structure of the code.
Remember above all else, though, that your job is to write working code, not pretty code that developers like. Your goal should be to infect it with order, not tear it out and rebuild it.
Some pitfalls you may expect:
1. You find some crazy thing that makes absolutely no sense happens in the code for some specific inputs. You remove the craziness, and someone complains that an essential feature just broke. When the essential feature is explained to you, you find that its implementation is batbarf crazy, and that although it would have been easily implemented in a more straightforward way, someone important, somewhere, is redefining inputs and re-interpreting outputs in some special cases to get a feature they needed and no one had time to implement.
2. You find some crazy thing that makes absolutely no sense happens in the code for some specific inputs. Based on your knowledge of the many pitfalls, you ask permission to fix it. The more experienced person whom you ask explains that the input conditions to trigger your bug are so rare that it makes no business sense to waste any time to change it, it's not a priority, and you will understand things better when you have more experience, and why are you wasting my time asking me if you can change it when 85% of changes in this code base introduce new bugs (because it is such a mess)?
3. You find some crazy thing that makes absolutely no sense happens in the code, not for just a small subset of inputs, but for quite typical cases. You remove the craziness, and then you learn that a 'workaround' is in place, perhaps including both other software and user procedures. Lots of people are earning a living doing the workaround, and they don't want to change.
4. You find some crazy thing that makes absolutely no sense happens in the code. You remove the craziness, and then you learn that some other system is now out of balance with yours or fails to run with the data it gets from yours. No one anywhere can figure out why, but you caused the problem.
5. You find some crazy thing that makes absolutely no sense happens in the code. You remove the craziness, and then you learn that your chief accounting officer thinks that he is a genius because he found a way to turn craziness into quarterly earnings, and he is not happy with your fix, and the auditors want to talk to you.
6. You find some crazy thing that makes absolutely no sense happens in the code. You remove the craziness, and then you learn that your CEO's brother earns a commission or royalty on the craziness, and he's not happy.
7. You correct a spelling error in a report heading, producing numerical errors in twenty-seven downstream reports.
That said, one of my favourite ways of "rewriting" is firstly to identify any parts of the system that could be separated off into a second application and start designing and building a new system in parallel.
Another relatively easy task is to spend a small amount of your working week building the "rewrite" from scratch starting at the highest level so that you can learn some of the problems that are not obvious from the current system but which the previous developers had to learn while they went along. You could build a large part of the scaffolding without struggling too much and then have the basis of something to suggest for the future.
There is a reason things are like this. One thing are "best practices", blogs of design patterns advocates, interview process, and smartass community celebrities. Another thing is real professional environment with business reasons, limited budget, and limited personnel.
> Should I just go and basically say that this person did a bad job?
It will not impress anyone. It's not a job interview where one can smear, judge, and reject the candidate for the most trivial nuance. It's your daily job and you'll have to work with these people.
Rewriting functional application during the first months of employment doesn't sound like a good idea. Use cases, edge cases, algorithms - probably took good time to figure it out. Just think about it - the last guy trying to do it ended up alone, dumping the whole codebase on someone else.
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?
It's not easy but it's possible.The first thing you need to do is get some people on your side, politically. If you just rewrite the code on your own, nobody is going to thank you. You might even look like a terrible employee because you're sure to break a few things, especially since there are no tests to help you.
Explain the problems with the existing codebase, and how they're preventing the team from delivering features.
One idea - give them some easy to understand graphs so they can see how much time's being wasted on the spaghetti struggles. Record your time for a week... show them a pie chart of how much time you spend actually implementing new features vs. how much time you spend fighting with the spaghetti.
Come up with a plan. Suggest devoting X% of your time to reducing technical debt and Y% of your time to delivering new features.
Come up with benchmarks to show your progress. Performance benchmarks, productivity benchmarks, anything.
Should I just go and basically
say that this person did a bad job?
Be very careful about this.You can explain the bad state of the code without implying that the previous coder did a bad job.
After all, it's normal for software to accumulate technical debt over time. This is especially common if you have a single coder that is under pressure to deliver a lot of features in a short amount of time. It doesn't necessarily mean he/she is a "bad coder"; maybe he/she was just not given time to refactor and reduce debt.
I've already made some comments about rewriting it and the
response was basically "ok".
Sounds like they're aware of the technical debt, so that's a good sign maybe? I started "repairing" the project but there're no tests to check whether my
adjustments are correct.
This is, of course, step one after you get approval and before you start reducing that technical debt. =)Good luck!!
Rewriting random code as a codebase newbie is always a mistake. You will get things wrong. Mistakes can be delayed a long time before you are aware of it.
Try to understand the product. You should spend more time to understand the product features and flow instead of staring at single ugly code fragments. Understanding it will reveal some structure in the code, even if it isn't explicit. Try to engage your peers about things you don't understand. They will potentially deliver tons of detail and background information. Over time you can mentally map this high level information to the codebase.
If you understand the product better, I'd suggest to write a few (1-5) simple selenium tests that cover the core tasks of the product. This test is simply to ensure that you do not break something on the greater scale and the product is still delivering it's basic value. If you start to find confidence with you tests, you can share them with your peers. This will give everyone some instant value and potentially a lot of respect towards you.
You seem to be afraid of your colleague. You don't even know if he wrote all that code, you don't know about his stance on the codebase. Engage him, try to find out how he is working with the codebase. Try to find out his skill level and mentality. Do not judge early, don't complain. Even if he is low skilled, he could have a lot of knowledge about the product and the company.
Eventually don't expect to rewrite the whole codebase. Rewriting a large valuable project which is in production will take easily a year. One year after that someone will claim this is legacy. It won't help. It is easy to think that you can do better, but even a clean codebase can be a huge mess. Figure out how to gradually improve the project alongside features. Figure out risks and high friction tasks for devs, operations and users.
No, absolutely not.
Look for the positives in what has already been accomplished, then sell them on the idea that "it can be even better."
Since it sounds like they are fine with you making improvements, just take it one step at a time and keep at it.
The code base that exists took many hours to develop. You wouldn't have a job at all if it didn't exist in some kind of functional state, no matter how bad it is.
Don't brag. Don't position yourself as some know-it-all hot shot. Just keep improving things and let your work speak for itself.
Ideally, you want to get good at explaining why the change matters in terms that make sense to the non technical folks involved in making decisions.
Do make sure they know you did the work. Don't let someone else get credit for it.
Don't go into the trap of rewriting the code the first week, all new coders think that is the only solution on the first job. It takes time to realize why the code ended up in that state in the first place and badmouthing previous developers doesn't make you look very good. Management might very well know why the codebase looks like it does, priority is often time, not quality.
Something that looks like a bug might have become a feature that the users depend on. If you remove that bug you have created another instead risking corrupting data and/or angry customers not able to do things the way they are used to.
For all you know you are being tested for work culture and how you behave towards your collegues :-)
But from your post it sounds like your immediate problem is you don't like working on ugly code. Thrn I'd suggest trying to get transferred, rewriting jto from scratch (at most companies this would be a hard sell, and probably a money pit), or find a new job.
Complaining about the code is only going to make your workplace more hostile.
If you do decide to go for the rewrite option try to make the case from a financial aspect. Calculate the cost of maintenance, the cost of the rewrite etc...
Don't be tempted to start a new shiny clean system. This almost never works (unless in demise cases) and you'll be playing catch up with features on the original system for the next 10 years.
As others have said. You have to write tests, this will cover your back and you'll understand the codebase back to front once you have - Testing/Interfaces/Simplify/Naming.
Remember, all the business logic is there. At least you don't have to do the hard part... this is a cleanup operation.
middle ground would be to write blackbox style tests and make them portable to the next revision of the codebase.
don't say that last person was a bad at coding, you won't benefit from saying that.
1. Is this code in production already making profit? 2. How often is this code updated? 3. Is it profitable to fix that code or just to learn from that experience and move on?
It is ok to complain about code quality, as far as you are doing something to improve it. Don't ever complain about a previous engineer, they might have been just implementing the sh*t product was enforcing on them or company asked an inexperienced engineering to glue together this project. It is rarely that a developer knowingly and happily writes bad code. Try to improve it, it will move you towards becoming a senior sooner, nothing helps you in understand how to code more than how-not-to examples. :) Also +1 to do-not-refactor-immediately comments here, observe the project and company and write tests until you know the project and culture well enough, then either increase refactor speed thanks to your tests or separate the project into chunk as micro-services or utility libraries/modules.
Also run static analysis on the codebase, you will be amazed how much "foo defined but never used" cases there will be, remove them completely, have a close eye on production errors when you release them and role-back on smallest of suspicion something is wrong. invesitage, fix, release and repeat. :)
The good news is that the team and code base are both small. Bigger teams and bigger code bases (like hundreds of thousands of lines) become non-linearly harder to repair.
Likely high level goals are: being happy at your job and making your company happy to have you there. As a new person on the team, refactoring on your own is perilous and probably won’t advance you towards these goals; so, what should you do?
Often, tangled code is paired with tangled development methods. Look for places to make useful improvements in the development process. Is the code and it’s data protected by adequate backups; is it under change control; is there a release schedule with documented testing procedures; is bug reporting and tracking done; are there specification, architecture, and design documents (under change control); are there tests; are there reviews and coding standards.
During this period of time, while learning the code base, you can make yourself valuable by helping with the process. Be sensitive to company politics and don’t embarrass people that could make your life difficult at this time. If the company has a reason for or against using cloud backups, don’t try to change it, but if they don’t do backups properly suggest that you can dive in and help with it. If they are doing adequate backups, move on to look at their source control. I’d especially focus on ensuring that source code control (like git or Microsoft’s Team Version Comtrol) is being used. Help in the areas that need it the most first.
To introduce better coding you could ask the person in charge to “help you” by reviewing your code. This will allow you to talk about your ideas without directly challenging their existing code. Act humble, but more than that be humble about receiving suggestions for your own code. This will foster an environment where better code will be produced.
I was working on a big OS kernel project once, and a new contract programmer joined the team I was leading. He spent the first few weeks getting a better kernel debugger running, while keeping me up to date on his progress. Other developers started going to him to get help with their debugging and soon, he became that essential guy that everyone respected and needed. At that point, he was able to start suggesting changes to parts of the kernel that weren’t even his. Dispite the huge code base and the politics of numerous departments and hundreds of programmers, this person made many important improvements throughout the kernel over the next couple of years and was viewed as one of the most valuable developers in the organization.
Then, isolate a section that is practically reeking. Maybe it's even so bad that it's leaked into the user experience. It has to be a super small part of the codebase though. Plan out the refactor, making sure to make it backwards compatible (that means no regressions, no changes to internal interfaces, etc.). Start the refactor, then when you reach about the 70% mark (which will be more like the 30% mark considering regressions and stuff), try to pitch the refactor. But don't pitch it as a refactor. Figure out any problem that the user or manager is having, whether it's slowness, crashing, etc. and then slide in your refactor as the solution. Like hiding spinach in brownies. If there aren't any problems, well, then maybe the codebase isn't that bad.
And remember, 50% good code and 100% done is better than 100% good code and 50% finished when it comes to refactors.
Look into various books and articles on refactoring. Don't change things open-loop, write tests if you can, manually run and verify too.
I have been there and the sad truth is that for most codebases I'm going to argue that bringing the whole thing up to a nice status is going to cost too much if it's already in production. Unless it's going to be running for many many years, it's probably not worth the investment. And if it's not worth the investment, then there will be no investment. That may be the correct management decision - but it's a huge red flag when choosing a workplace.
If the code isn't in production already, then it's another matter. Then you can make it worse before it gets better, and it may be a productivity boost even if the cleanup takes several years.
The thing to look out for if you can't clean up the whole thing, is any kind of modularization. Can you suggest that some subsystem is either rewritten or refactored? Make a business case such as "we have so many bug reports in subsystem X" or "We could easily make Y processing 10x faster". Even without tests, it's sometimes possible to implement a new subsystem and use a previous subsystem as the test - your new subsystem works if it responds the same as the old one.
If you are not allowed to take an architectural or greenfield approach (i.e. you can neither rewrite or do large-scale refactorings of at least parts of the code) and you are simply asked to provide small focused (business-driven) changes to the code, and management shows no clear dedication to large scale refactoring or rewriting, then I'd reconsider working there.
When I joined I was told by one of the (post-startup) devs he had a rule: whenever he had to fix a bug or add a feature to a piece of code, he'd try to clean up the method or class he had to work on to implement the change. Bigger changes then allowed for bigger cleanups.
We also had the challenge of testing. So we decided to accept the situation, that we have a fragile project and that our changes will lead to bugs, and instead wrote an infrastructure that made it easy for our users to launch older versions of our application. For the most part this meant blocking bugs were not such a PITA for users, if we broke version 42 they could easily launch version 41 and use that until version 43 with a fix came along.
As for saying the lead guy did a bad job, I think you're making the mistake of looking at the code as the most important thing. It really isn't. The application and its functionality is the most important thing. Functionality is what makes the application useful to users. An application with no users is useless.
Start adding comments without changing code. Any time you touch anything, comment that area as you figure it out.
If you find unused code, put something like #ifdef OBSOLETE around it, rather than removing it immediately, and stick in "assert(false)" where something is unreachable.
Then implement and run the tests. The code will fail some of the tests, so make sure that those tests really do correspond to the spec and then present the remaining failures to product owner/your boss. Maybe the spec will change, and then the tests will change and the code will pass. If there are still failures then get written instruction to fix the code.
Eventually you will have code that passes all the tests.
Now you are in control. You know what the code should do and that it does it. And best of all, you can make changes to the code and know if the code is still correct.
Additionally, you might find yourself looking at the awful spaghetti in a different way. After all, the spaghetti does exactly what it's supposed to. Maybe you can make it do it better and be more maintainable etc, but as far as anyone else cares, it'd better taste exactly the same or else you'll have screwed it up.
First, 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!
While I agree you should not attempt significant rewrites right away, and spend time to develop your knowledge of system... don't just sit back and wait. That won't help solve the problem.
1. Be up front about your opinions and rally the group around the long-term goal to improve code quality. Do it in a positive manner where it's clear that you want to make an impact and help make changes for the better! (not just complaints) 2. Explain your reasoning clearly (testing, code quality, engineer happiness, etc) and ask for input / get buy-in. Open up the discussion. 3. Bring this feedback up consistently as an important part of your experience at this company (in 1-1's, and other team meetings) Good ways to do this are when discussing OKRs, retrospectives, etc.
Believe that YOU have the ability to spark a change and develop real pride amongst the team! If you don't feel this is possible, this is not the right place for you.
"relatively big spaghetti (tens of thousands of LOC)."
That's not very big.
"Should I just go and basically say that this person did a bad job? "
Definetly not. Just writing ugly code is not malfeasance. If the code actually works as expected once compiled then you have very little basis for this accusation.
Functionally complete and maintainable are two different things, and how much the second is cared about varies A LOT.
"The person in charge of this project was working on it alone and from the outside it all looks fine and it's working."
First, and the second metric of code in business is that a) does it compile b) does it work.
All other considerations are secondary.
"Maintainability" has no absolute metric. Organizations learn slowly about this, and see how it affects their internal capability to deliver, but there is no way you can directly point out based on what metric the code is faulty.
The guy before you did not do a bad job if it actually works.
The time to point fingers would be if there is no source code to be found, you found out it's some repurposed third party codebase with an incompatible license related to the business use of the code, you found out he had sneaked in a virus to the installer and so on.
'I've already made some comments about rewriting it and the response was basically "ok".'
Sounds like an ok way to move forward. Make sure you understand the business value of the codebase as well.
God, no. If the code works and scales, he did not do a bad job.
Start to apply your new standards for your new code you write on it - make tests for it too. You might need to add a few tests for the most important parts of the old code to make sure nothing breaks but I would leave the old code be as much as possible.
The best thing to do is to gradually rewrite it by always leaving the code in a better state than it was before. For example, you're tasked with a non-trivial adjustment to an API endpoint--instead of adding to the mess, extract the endpoint out into it's own file and rewrite/clean it up. Write tests beforehand if that makes you more comfortable.
Using this technique, my current team has been able to slowly rewrite our codebase from spaghetti JS (read: thousand line JS files) into a more structured TypeScript project. It has a long way to go, but doing an incremental rewrite works wonders.
And like another poster said, being able to read legacy code is a good skill to have. After all, you'll need to understand it before rewriting it.
Regardless of the exact situation, there should always be a way for you to work on your craft. It sounds like you are doing a lot of refactoring now and that could be quite rewarding, even without tests. If you find yourself trying hard to improve the environment and feel like it is to no avail and that no one is listening, while simultaneously feeling like you can’t leave the job in the short term, you might want to think about how to take advantage of the dead time at work to learn new things or do other activities that improve your skills.
Frame it correctly: "I don't understand why these classes share this data or code, do you remember why?" is miles better than "Explain me this mess please it's making me crazy"
If the original dev is still around focus on the task that come from business so he's free to focus on fixing the structure, he is, after all, the local expert on it.
In general a rewrite is not something you'd do without having a clear understanding of the requirements. What you can do instead if you don't have the dev support is to fix thing as you go, removing cross dependencies and encapsulating behaviors. You don't have to fix code only to hide the ugliness behind a clear structure and as small as possible interfaces, order will eventually emerge.
A strategy of unethical developers is to make themselves relevant by producing a lot of features while drowning the project in technical debt.
This often happens when:
a) stakeholders are not technical.
b) their strategy is to have an early exit (like an acquisition) before the problems caused by the technical debt materialize.
c) they can compensate for the technical debt hiring an army of thousands of developers.
I would say it is a management problem. And like all management problems, unless you can fix that, your best option would be to spend your peak productive years in a company that cares.
If you want to take issues to management, tread very lightly. First of all, your challenge will be to present the problem while not seeming depressed, irritated or pessimistic, or heavily criticizing others. Try to sell the idea using a positive twist, like increased productivity, robustness, etc. If that doesn't work, just start interviewing.
---
If you decide to stay, just start creating tickets from all the technical debt. Make sure to include details like commit links, and objective information about how it is a problem. Then:
- link bug tickets to tech debt tickets.
- link incident tickets to tech debt tickets.
- if a task requires a tech debt workaround, create a separate ticket for the workaround and link it to the tech debt ticket.
This should create some sense of visibility around the tech debt. It will likely won't work anyways:
- Some people feel smarter and empowered when they deprioritize tasks.
- The only ability of uncreative managers is to say no. Saying no makes them feel useful.
If your project doesn't have an issue tracking system, then recommend something simple that fits into the tech you have, i.e. GitHub or whatever.
Use the issue tracking to create and document the shortcomings and technical problems you're seeing so that the more senior and leads of the projects have documented visibility into what you are seeing with the code and the application.
For example create a technical task: "Add unit test coverage for user sign up" or for whatever gaps you're seeing in testing.
In my projects I always make sure that we have a category available for "technical tasks" and/or "system stories" to document anything that is engineering/code/refactoring/devops related vs. user facing functionality.
If these channels don't exist, then you need to recommend that they are established so you can document and track specific issues you're seeing.
Also, since you've recently joined, any team that brings on new members should be open to what you're seeing as someone new with a fresh perspective and be open to learning from you as much as what you need to learn coming into a new project. Hopefully, your team has a culture that is open to new ideas and if so, don't be shy on bringing up technical issues in standups, chat discussions, PR comments, etc.
Just be proactive in documenting your specific issues, ideas and what you're seeing, ideally through the established issue tracking system. Be prepared to offer, document and communicate solutions to the problems and gaps you're seeing.
Good luck with the new project and I hope it works out for you and your team.
Generally speaking:
1) Stick to bug-fixing for a good few weeks / months. This is invaluable for figuring out the mess and understanding why it is the way it is.
2) Ask questions - don't be afraid of asking basic questions if you think something looks silly. More often than not there's a reason.
3) Improve the code in small chunks, but be mindful that what you consider an improvement may not be.
4) Understand that working as a team means you will need to accommodate varying levels of skill.
5) Don't blame individuals for bad code. Leave that to your superiors.
But keep this in your mind; if you make their life harder by your rewriting, then they will very likely make yours painful later on. Bring gradual, safe but immediate improvements and gain other's trust first. Then thing will get much easier next time, even someone else could do the job for you!
Joel makes it pretty clear that all codebases go through some kind of "maturing" where the quality is lacking, usually. It kind of made me change my opinion and approach to old codebases. Still, I think /u/freetime has the correct idea in not jumping into refactoring the first weeks of your job. Makes you look like a jackass (I made the same mistake at my current job). It might hurt, but I'd also advise to wait with improvements.
Make sure the full project is split into some reasonable modules with well-defined dependencies. The internal quality of a given module is less important as long at what it does and what it depends on is clear.
It can be easier and more powerful to throw out / replace than refactor / rewrite. Modularization is what enables this.
Make sure that version control is properly used (everything including configuration is under vcs, what is in production can be easily produced, commits are done in small meaningful units), builds are reproducable. These things are more important than detailed unit testing, documentation and test coverage.
Lastly. Get used to it. Most larger projects have tons of bad code.
On my own code, every time I have gone in and thought I was going to fix it by refactoring or rewrites, I slowly and sometimes tragically re-learn all of the edge cases I was writing around and frustrating things I discovered when I was writing it in the first place. There may be non-obvious problems regarding other peoples’ APIs, for instance, that necessitate methods which appear to be poor practice from the outside.
The only difference in my case is that i have to add more functionality to the project, i started by trying to refactor and clean the code, after a while i realized that this is almost impossible and has no visible benefits from the higher managers's point of view. So i adapted my self to write spaghetti code like the one i am facing, just to meet the deadline, and i am planning to get the hell out from here next week (resign).
Edit: definitely kick the tires a bit by seeing if this situation is typical for the company, or if it's an outlier.
I work in a similar company in the past, and having tests / a build server is step 1 to empower other to refactor more. The lack of tests is usually what brings people to paralysis / creep / huge tech debt ime.
And don't forget that beautiful code is not actually necessary for business. What is needed is delivering value to your users in a consistent manner. Well structured code will be needed if the project is here to last in the long run, but it is not always the case.
As far as I can read, you're doing great. Keep it going and other are likely to look up on you in the future, and you'll grow a lot.
If you stay: After 3 months you should have a good feel for the project. After 6 months you should be able to regularly make small contributions.
After 9 months: Spend 10% of your time documenting the problems you see and the changes that might solve those problems. Spend 10% of your time planning another work project that is unrelated to the main project. Spend 20% of your time honing a skill you want to learn. Spend the remaining 60% doing the work you were hired for.
After a year, if you feel the same way, discuss with mgmt + senior the problems you’ve been documenting and if the changes you suggest are something that should be implemented.
Iterate.
When basic things are not being done right, it feels almost demeaning to try to start a conversation with someone that the variable name "stuff" might not be descriptive.
(0) Learn how they do things there, and why, and gain the respect of your coworkers by getting really good at it. You won't find you can make much progress along any axis without people perceiving you as already being successful.
(1) Write all the unit tests you possibly can for as much of the codebase as you possibly can, and try and get the rest of the team onboard with a solid testing philosophy. Extoll designing for testing and testability.
(2) Use these to confidently move through the codebase and replace portions as necessary.
This is the 'replacing the engine while the car is on the freeway' approach.
Remeber, code is the embodiment of business rules. Often business are not aware of the rules until after thing work incorrectly. Non-beautiful odd can be the result of programmers dealing with the messy realities of trying to satisfy various stakeholders who are at odds with each other and can't articulate what they need.
If you're much younger than this other developer both professionally and biologically, then just fall in line, to be frank. Do your best. Try to help them out. Always be learning. Eventually maybe they'll move on, or you'll find a better opportunity. But don't under any circumstances badmouth anyone, or try to undermine anyone, or get into an it's-me-or-them situation, these things will only set you back.
I think if you are in the position of asking your manager for permission to refactor or rewrite or whatever, the problem has been framed incorrectly. Managers are for helping people work together, and helping them understand what is important to the rest of the company, and helping the rest of the company understand what you are doing, and helping you understand how to succeed at the company, and... many other things along those lines. They're not for telling you what buttons you're allowed to push on your computer.
Your high level goal should not be to own the code, but rather the problem that the code solves. Then you are in charge of deciding whether to refactor the existing code, or make a fresh start, or something in between. But you have to make the right decision. As a professional, you take responsibility for doing the right thing given the context in which you operate -- the state of the business and the people working around you and of course the existing systems in place. As a professional, you are trustworthy and trusted. You write code when it is appropriate, solving the most important problem at hand in an efficient and maintainable fashion. You account for both short term and long term priorities when making decisions. You communicate thoroughly. You don't make big mistakes, and you own your small mistakes and fix them as soon as possible.
In your career decisions, you should think strategically and follow a path that leads where you want to go. Will this role help you become a mature professional? Do you have mentors to learn from? Will you have a balance of maintenance and greenfield development? Do you see people working there who have healthy levels of autonomy/mastery/purpose?
So overall, just keep your chin up and do your best.
But damn how do they not have tests.
Don't be afraid to pragmatically say "this code is dogshit". But don't ever expect a full or even a large rewrite. But you can move the bar forward one piece at a time.
If you have the flexibility, just go do it. As the phrase goes, better to ask for forgiveness than permission.
2. Document the existing codebase
3. Start writing unit test and/or integration test for the existing codebase
4. Adopt a mindset of ZERO tolerance for defects making it to production
5. Begin at the lowest layer of the codebase (usually data access) and execute refactorings there to either encapsulate areas of potential change or business domains
6. Once the data access has been refactored to "good enough" work on the next layer up and continue this process to the top, this should enable you to have an architecture that is closed and decoupled.
Then concentrate on slowly but surely cover & refactor to support improvements
Much safer than a rewrite IMHO
You wouldn't be correct in saying that. That person delivered a project that works. In the beginning of a project, that's all that matters.
>I'm so frustrated and depressed by it.
Should you feel like that? It sounds like you're working to make things better.
This Simon Sinek interview about millennials in the workplace might resonate with your situation: https://www.youtube.com/watch?v=hER0Qp6QJNU
Then the author leaves, and management assigns someone else to take over the code, and he/she flails for months. It seems so inefficient. Management prioritizes shipping features over long-term maintainability and tests.
Are comments considered "old school"?
It's possible to rewrite but you incur risk when you do. Add tests and then refactor. Measure twice, cut once.
Or move. It's possible the person hates what they wrote and just escaped. But do the best job you can while you're looking for a better role. And focus on business value.
Learn to enjoy the feeling of thriving in a mess. Don't let yourself become fragile by it. Try to slowly turn that frustration into appreciation that you're growing as an engineer. Being an efficient, competent and likable worker even around ambiguity, spaghetti code, pressure and less-than-optimal communication will make you a rich and happy engineer.
The industry is much more than just hard skills. Build trust and improve as you go.
Even the worst projects tend to have logging. You can hook into that, and whilst it's not great, you can test that correct log messages are made.
The TDD book, does in fact step you through examples with no tests and show you how to hook them in.
It isn't quite as hopeless as you think it is. In fact this is the kind of system I quite enjoy working on.
Depending on the company, I would just keep my mouth shut. Specifically in corporate environments, where ass-kissing gets you promoted faster than skill level. I've experience this time and time again. I'll go into a company, realize the code base is trash, I'll say something about, and then not couple months later I'll be let go.
1. You rewrite the code. You said you would hate that, so not an option 2. You develop on top of the spaghetti code. You said you would hate that.
So, no matter how, you would hate working at this job.
That's why the only option left for you is really telling your boss how things are and that you can't see yourself working on this project.
You'll probably receive an offer with a 50% salary increase, so you can take that, but you should have a new job lined up already at another companyy.
Chances are, the code is a mess because nobody who knew better was empowered or motivated to advocate for a better way, but you have a hidden opportunity to be that hero.
Good luck!
I'm sure there's some counterexamples, where a rewrite was the right thing to do. But be wary.
If you change the code, you become implicitly part-responsible for it. And if you change complex code that does not have tests, you will break something.
Yes and no. Let's be honest, we all rewrite (i.e., refactor). It's just that we do it to our own code.
Long to short, this might not be what you enjoy but it has the potential to be a great learning experience. Doing code autopsies can teach you more than you realize.
If you were to apply TDD to this, you would begin by writing a test verifying existing code, then you will piece-by-piece rewrite it and use your tests to make sure you didn't break stuff.
Yea, so you need to write some tests before you worry about that.
Fixing tech debt on a large mission critical code base requires years of work. If you stay a year often the best you can hope for is to leave them behind a decent set of tests.
You have no idea of the conditions and constraints this person was working under. You don't know if you would be able to do a better job given the same conditions (spec, schedule etc.). It is possible they did a bad job. It is also possible the did a very good job.
And if you have the option to rewrite it and you think you can do a good job, go for it and good luck!
Also meditation is good. Sometimes I get so upset at legacy code that I have to stop and meditate.
Enhance, clean, iterate. Inspire the other devs to write cleaner code by leading by example.
i have news for you, it’s like this at all companies.
you can’t fix it. it grew that way from the people there. you need to outnumber them 2:1 to make headway.
grin and bear it. fix what you can along the way. trust me, the next guy will consider your code garbage. no matter how good it is!
Does this product work? if so, its doing its job. It may not be pretty, it may not be maintainable, but these concerns are orthogonal to its value.
1) As tempting as it is, DO NOT refactor the code right now. Do the minimal amount of work needed to complete the task.
2) Start etching out some time with your manager/tech-lead to start writing unit tests for the code.
3) Gradually refactor code that is unit tested.
To change the work I suggest communicating with the people around you:
Talk to the previous developer, explain him/her what anti patterns are, why testing is important and perhaps venture into things like solid design or design patterns. Is he/she willing to listen/learn? Then he/she might also assist you in communication towards management. If not, then I personally wouldn't go to too much length to save his/her ass.
Communicate to management and talk their language as much as needed. Talk about what the cost of maintenance is and why finding bugs in production is so much more costly then while writing unit tests. Talk about the cost of change in this versus a well designed and tested system.
Then decide with management and hopefully the other dev what to do.
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
Write the tests first, that is your first job. Make changes after that.
https://github.com/NARKOZ/hacker-scripts
;) thank me later
Response from whom? The previous developer?
It's called "Therapeutic Refactoring" by Katrina Owen:
Perform small, incremental refactoring.
Spend 2/3rds of your time adding / updating tests.
(If you can,) involve the original author in code reviews.
Figure out what the original author did right. If the project appears to work, something was done right. Make sure to maintain the right design patterns, libraries, frameworks, testing tools, ect, as long as you can.
Honestly, refactoring is my favorite part of programming. Taking something that "almost" works and turning it into something that's industrial strength and maintainable is what gives me the most professional joy.
(Edit) The last time I was in this situation, the product had very visible problems. I had to put my foot down and declare that they had to be fixed, because no one would use our product if they weren't fixed. If you can point to a problem, like hard scalability limits that management agrees will become an obstacle to company growth, that's your way to get buy-in for refactoring.
If it works, don't fix it.
Literally everything (and I mean everything) in the old implementation was a global variable that would just be recycled throughout the runtime of the program. Excluding loop counters, you could count the number of localized variables on two hands. The call-and-response serial communication implementation was a pseudo-state machine scattered across a shitload of global variables, and it ran entirely in the UI thread. When variables didn't have useless names, they would often have the same name as another variable, just with the capitalization changed.
Software development is hell.
To make things more fun there rarely is a function with less than a 1000 LOC, written in a Winforms Button_Click handler. Less than 10 functions I've seen take parameters and return a value, almost all rely on globals being set and set some themselves in return.
After the manager and main author of the software asked me to build a new UI and leave the underlying code untouched I decided to leave the company and work on someone elses messy code
I can't imagine why he thought that was a good idea.
> 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.
Yep
> 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).
CACE principal at work.
> I started "repairing" the project but there're no tests to check whether my adjustments are correct.
Start writing tests, start motivating other contributors to write tests. Focus on the code that matters.
> I'm so frustrated and depressed by it. The job basically turned into something I hate - I'm just rewriting the code!
You're going to need to work through this and either change your mindset about this or start looking for a new job. I challenge you to do the former and not the latter. This is how 90% of the projects you're going to jump into are going to be. The real test will be whether or not you can thread the needle and make valuable improvements to the code while also delivering business needs.
You can't let this job get you down. I know... it's your vocation, but most of the time people treat their job like a sweet paycheck. Instead you're going to need to treat this as a challenge if you want to get through it. You've identified the problems, now you can either continue to be negative and wallow (bitch) or, attempt to offer solutions to fix them.
The challenge is not so much technical as it is cultural. You'll need to be able to communicate the value of the changes that you want to make. You'll need to change the mindset of the stakeholders of the people on the project to let them know why integration tests are not unit tests. Why, separation of concerns is important and how you plan on doing that. Otherwise, you won't be able to create change and this is the cultural challenge.
The bad news is that this isn't necessarily the exact job you signed up for as an engineer. The good news is that you'll be able to build new valuable skills that are often highly sought after. If you feel like you're a "better engineer" than the current engineer that built it, then you need to help elevate that engineer and the other engineers around them. Otherwise if you become depressive or negative on the job, you're going to have a major negative impact on the rest of the team.
> 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.
Welcome to business life. This is how it's going to be a lot. I'm sorry.
> 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.
No, don't go in and blame the person who wrote it. You always need to "consider the context". It's easy to point at someone's code and call it shit, or to say that a project needs to be rewritten. It's harder to reverse engineer the "why" of how it got to where it is. If you can identify the "why" then you'll have more insight as to how you may be able to fix the problem or address the aspects that may have caused the problem.
> I've already made some comments about rewriting it and the response was basically "ok".
"ok" or in other words, "how are you going to deliver business value for our team". Show them data, show them why having easy to maintain code delivers business value, then people's ears will prick up.
It's your job to manage this problem. If you have the necessary insights, it's on you to fix that. Otherwise move on and don't let it kill you. I know, my job is very important to me as well, you can't make yourself miserable over something like this. It will be far too prevalent in this field.
First you need to discover all associated business requirements. You need to create all the missing tests, but you cannot do that without knowledge of the business requirements. Get these requirements and get them in writing. Find them. If you cannot do this stop. Stop now.
> I'm so frustrated and depressed by it.
This is ultimately the difference between a newb, a mid, a senior, and a super duper ninjitsu rockstar.
A newb will just accept bad code and work on top of it with their own flavor of bad code. A newb doesn't know any better and everything is frustrating all the time even if the code were awesome.
A mid has just enough experience to be dangerous. Sometimes a mid suffers from Dunning-Kruger Effect in that their personal perception of the existing code isn't how they would do it and their way is soooooo superior. The mid level developers who don't suck prioritize tests and security above everything else. They know they aren't fabulous architects, but they also have some idea of where code applies known bad practices and known best practices and can work from there. A good mid isn't going to rewrite the code base, because the risk is too high. Instead they will make the most minor of best practice corrections confined to the areas they have to touch anyways.
A good senior understands concepts like automation, graceful degradation, progressive enhancement, scope, recursion, and so forth. They know how to make the code work for them. It isn't about writing a few lines of code to solve a single business requirement. It is about writing a few extra lines of code to allow the current business requirement to scale to the next thing so less work must be performed in the future and less regression occurs when the business direction shifts.
The super awesome developer takes automation to a whole new level. Documentation is automated and therefore always up to date and accurate. Debugging tools are already embedded as part of the validation process so that when validation fails you spend less time on research or resolution. Dependencies are few and well managed, because nothing pisses people off more than corruption from outside the code you own. In the end top developers value the time of other developers more than anything else and will write their code accordingly. It isn't so much about the readability of the code as it is speed of maintenance cycles moving forward.
If your job is actually important to you then you will find an acceptable level of risk that you are willing to own and have the time to practice against. Getting this right shows that you care to do the right thing without making an ass of yourself at the company's expense.
TLDR: this is hard
The biggest mistake people make (yeah, I made it too) is to quickly loose credibility with stakeholders by not providing visible value or by increasing failure rate.
Before you even start the project, you need to communicate with stakeholders and discuss what are their problems and how you are going to address them.
You also need to get very familiar with the codebase. You need to understand various patterns that are hurting it, what is their effect (are they just cosmetic problem, are they hurting reliability? maintainability?). You then need to create a step by step plan that will address those problems starting with removing the problems that are causing biggest issues, are prerequisites to resolving other issues and are least risky to deal with.
Remember, on any project like that you are spending your trust you have with the stakeholders. The more trust you have and the less expenditure rate the longer you will be able to work with.
Try to figure out how you can bring some real visible improvements at the very start of the project to build your credibility and then a string of even small successes along the line so that stakeholders are kept in positive mood.
Plan hard to avoid failures as the mood may change quickly to "why are we even doing this" when stakeholders are in hot water with their clients.
Choose KPIs and measure them throughout the project to give visible feed on your improvements to your managers.
On the technical side, it will be critical to have good development process. Automate delivery mercilessly early on -- this is key to keep failure rate in check and allow you to work at required velocity but more importantly, it is a visible improvement that you can show to your customer to build your credit of trust.
Touch the code as little as possible. Only try to fix the big problems that you have identified. If you start messing around too much you may introduce failure when fixing stuff that wasn't worth the risk and you also risk alienating other people that are working with the code base. Remember, you are making everything more difficult for them already, don't make it more difficult than necessary.
My most recent project was a mess of hundreds of components, 400k of extremely low quality code. Instead of jumping right away into fixing the code I have spent time talking to stakeholders and to what their problems were (not what I was thinking). I then quickly addressed their problems. The existing team wasn't prioritizing it highly because they didn't even know it was important... I then observed that with the existing development process I will never be able to deliver this many changes with acceptable failure rate, so I started by working on automating build, deployment and testing. Early on I identified that the high complexity of the project meant people were having hard time doing anything reliably. So I quickly prioritized reducing complexity over fixing bad architectural patterns. This helped my peers working on the code and further helped me fuel positive image of the change.
> I started "repairing" the project
Don't. Unless your immediate supervisor requests you to do the repairs. These type of efforts largely go un-noticed or un-rewarded, and only cause you stress because you care a lot about the work and the codebase. Sometimes, you even get punished for trying to do the repairs.
> I'm so frustrated and depressed by it
I know it's hard, but try not to take it to heart. There's a life outside of work, and don't neglect that at the cost of your work ethics. Have to leave you with this quote from https://workplace.stackexchange.com/a/83319/12527
> There is an incredibly rich world out there outside of work, and my sense is that all too many people get caught up in what happens within the office walls, at the expense of simply being, living in, discovering and enjoying this world. There have been some interesting survey studies of people on their death beds, and what they wish they had done more of when they were younger and healthier. Hardly anyone says "I wish I had spent more time in the office." I keep this in mind every day on my way to work, and it helps me keep my internal compass focused on the things that really matter, so that I don't regret not dedicating enough of myself to non-work-related pursuits years from now, when it may be too late.
RELATED: Try to achieve the 'Zen-like' state that the dude in office space gets into, when he get hynotized and does't snap out of it. https://www.youtube.com/watch?v=Dp7EUUVdrt0
No, because it’s not true. This person was operating under constraints you don’t understand with incomplete and constantly changing requirements. You would not have done better in that situation.
Other professionals do not stab each other in the back either. You will never hear doctors or lawyers do it. If you have a beef with this person keep it behind closed doors.
Normally, unit tests would be the solution, but my ticket spans more than 10 modules (files), some of them suffering from the God Class syndrome. It's infeasible for 1 person with only moderate experience in unit tests to write enough tests before he's fired for not showing results. But it made me realize how important having unit tests in place is, and I made a resolution to write them for my future tickets spanning a single module.
Graphviz uses a super simple DOT notation that is perhaps best explained through showing the gallery. If you click on a graph image, it takes you directly to the source. https://www.graphviz.org/gallery/
With a big enough graph, GUI tools like Dia or Astah.net just don't cut it anymore. Graphviz lets you focus on the only thing you care about - content, and the visualization algorithm takes care of positioning. Because the format is a text file, it's a very good fit for git, you can attach a modified graph to each git commit.
Graphviz is also a great fit for vim. For example, to color all edges leading to particular node, you can type:
:%s/-> ParticularNode/-> ParticularNode [color=red]/gc
Or you can manipulate lists of nodes or edges with VISUAL LINE select mode and type :sort. Lots of possibilities. It's easy to extend a graph copying and pasting modules (subgraphs with cluster_ prefix ) or anything else, and it supports attribute inheritance so you can cut down on redundant lines. Just declare a node style, for example:
node [label="update"] update0; update1; update2; update3; update4; node [label="create"] create0; create1; create2; create3; node [label="\N"] // this resets node labels to default behavior
This way you can have the cookie and eat it. All methods of endpoints are uniformly named, but it's not ambiguous which one is which.
And the kicker - graphviz was initially developed by AT&T, so you have a BIG NAME to back up your tool choice.