r/programmerchat • • 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.

24 Upvotes

38 comments sorted by

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.

4

u/SkippyDeluxe May 28 '15

Yes, very true! You're right, I ignored the harder problem. To be honest, I can't think of a way to do this in any language I'm familiar with. It seems like there might be a way to express such a thing in a dependently typed language (where types can depend on values), but that's above my pay grade. :)

Unfortunately no matter how fancy your type system is, at some point it will become either overly cumbersome or just plain impossible to represent all the invariants of your program as types. At that point it becomes a question of executing an effective tradeoff, expressing as much as possible in the type system and encapsulating the runtime checks so that calling code doesn't need to know they exist. For example, we might have a CompleteDeck type representing a deck that contains exactly one of each card. Given this type we can provide functions e.g. shuffle of type CompleteDeck -> CompleteDeck (a function that takes a complete deck as input and produces a new deck as output). Callers are guaranteed (statically) that shuffling a complete deck will preserve "complete-ness", but our implementations of CompleteDeck, shuffle, etc. still need to be careful about preserving internal invariants. (Similar solutions are possible in OO languages although in my experience it's much less idiomatic in those languages to use such fine-grained types).

3

u/Ghopper21 May 29 '15

You bring up the word invariant -- one of the best programming concepts -- now that I think about it I'm surprised there aren't more language features letting you express desired invariants directly. I mean there are types and type-like behavior, but not really about data. Maybe the functional side of things deals with this. If so, this may be what gets me to finally learn about real functional programming.

3

u/bamfg May 29 '15

Real functional programming is 100% worth learning about regardless! Try searching for "dependent types" - lots of functional languages have this to some degree.

I see Idris thrown around a lot when discussing dependent types but I haven't investigated it thoroughly yet. Have a look here for an idea

2

u/Ghopper21 May 29 '15

No time to read it all but the first par of that link definitely whets my appetite:

This month, we dig deeper into dependent types. Dependent types were introduced in an earlier article in this series as a richer language of types, allowing us to explain more about what a program was doing, by encoding structural information and proofs about properties. I closed the previous article promising that we could replace some (maybe all) of the measures required to gain confidence in the word wrap code by using such a richer language, specifically replacing tests and hand-waving assertions with firm statements of what properties and details we expect to hold and back them up with proofs.

Thanks!

1

u/Ghopper21 May 30 '15

The Eric Lippert AMA and our discussion here got me to learn more about the "design by contract" concept from Eiffel and the C# code contracts implementation of that idea -- which in fact includes a way to express invariants directly on a type, by tagging certain methods as being invariants which will then tested by the compiler automatically. Cool stuff.

1

u/kevindamm Jun 08 '15

Except there are actually times when you might want to represent a deck with 5 or more aces of spades, such as simulating the deals at a multi-deck blackjack table in a casino...

Of course, in real life they would probably be a matrix representation of a Markov Chain. But, if you wanted to be sure that only one instance of each card existed, yeah, you wouldn't be able to do so with any collection of card representations, you would have to model the state as the permutation of the 52 cards and their locations (in hand, draw pile, etc.) in such a way that no invalid states are allowed, but that would be very game-dependent and possibly intractable.