From: nebbe@lglsun.epfl.ch (Robb Nebbe)
Newsgroups: comp.lang.ada
Subject: Re: FORTRAN bug(was Re: C++ vs. Ada -- Is Ada loosing?)
Date: 11 Dec 92 15:38:11 GMT
Organization: Ecole Polytechnique Federale de Lausanne

Michael Feldman:

   int x;
   ...
   x = 1;
   while (x <= 10);
   { 
      printf("%d\n", x);
      x++;
   }

For 10 points on your grade: what is printed? Why?
Try explaining it to a freshman.


Fred McCall:
 
Nothing is printed.  You built an infinite loop.  This is hard to
understand?  You made a while loop with an empty body and a condition
that is never met because you never increment x.  Your freshmen don't
get this?

Me:

Good syntax means that code should do what it looks like it should do. The
above code is an example of less than optimal bordering on poor language
syntax. The reason being that many compilers don't detect that what is
obviously wrong code is wrong. On top of that the problem is rather inocuous
and if I didn't know that the problem was there I might miss it when reading
through the code. Basically the presence of a typo must be detected by having
your program hang. A far from optimal situation.

The far worse problem with C syntax is the one that caused the problem for
ATT. (This is all second hand so anyone in the know should correct me if I'm
wrong.) A programmer had left out a break in a switch statement. I don't
know the what the actual code was like by a switch looks like this.

   switch (x)
   {
      case 1:
         printf("x == 1\n");
         break;
      case 2:
         printf("x == 2\n");
         break;
      default:
         printf("x != 1 && x != 2\n");
   } 
   printf("done");

The break is necessary to leave the switch because the cases only serve as
labels and execution will fall through to the next case statement. This is a
case (no pun) where assembler shows through the C syntax. The machine code that
is generated for the switch is a jump table based on the value of x. At the end
of the statements another jump is required to avoid executing the rest of the
code from the other case statements. 

The fact that these problems exist in C is well known and most C texts point
them out. I would like to point out that these idiosyncracies have not
prevented C from being succesfully used in a phenomenal amount of code.

People need to put C in perspective. When people started using C it was 
because the choice was between assembler and C for many projects. For larger
projects the choice was between something like FORTRAN with some assembler
and C. My experience in mixing assembler and FORTRAN is very limited but it is
far from a trivial endevour. In both cases I would unhesitatingly choose C.

Now there are other choices such as Ada for doing programs that interface with
the hardware. Why do people keep using C? A lot of it has to do with inertia.
Another reason that we shouldn't forget is that a good C/C++ programmer can
turn out some very high quality code and if it aint broke don't fix it. In 
fact I would go as far as to say that well written C/C++ code is almost as
readable as Ada.

The problem with Ada is that it is a lot more difficult to do code generation
in my head ;-) Some of you will scoff but my education is in computer
engineering (thus hardware) and there is a tremendous amount a satisfaction in
knowing what it is that I am telling the hardware to do and not just in the
abstract sense.

When I program in Ada this sense of satisfaction is just not there. Programming
in C is sort of like making a piece of furniture yourself from scratch and
programming in Ada is like buying a kit. 

However I put such personal feelings aside when I have to do a project because
my level of confidence in 5000 lines of Ada is definitely higher than my
level of confidence in 5000 lines of C/C++. This isn't that I'm a better
programmer in Ada than in C/C++. To the contrary I'm sure that I master C
better than Ada but I probably know Ada better than C++. The reason is that
when I write code in Ada it usually won't compile if it wasn't what I meant.
In C/C++ the compiler isn't as helpful. For novice Ada programmers however this
is viewed as frusterating because the compiler won't except their code and it
is what they mean :^)

Michael Feldman:
|> >Any similar idiosyncracies in Ada? 

Fred McCall:
|> 
|> Single character ones?  Probably not, but that just indicates that Ada
|> is incredibly more verbose than C.  One of the things I DISlike about
|> it, by the way.

Me:

Actually the fact that Ada is verbose has absolutly nothing to do with the
fact that it doesn't have any similar idiosyncracies. The difference is that
the syntax of Ada was studied to avoid such problems and this wasn't a 
concern when they thought up the syntax for C. If they designers of C had been
interested in avoiding such problems it wouldn't have been too hard.

Robb Nebbe                nebbe@lglsun.epfl.ch

P.S. If you don't put the breaks in the switch it would print the following
if x == 1:

x == 1
x == 2
x != 1 && x != 2
done

with breaks and x == 1 it works as one would expect printing

x == 1
done
Newsgroups: comp.lang.ada