Thursday, August 13, 2009

Impressions

Today I'm adapting a script some other guy wrote, so that we can use it for our own purposes.

I always find it interesting to read other people's computer code. Scripting is a relatively new skillset for me, and there's always a lot I can learn from their approaches to problems.

I was going to say that his script looks pretty tight, overall, but after looking at it some more I've decided that it isn't, really. In fact, in a lot of ways it's really loose. More on that in moment.

He does use comments more liberally than I do, and they really do help me follow along with his train of thought.

On the other hand, he has an annoying tendency to name his variables things like "variable1", and "variable2". Or "value1" and "value2". Or even "variable_first" and "variable_second". This makes it very hard to follow along with his train of thought. Me, I believe that as much as possible, variable names should give some clear indication of what that variable is for. That way, whenever you see it, you know what it's up to.

On the third hand, he used a "case" loop, which I wasn't familiar with before today. It threw me at first. I was going to reach for my reference books, but decided to finish giving the script a once-over before doing any research. Further along, I realized that it was basically just an "if" loop, with the added advantage of being much easier to read. I'm quite comfortable with "if" loops, but I plan to start using "case" loops wherever possible. Anything that makes it easier for me to read and understand my own code is a bonus!

One of the most interesting things is that he has a lot of useless code, mostly commented out, so that it doesn't actually run. Code written with certain assumptions in mind, that later turned out to be untrue in certain environments. Code that has been replaced by temporary workarounds to some vexing problem the original code couldn't handle. A large, complex function--the bulk of his script, in fact--is never called because it has a show-stopping bug in it somewhere.

Me? That would creep me out, having all that dead code in there. One of my friends--an actual professional software developer--tells me that whenever he's adapting someone else's code, he always removes dead code, so that he's left with only the tight, live script that actually does the job.

No comments:

Post a Comment