home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / ada / 3614 next >
Encoding:
Internet Message Format  |  1992-12-11  |  5.4 KB

  1. Path: sparky!uunet!mcsun!Germany.EU.net!ira.uka.de!chx400!sicsun!disuns2!lglsun!nebbe
  2. From: nebbe@lglsun.epfl.ch (Robb Nebbe)
  3. Newsgroups: comp.lang.ada
  4. Subject: Re: FORTRAN bug(was Re: C++ vs. Ada -- Is Ada loosing?)
  5. Message-ID: <1992Dec11.163811@lglsun.epfl.ch>
  6. Date: 11 Dec 92 15:38:11 GMT
  7. References: <EACHUS.92Dec7184734@oddjob.mitre.org> <1992Dec8.072300.21473@smds.com> <1992Dec8.172551.16780@newshost.lanl.gov> <1992Dec9.060218.23940@seas.gwu.edu> <1992Dec11.132942.24054@mksol.dseg.ti.com>
  8. Sender: news@disuns2.epfl.ch
  9. Organization: Ecole Polytechnique Federale de Lausanne
  10. Lines: 125
  11. Nntp-Posting-Host: lglsun6.epfl.ch
  12.  
  13. Michael Feldman:
  14.  
  15.    int x;
  16.    ...
  17.    x = 1;
  18.    while (x <= 10);
  19.    { 
  20.       printf("%d\n", x);
  21.       x++;
  22.    }
  23.  
  24. For 10 points on your grade: what is printed? Why?
  25. Try explaining it to a freshman.
  26.  
  27.  
  28. Fred McCall:
  29.  
  30. Nothing is printed.  You built an infinite loop.  This is hard to
  31. understand?  You made a while loop with an empty body and a condition
  32. that is never met because you never increment x.  Your freshmen don't
  33. get this?
  34.  
  35. Me:
  36.  
  37. Good syntax means that code should do what it looks like it should do. The
  38. above code is an example of less than optimal bordering on poor language
  39. syntax. The reason being that many compilers don't detect that what is
  40. obviously wrong code is wrong. On top of that the problem is rather inocuous
  41. and if I didn't know that the problem was there I might miss it when reading
  42. through the code. Basically the presence of a typo must be detected by having
  43. your program hang. A far from optimal situation.
  44.  
  45. The far worse problem with C syntax is the one that caused the problem for
  46. ATT. (This is all second hand so anyone in the know should correct me if I'm
  47. wrong.) A programmer had left out a break in a switch statement. I don't
  48. know the what the actual code was like by a switch looks like this.
  49.  
  50.    switch (x)
  51.    {
  52.       case 1:
  53.          printf("x == 1\n");
  54.          break;
  55.       case 2:
  56.          printf("x == 2\n");
  57.          break;
  58.       default:
  59.          printf("x != 1 && x != 2\n");
  60.    } 
  61.    printf("done");
  62.  
  63. The break is necessary to leave the switch because the cases only serve as
  64. labels and execution will fall through to the next case statement. This is a
  65. case (no pun) where assembler shows through the C syntax. The machine code that
  66. is generated for the switch is a jump table based on the value of x. At the end
  67. of the statements another jump is required to avoid executing the rest of the
  68. code from the other case statements. 
  69.  
  70. The fact that these problems exist in C is well known and most C texts point
  71. them out. I would like to point out that these idiosyncracies have not
  72. prevented C from being succesfully used in a phenomenal amount of code.
  73.  
  74. People need to put C in perspective. When people started using C it was 
  75. because the choice was between assembler and C for many projects. For larger
  76. projects the choice was between something like FORTRAN with some assembler
  77. and C. My experience in mixing assembler and FORTRAN is very limited but it is
  78. far from a trivial endevour. In both cases I would unhesitatingly choose C.
  79.  
  80. Now there are other choices such as Ada for doing programs that interface with
  81. the hardware. Why do people keep using C? A lot of it has to do with inertia.
  82. Another reason that we shouldn't forget is that a good C/C++ programmer can
  83. turn out some very high quality code and if it aint broke don't fix it. In 
  84. fact I would go as far as to say that well written C/C++ code is almost as
  85. readable as Ada.
  86.  
  87. The problem with Ada is that it is a lot more difficult to do code generation
  88. in my head ;-) Some of you will scoff but my education is in computer
  89. engineering (thus hardware) and there is a tremendous amount a satisfaction in
  90. knowing what it is that I am telling the hardware to do and not just in the
  91. abstract sense.
  92.  
  93. When I program in Ada this sense of satisfaction is just not there. Programming
  94. in C is sort of like making a piece of furniture yourself from scratch and
  95. programming in Ada is like buying a kit. 
  96.  
  97. However I put such personal feelings aside when I have to do a project because
  98. my level of confidence in 5000 lines of Ada is definitely higher than my
  99. level of confidence in 5000 lines of C/C++. This isn't that I'm a better
  100. programmer in Ada than in C/C++. To the contrary I'm sure that I master C
  101. better than Ada but I probably know Ada better than C++. The reason is that
  102. when I write code in Ada it usually won't compile if it wasn't what I meant.
  103. In C/C++ the compiler isn't as helpful. For novice Ada programmers however this
  104. is viewed as frusterating because the compiler won't except their code and it
  105. is what they mean :^)
  106.  
  107. Michael Feldman:
  108. |> >Any similar idiosyncracies in Ada? 
  109.  
  110. Fred McCall:
  111. |> 
  112. |> Single character ones?  Probably not, but that just indicates that Ada
  113. |> is incredibly more verbose than C.  One of the things I DISlike about
  114. |> it, by the way.
  115.  
  116. Me:
  117.  
  118. Actually the fact that Ada is verbose has absolutly nothing to do with the
  119. fact that it doesn't have any similar idiosyncracies. The difference is that
  120. the syntax of Ada was studied to avoid such problems and this wasn't a 
  121. concern when they thought up the syntax for C. If they designers of C had been
  122. interested in avoiding such problems it wouldn't have been too hard.
  123.  
  124. Robb Nebbe                nebbe@lglsun.epfl.ch
  125.  
  126. P.S. If you don't put the breaks in the switch it would print the following
  127. if x == 1:
  128.  
  129. x == 1
  130. x == 2
  131. x != 1 && x != 2
  132. done
  133.  
  134. with breaks and x == 1 it works as one would expect printing
  135.  
  136. x == 1
  137. done
  138.