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.

25 Upvotes

38 comments sorted by

View all comments

Show parent comment

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!