Rogue is a dungeon crawling video game first developed by Michael Toy and Glenn Wichman around 1980. It is generally credited with being the first "graphical" adventure game, and was a favorite on college Unix systems in the early to mid-1980s, in part due to the procedural generation of game content.

-- http://en.wikipedia.org/wiki/Rogue_(computer_game)

Thursday, June 17, 2010

The Changes of Style

I have always been a fan of seeing the development of ideas though common media over time. One of my favorite examples it Issac Asimov's Foundation Series. The first set of books were done in the late 1940's and then more books are added in the 1980's. What I found interesting is that both books represent more about the culture at that time then the future. How women are treated in the original trilogy is much different then in the later books. But they reflect the notions of the time they were written.

With respect to programming we have the same thing. Coding style and thought has changed quite a bit even over my 25 some odd years of exposure. And none has changed so much as C and all its offspring.

K&R is the book on C programing, at least according to programing purists (or gorgnards). It is authored by those that did the primary development of the language so they should know what they are talking about. One interesting note about K&R is the code examples. On one hand, they are correct in that they do what they say. They very concisely solve the problem at hand. But conciseness in coding isn't necessarily a virtue today.

Back in the day, a programmer flexed his muscles by writing a program in the fewest lines (or even characters) as possible. What you would get were these curious little nuggets of brilliance. Yet in the long run not very useful. Part of this came out of the days when lines were done on punch cards and so "smaller" programs were more efficient. This is also back when people "wrote programs" and not "develop software." Software development was done back then, but primarily from large firms like IBM or the DoD. Then again, back in the 1970's and early 80's large firms were the primary consumers of Software. Personal Computers and Gaming Consoles starting bringing software to the masses.

Now there is a whole field on the process of developing software. And as such, concise and clever coding is not as much an asset in its own right anymore. Take this little piece of code from K&R (p 115):

while ( --argc > 0)
printf("%s%s", *++argv, (argc > 1) ? " " : "");
printf("\n");
return 0;


This is a simple echo command. Except for the main() and {}, that is it. It is elegant and concise. And if I were a Project Manager and a coder gave me this bit of code, I would pat them on the head, give them a treat for "whose a clever programmer?" then beat them until they re-wrote the code with out too many operations happening in one line. Granted if the project were at the "extreme code tuning phase" where you need to pull out all your tricks, the beating may be a little less sever, but at the very least some better formatting and not manipulating argv and argc directly would be preferred.

For example. A "good" programmer knows the difference between i++ and ++i. Sure on a line by themselves they do the same thing. But if in the above code we had argc-- and *argv++ the output would be incorrect. The program name itself would be included along with the rest of the command line, it may try to print an undefined argv.

Today, you need to write software so that less qualified programmers could understand it and support/change it after you are done. Most prevailing thoughts on style, readability, and supportability would at the very least insist on moving all increment and decrement operations to their own lines. The embedded conditional in the printf is questionable. It is small enough to be quickly understood, but there is an opinion that you should not embed any arithmetic in your function calls. I personally would leave it in there. Unless that same computational result is needed elsewhere.

My style itself is changing. I started off doing procedural programing in Pascal over 25 years ago. In college I learned FORTRAN and studied mathematics so the use of i, j, and k as iterators and vector notation was just natural. C++ was just still in development and not fully standardized. Object Orient Programing was a nice idea, but at the time too much overhead to do properly. Now a days, both compilers and hardware have gotten to the point where the extra overhead can minimized or even eliminated.

My code has gotten more verbose. I use longer variable names and take time to create relatively good class structures. One thing I am finding out is a lot of the common problems have been solved and I don't really need to write them myself anymore. For example, C++ has a standard Double Linked List implementation that has been tuned over the years by many people. I can now just say "Make me a list of foo" and boom, I have everything I need.

0 comments:

Post a Comment