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
4
u/Ghopper21 May 28 '15
Nice explanation.
But the example only prevents having a Fourteen of Kittens card, not 5 Aces of Spades, right? That kind of check would typically be in some kind of hand/deck-level validation, i.e. every time the hand/deck is touched, check all constraints, reject change or throw exception if not. I'm curious are there languages/tools where you can declaratively say "there shouldn't be more than 4 of each rank" and have that be automatically enforced? Akin to how databases can enforce uniqueness and other constraints.