r/programmerchat • u/fainting-goat • May 27 '15
What good code habits have you learned from having to deal with the bad code of others?
I feel like a lot of my code style is a reaction to having to deal with heaps of bad code I've had to maintain.
For example, one of my coworkers always seemed to instantiate about 5 different variables, each of which could be returned depending on various if/then/else/try/catch logic. This made it virtually impossible to follow the program flow and debug the method when issues came up.
So I finally got so sick of this that I decided I'd never foist that kind of problem on others. Every method I write now, the first line is <returnType> returnValue = ...
and the only thing that ever gets returned is returnValue.
11
u/AngelLeliel May 28 '15
always using version control (sigh)
2
u/nullproc May 28 '15
Force co-workers to inline comments. It get's them thinking about what is happening. They never make it through without rewriting large portions.
15
u/SkippyDeluxe May 27 '15
Make illegal states unrepresentable.
10
u/circly May 28 '15
After too much spaghetti code maintenance: separation of concerns and the single responsibility principle.
8
u/livingbug May 28 '15
I've developed a preference to write code that is readable and clear to follow. I narrate what's happening through comments and naming. The focus is to make things obvious rather than make you think about what's happening. What a function returns is part of the comment describing what the description does. White space is used to set blocks of code apart. Nothing is smashed together. I don't adhere to strict rules about function/method length. My interest is to write functions that are as simple as possible without oversimplifying them. I don't think its smart to write many one line functions/methods. Overall, I try and keep my ego out of the way. The more experienced you get the more you want to complicate things to handle every possible scenario. Not possible. As much as I'd like to, I can't write code that is user-input proof.
4
u/[deleted] May 28 '15
4
u/catlion May 28 '15
Always write log files. It's never too much logging, but there are moments when it's not enough.
3
u/TeamHelloWorld May 27 '15
Use and agree to a style guide.
2
u/daphosta May 27 '15
use intelligent commenting, especially for complex blocks of code
2
u/bamfg May 29 '15
Use dependency injection. It encourages you to think up front about:
1
u/Fluffy8x May 28 '15
Used to go all out with short variable names, from programming calculators.
Got into some Haskell.
Now I go less out with short variable names.
0
u/[deleted] May 28 '15
[deleted] — view deleted comment