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
2
u/[deleted] May 28 '15 edited May 28 '15
Out's OK. You shouldn't be using it all the time, because you shouldn't need it very often, but it's the correct solution when you need to return more than one thing and there's not enough of a use case to justify a full-fledged class to encapsulate the return values. (An out parameter on a void method is a definite code smell, though.)
I think multiple out parameters is a bit troubling, mostly because, at some point, I'd want to know why you didn't produce a small class or return a Tuple.
If you only need to return one 'thing', use a return type, never an out.