?

Log in

No account? Create an account

Peter Rogers's Blog
Artist-in-Residence at Chez Firth

Thursday (4/17/14) 2:11am - ... wherein Peter wonders what simple, basic programming guides are out there.

So there's a diet book out there called Eat This, Not That, which shows various unhealthy foods, and lists healthier alternatives to each.

I'm wondering if there is a similar resource for programming.  It would be especially helpful if there were a blog out there along the lines of Code This, Not That.  It could have entries like this:

The Bad Code:

TurnObjectBlue(object) {
        colorset = object.getColorSet();
        colorset.SetForegroundColor(BuiltInColors.blue);
}

TurnObjectRed(object) {
        colorset = object.getColorSet();
        colorset.SetForegroundColor(BuiltInColors.red);
}


The Good Code:

SetObjectColor(object, color) {
        colorset = object.getColorSet();
        colorset.SetForegroundColor(color);
}


What was the Problem?
The problem was, someone has copied code.  Odds are, they wrote TurnObjectBlue, copy-pasted the whole routine, and then changed two words to make the new TurnObjectRed function.

This is hard to maintain because you have to maintain multiple copies of the same code.  Say you want to log every time the object's color gets set.  In the bad code, this means you have to update TurnObjectBlue *and* update TurnObjectRed -- and even then, we're assuming there is no TurnObjectGreen lurking out there to trip you up.

This is hard to read because conceptually, it's telling you the wrong story.  In English, this code is saying, "Here's how you turn an object blue.  And here's the *completely different* mechanism for turning an object red."  Maintainers will assume there must be some important difference between the two -- "it can't just be that they're using a different color, right?" -- and pore over the code looking for differences.  (Maybe this examination is trivial in this case, but imagine if the functions were each 50 lines long!)

Instead, when you find yourself writing two functions with similar-but-not-identical functionality, find a way to implement both in one function that only switches out the bits that vary.
Or, like that, only better.  Writing Idiomatic Python follows this general format, but it's specifically geared towards the quirks of Python.

I feel like, in the freelancing world, I've come across a lot of people who have picked up coding by just getting out there and doing it -- and that's great, but it never teaches you these basic fundamentals.  No "learn Go in 10 hours" book will teach you the basics of encapsulation, and no CSS-tips blog will tell you why you shouldn't use globals everywhere.  So it would be nice both to have a language-agnostic resource that hackers can learn from, and a reference that I can link to -- for example, in a changelist where I collapse several nearly-identical functions into a single one, I could link to the above post in the comments.  The hacker who asks "why did Peter change my code?" could go to the link and learn something new.

So: does a resource like this exist?

Tags:
Mood: [mood icon] contemplative
Previous Entry Share Next Entry