Writing Software – Part 1

Ever since I started programming for Linux I’ve been thinking about writing about the differences between writing software for Windows and writing software for Linux. In the process of thinking about it many other programming-related thoughts started coming up and I couldn’t quite categorize them into sections (like Windows, Object Oriented Programming, etc…). This is why I decided to write this series, in which I will share some of my thoughts about and experiences with aspects of writing software.

Let me tell you about my first experience with programming, as a kid. When I was about 6 or 7 years old, my older brother got an Apple compatible computer. These kind of computers only existed for a little while, as Apple sued compatible Apple computers manufacturers and the rest of the story is well known (IBM compatibles outsell Apple’s computers to the point of near extinction).

The programming language available for me back then was the BASIC programming language and so my first program was:

10 PRINT "amit"
20 GOTO 10

Then, the uber-hacker version of this piece of code replaced the first line with:

10 ? "amit"

Looking back at this I wonder why it was decided that “?” can be used instead of “PRINT”. It doesn’t make too much sense from the write-clear-code point of view. I mean, they could have gone all the way, like Brainf*ck (or Linguine) did.

Ah, the good old, misunderstood and now forbidden GOTO. Where have you gone to? Today merely mentioning goto in a job interview is sure to get you another one of those we’re-sorry-but-you’re-over-qualified letters. GOTO is such a nasty word that, in the beginning of an introductory C course in the Technion, the teacher said that he will not teach us what goto is and a student who will use it in a homework assignment will automatically get a zero grade for that assignment. And by the way, if the interview is already going bad, make sure you say something like “when goto isn’t enough, I simply use longjmp()“.

In Java, goto is a reserved word, but it is not implemented. That’s the zombie land of language keywords. The reason for the deprecation of goto started with the famous article “Go To Statement Considered Harmful” by Edsger W. Dijkstra. The misuse of this statement was just too much to bear. The thing is that goto was still useful, for example in the case where you wanted to do some cleanup at the end of the function, or maybe some common error handling code. Many uses of goto use can be found in libraries (A.K.A the place where you can do stuff that is generally not allowed as long as it works) which is an indication that it is still useful. Actually, there’s a thumb rule I learned once that prevents the spaghetti code which usually results from using goto: always “go to” in one direction. It’s a simple and effective rule, which means that if you use goto in a function, always make sure that the goto target is, for example, below the current line.

Alas, goto is already doomed. Modern languages offer “structured” ways for doing cleanup. In Java and in C# (I will use C# as the representative for .NET languages) you can use the try-finally combo, which is part of exception-handling. And if we’re already on the subject of exception handling, let me tell you about my “real world” experience with C++ and exceptions.

Exceptions in “real world” C++ are rarely used. Most of the reasons I heard were the usual FUD – exceptions are unexpectable, exception handling is slow, exceptions are too complicated. This is true for three out of three C++-using companies I worked for. Ultimately this means that the code has the call-check-trace-return pattern, which is something like this:

STATUS FunctionA()
{
  STATUS ret = STATUS_ERROR;
  ret = FunctionB();
  if (STATUS_OK != ret)
  {
    TRACE_ERROR(ret, "I called FunctionB and it failed. Boo hoo");
    return ret;
  }
  
  ... // repeated for evey call inside FunctionA
  
  return STATUS_OK;
}

Those of you who use exceptions can see the irony. Naturally, it means that constructors are not allowed to do any work (so there’s always a Construct() function for a class, immediately following the constructor), which apparently is one of the five habits of “highly profitable developers”. In an environement that embraces that kind of attitude there’s no room even for mentioning RAII, pimpl, Loki or even boost. Sometimes I wonder if the C++ I read in books will ever come useful to me at a workplace.

Mentioning Loki and boost brings me to the subject of templates, which is very similar to exceptions, but I’ll talk about that some other time. Oh, and I have to continue the story about how I started programming.

4 Replies to “Writing Software – Part 1”

  1. 1. As a side remark, Dijkstra had many important papers, and he was a pioneer in concurrent systems (not only algorithms for shortest path…).
    2. Your thumb rule works only when there are no recursions, from any kind.
    3. What is an Exception? :)

  2. 1. I didn’t know that. Anyway, her was pretty much right about goto as well.
    2. It’s funny that you mentioned recursions because that’s abother thing that is not allowed in production code in many companies (again, this is true for every company I worked for)
    3. The fact that you wrote Exception with a capital E exposes you ;)

  3. “pretty much”??? Just to give you an analogy with something that you are familiar with: saying that Dijkstra *was pretty much right about* something in CS, is like saying that Stroustrup pretty much knows how to program… (no offence to my friends at Bell Labs)

Comments are closed.