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.
View all comments
Show parent comment
1
u/fosforsvenne May 28 '15
I never thought about this before, but I guess if a method has a side effect it's better for it to be void with an out parameter than to return a value. The first should make you realize that the method does something else than give you the value while the latter doesn't.
Oh right, I forgot about tuples. I guess they're cleaner than out. However, in my admittedly non-noteworthy opinion there's no more reason to make structs/classes for a few values than for just one. Having an RGB struct with three values for each color is definitely nicer than three different bytes, but a meter struct with one value is also nicer than just a double.