The best way to do it is by asking questions and showing your colleague the respect you would want if the situation were reversed. By asking questions instead of immediately criticising their bad code, you prompt them to self-evaluate their coding practices.
The Main Signs of Bad Codeit is difficult to make sense of it; it has unnecessary iterations and complexities; the function name is unclear to show the task it solves; if complemented with something, code can become inoperable.
A bad code is when a programmer or coder do program to get things done faster without thinking much about future changes and ignoring the possibility of other developers touching the code. Hard to read and understand: The first characteristic of bad code is that nobody else understands it fast.
General tip:
if (it is working)
Do (not touch it);
else
{
Make (as few modifications as possible)
Or (it will stop working at all);
}
This is experience of generations.
Michael Feathers wrote a good book about this subject exactly.
Working Effectively with Legacy Code.
Another great book is by Martin Fowler, Kent Beck and others:
Refactoring: Improving the Design of Existing Code.
Refactoring needs the safety harness of a unit test suite to remove that "Have I broken it?" feeling. Covering the bad code in a blanket of tests will help you whilst you strive for good clean code.
Pex is a tool that I find useful (if you are in the .NET world) for creating tests for legacy code.
Legacy code == code without tests!
Kindness,
Dan
When I have to deal with adding functionality to bad code, my usual approach is:
That give you at least some confidence that you didn't break everything. As for how to learn coping with bad code, I guess it's just about experience.
Well, if you're going to refactor large amounts of code in a project I'd recommend using some decent version control, so you can branch and fall back easily. Given, this is probably is an open door, but crucial imo.
Furthermore, before you start getting in to complex OO, try breaking methods and functions into smaller ones. Ensuring some level of atomicity in each of the functions, which makes the code alot easier to maintain, read and manage. It's all about the small stuff, break it down into logical units of operation, I'm doing a refactor action on a 1k lines method. It does al sorts of fancy stuff. My first goal is to get as much stuff out of it in smaller pieces, when that's done I'll start thinking about a better OO design, which is much easier because I've a much better grasp on things.
Aspirin works good too.
I am currently in this situation. My approach is to answer some questions before touching the code:
This is tough work, and nobody will thank you for it. Be proud of small improvements and enjoy a good work done :)
I think that it is always good to have a general idea of how everything works in the software you are developing/improving. That's where the design documents and other documents made after or during the development process come in. I believe that if someone before you has not done proper documentation, then at least you should write a couple of lines somewhere about what you experience throughout your development process. I usually use OneNote or other stuff to write notes about what I encounter, and usually keep listing things which I feel would require refactoring. And if there is some downtime during the project, I usually go back to that list and try to improve things bit by bit.
So basically, if someone before you hasn't done it right, it would be good if at least you could help reduce the problems for any other developer who would come across the same code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With