Thursday, March 24, 2005
Embrace Code!
One of the things that formal Computer Science education taught me was that Code Is Evil.
I was primarily a self-taught programmer. I tried taking computer courses in High School, but they were very strict about taking the courses in order… you couldn’t take the Advanced Course (and learn useful languages like Pascal and COBOL) until you took the intermediate and beginner course. The Beginner Computer Course was taught by a very nice lady who only barely knew more than her students. It was almost totally useless to me, and so I quit taking the courses after that.
Now, during my self-taught years prior to college, I never learned to fear code. Oh, I would sometimes find myself reading my own code from months before – and I’d swear that I must have written it while under some form of demonic possession. So I guess I had an inkling that code was evil. But I never feared it. Back in the day, we had magazines with game programs in them – in order to play the games, you had to type them in by hand, copied out of the magazine. If you made a mistake, you HAD to understand the code well enough to find the error and fix it. The programs were in interpreted BASIC (sometimes with machine-code subroutines - now THOSE were a bear to debug!) – so the comments actually slowed down the execution. As a result, comments were few and located in specific areas where they wouldn’t slow the game down. You couldn’t afford to fear code.
Then I went to college, and received the proper, disciplined, formal training in software engineering. I learned lots of very useful stuff there, but I was also infused with an attitude that CODE IS EVIL and should be feared. All the “proper” programming practices that you are taught are designed to mitigate the Evil That Is Code. It’s all good, useful stuff, even in the real world – learning to avoid “magic numbers” (nobody looking at it later is going to have a clue why you are multiplying your results by 47.5), add lots of comments comments, good and consistent indentation style, data-driven architecture, etc. But there’s this attitude that program code is a foreign language no matter how experienced you are, and that you need to minimize the need for people (including yourself) to actually read the stuff later. There’s this huge emphasis on up-front design because going back and changing existing code later is fraught with peril.
So, years later, I worked on a “somewhat” XP (eXtreme Programming) development shop for a few months. XP’s motto is “embrace change” – it’s an agile development process that throws a lot of the stuffy conservative attitudes towards programming out the window. More than I’m comfortable with, actually – I guess I’m stuffy and conservative. But the truth is that a lot of what WAS taught at the universities (things may have changed since then… my education is over ten years out of date, so I’m a fossil) doesn’t survive the realities of modern software development.
The surprising thing to me about XP was the lack of fear of code. XP de-emphasizes comments and written documentation, as these elements become outdated but “code is always current.” The emphasis on refactoring, self-documenting code, unit testing, pair programming, lack of detailed design, and so forth are all stem from an attitude that Code Is Not Evil, Just Wild But Tamable. It’s not the enemy. The attitude I got back from XP was that programmers should not be afraid of their own code – or anyone else’s. Program code is our medium of expression, so we should dive right in and go for it. XP programmers are not afraid to jump in and completely change things in their code – in fact, it’s built right into the entire methodology – after all, change is part of XP’s motto.
I found the experience and attitude extremely healthy to me in the last year or so. I found that this attitude removed a mental barrier from my brain that had been put in place by professors and TA’s over a decade before. The biggest difference I’ve noticed is that I’m less worried about jumping in and modifying existing code, and I’m less concerned about programming to insufficient specifications (and in my career, I can’t recall any time I’ve really received “sufficient specifications – especially not in games.) My productivity has increased, and I was able to approach a complicated third-party code base (Torque) with a bit more gusto and confidence. Now, I’m still a big advocate of including lots of comments in code (code can tell someone what you are doing, but not why). I’ve started adopting a few of XP’s processes to provide a bit more of a ‘safety net’ for rapid development and change, but otherwise my programming style hasn’t changed much. Just my attitude.
I’m just embarrassed that it took me so long to stop worrying and love the code. Fear inhibits action (well, unless your flight reflex kicks in, I guess), and fear of code is a miserable impediment as a programmer. If you are just learning to program, you’ve probably heard a lot of stuff about how complex and difficult-to-understand it is. Don’t be intimidated! It will slow your learning if you have this voice in your head saying, “This is too complicated.” Throw off those shackles! Embrace Code!
Labels: programming
Comments:
Links to this post:
<< Home
Cool blog. I think it was from you that I learned "comment on why you're doing something, not what you're doing". It's made a big difference in the ability of other people to read my butt-ugly Perl and Python code.
I used to be pedantic about commenting because I'd always heard "commenting is good". I mean, down to the "this routine takes a number X, multiplies it, and does this and that". That was stupid :) The code itself is the documentation on the process. The comments are the documentation on your thought process.
It would be interesting to work in a shop that embraced XP. Basically, I'm so embarassed about my programming style, reference manual in hand most of the time, that I think I'd be mortified to do it in front of someone else...
I used to be pedantic about commenting because I'd always heard "commenting is good". I mean, down to the "this routine takes a number X, multiplies it, and does this and that". That was stupid :) The code itself is the documentation on the process. The comments are the documentation on your thought process.
It would be interesting to work in a shop that embraced XP. Basically, I'm so embarassed about my programming style, reference manual in hand most of the time, that I think I'd be mortified to do it in front of someone else...
But that's how you learn :) Besides, getting some peer reviews (or pair programming) can boost your confidence as well.
One of the best programming tricks I've learned is to comment first when you create a new function. Create the comments explaining what you intend to do, and the general algorithm on how you are going to to it (i.e. "Step 1: find the centroid of the triangle. Step 2: Create a sphere object that encompasses the triangle, using the centroid as the origin." etc.) The comments then help guide me as I move around, so I don't lose the "big picture". I fill out the code beneath the appropriate comment line. And when I'm done - viola - I have some decent comments for my code. (They have USUALLY been revised between the time they were first created and when the function is "code complete", but that's par for the course.)
One of the best programming tricks I've learned is to comment first when you create a new function. Create the comments explaining what you intend to do, and the general algorithm on how you are going to to it (i.e. "Step 1: find the centroid of the triangle. Step 2: Create a sphere object that encompasses the triangle, using the centroid as the origin." etc.) The comments then help guide me as I move around, so I don't lose the "big picture". I fill out the code beneath the appropriate comment line. And when I'm done - viola - I have some decent comments for my code. (They have USUALLY been revised between the time they were first created and when the function is "code complete", but that's par for the course.)
Back in the day, we had magazines with game programs in them – in order to play the games, you had to type them in by hand, copied out of the magazine. If you made a mistake, you HAD to understand the code well enough to find the error and fix it.
I absolutely hated typing those in, but it was worth it. (Remember when they started adding checksum codes to the listings?)
The real fun came when it was time to muck around with their code.
I absolutely hated typing those in, but it was worth it. (Remember when they started adding checksum codes to the listings?)
The real fun came when it was time to muck around with their code.
Yeah, the checksums were invaluable - especially for the machine-code bits. Though there were a couple of times I SWORE the checksums were wrong.
I learned a lot about programming from typing in those games. Two books I STILL have (and cherish) are Creative Computing's "Basic Computer Games" and "More Basic Computer Games." Those were as much a textbook for me for learning to program in BASIC as well as game programming. The biggest lesson I learned was how you could break down a similar problem in so many different ways, each way offering different strengths and weaknesses. Like how many different ways you could represent a map.
Great stuff!
Post a Comment
I learned a lot about programming from typing in those games. Two books I STILL have (and cherish) are Creative Computing's "Basic Computer Games" and "More Basic Computer Games." Those were as much a textbook for me for learning to program in BASIC as well as game programming. The biggest lesson I learned was how you could break down a similar problem in so many different ways, each way offering different strengths and weaknesses. Like how many different ways you could represent a map.
Great stuff!
Links to this post:
<< Home


