home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / os / msdos / programm / 11679 < prev    next >
Encoding:
Internet Message Format  |  1992-12-29  |  14.5 KB

  1. Xref: sparky comp.os.msdos.programmer:11679 comp.sys.ibm.pc.programmer:700 comp.lang.c++:18502 comp.lang.c:18978
  2. Path: sparky!uunet!usc!cs.utexas.edu!uwm.edu!linac!att!att!allegra!alice!bs
  3. From: bs@alice.att.com (Bjarne Stroustrup)
  4. Newsgroups: comp.os.msdos.programmer,comp.sys.ibm.pc.programmer,comp.lang.c++,comp.lang.c
  5. Subject: Re: Newbie Wants Advice on C-Programming
  6. Summary: general comments on learning C++
  7. Message-ID: <24506@alice.att.com>
  8. Date: 29 Dec 92 17:52:53 GMT
  9. Article-I.D.: alice.24506
  10. References: <1992Dec25.050515.20871@ncsu.edu> <1992Dec25.073517.16089@grebyn.com> <1992Dec29.003005.27059@ucc.su.OZ.AU>
  11. Organization: AT&T Bell Laboratories, Murray Hill NJ
  12. Lines: 288
  13.  
  14.  
  15.  
  16. There has - under various headings - been several related discussions
  17. about the proper way to learn C++, C++'s relation to C, C++'s relation
  18. to Smalltalk, the difference (or not) between data abstraction and
  19. object-oriented programming, etc.
  20.  
  21.  
  22. I think the practical concern underlying many of these discussions is:
  23.  
  24.     Given that I don't have much time to learn new techniques
  25.     and concepts, how do I start using C++ effectively?
  26.  
  27. It is clear that to use C++ ``best'' in an arbitrary situation you
  28. need a deep understanding of many concepts and techniques, but that
  29. can only be achieved through years of study and experiments. It is
  30. little help to tell a novice (a novice with C++, typically not a
  31. novice with programming in general), first to gain a thorough
  32. understanding of C, Smalltalk, CLOS, Pascal, ML, Eiffel, assembler,
  33. capability based systems, OODMBSs, program verification techniques,
  34. etc., and then apply the lessons learned to C++ on his or her next
  35. project. All of those topics are worthy of study and would - in the
  36. long run - help, but practical programmers (and students) cannot
  37. take years off from whatever they are doing for a comprehensive
  38. study of programming languages and techniques. 
  39.  
  40. On the other hand, most novices understand that ``a little knowledge
  41. is a dangerous thing'' and would like some assurance that the little
  42. they can afford time to learn before/while starting their next project
  43. will be of help and not a distraction or a hinderance to the success
  44. of that project. They would also like to be confident that the little
  45. new they can absorb immediately can be part of a path that can lead
  46. to the more comprehensive understanding actually desired rather than
  47. an isolated skill leading nowhere further.
  48.  
  49. Naturally, more than one approach can fulfill these criteria and
  50. exactly which to choose depends on the individual's background,
  51. immediate needs, and the time available. I think many educators,
  52. trainers, and posters to the net underestimate the imporatance
  53. of this: after all, it appears so much more cost effective - and
  54. easier - to ``educate'' people in large batches rather than
  55. bothering with individuals.
  56.  
  57. Consider a few common questions:
  58.  
  59.     I don't know C or C++, should I learn C first?
  60.  
  61.     I want to do OOP, should I learn Smalltalk before C++?
  62.  
  63.     Should I start using C++ as an OOPL or as a better C?
  64.  
  65.     How long does it take to learn C++?
  66.  
  67. I don't claim to have the only answers ``the (only) right answers''
  68. to these questions. As I said the ``right'' answer depends on the
  69. circumstances. Most C++ textbook writers, teachers, and programmers
  70. have their own answers. For example, I seem to remember that the C++
  71. FAQ discusses these questions. My answers are based on years of
  72. programming in C++ and other languages, teaching short C++ design
  73. and programming courses (mainly to professional programmers),
  74. consulting about to introduction of and use of C++, discussing C++,
  75. and generally thinking about programming, design, and C++.
  76.  
  77.     I don't know C or C++, should I learn C first?
  78.  
  79. No. Learn C++ first. The C subset of C++ is easier to learn for C/C++
  80. novices and easier to use than C itself. The reason is that
  81. C++ provides better guarantees than C (stronger type checking).
  82. In addition, C++ provides many minor features, such as the `new'
  83. operator, that are notationally more convenient and less error-prone
  84. than their C alternatives. Thus, if you plan to learn C and C++ (or
  85. just C++) you shouldn't take the detour through C. To use C well,
  86. you need to know tricks and techniques that aren't anywhere near
  87. as important or common in C++ as they are in C. Good C textbooks
  88. tends (reasonably enough) to emphasize the techniques that you
  89. will need for completing major projects in C. Good C++ textbooks,
  90. on the other hand, emphasizes techniques and features that lead
  91. to the use of C++ for data abstraction and object-oriented programming.
  92. Knowing the C++ constructs, their (lower-level) C alternatives are
  93. trivially learned (if necessary).
  94.  
  95. To show my inclinations:
  96.  
  97.     To learn C use:
  98.  
  99.         Kernighan and Ritchie:
  100.         The C programming Language (2nd edition)
  101.         Prentice Hall, 1988.
  102.  
  103.     as the primary textbook. To learn C++ use
  104.  
  105.         Stroustrup:
  106.         The C++ programming Language (2nd edition)
  107.         Addison Wesley, 1991.
  108.  
  109. Both books have the advantage of combining a tutorial presentation
  110. of language features and techniques with a complete reference manual.
  111. Both describes their respective languages rather than particular
  112. implementations and neither attempts to describe particular libraries
  113. shipped with particular implementations.
  114.  
  115. There are many other good textbooks and many other styles of
  116. presentation, but these are my favorites for comprehension
  117. of concepts and styles. It is always wise to look carefully
  118. at at least two sources of information to compensate for bias
  119. and possible shortcommings.
  120.  
  121.  
  122.  
  123.     I want to do OOP, should I learn Smalltalk before C++?
  124.  
  125. No. If you plan to use C++, learn C++. Languages such as C++, Smalltalk,
  126. Simula, CLOS, Eiffel, etc., each has their own view of the key notions
  127. of abstraction and inheritance and each support them in slightly different
  128. ways to support different notions of design. Learning Smalltalk will
  129. certainly teach you valuable lessons, but it will not teach you how to
  130. write programs in C++. In fact, unless you have the time to learn and
  131. digest both the Smalltalk and the C++ concepts and techniques, using
  132. Smalltalk as a learning tool can lead to poor C++ designs.
  133.  
  134. Naturally, learning both C++ and Smalltalk so that you can draw from
  135. a wider field of experience and examples is the ideal, but people
  136. who haven't taken the time to digest all the new ideas often end up
  137. ``writing Smalltalk in C++'' that is applying Smalltalk design notions
  138. that doesn't fit well in C++. This can be as sub-optimal writing C or
  139. Fortran in C++.
  140.  
  141. One reason often quoted for learning Smalltalk is that it is ``pure''
  142. and thus force people to think and program ``object oriented.''
  143. I will not go into the discussion about ``purity'' beyond mentioning
  144. that I think that a general purpose programming language ought to
  145. and can support more than one programming style (``paradigm'').
  146.  
  147. The point here is that styles that are appropriate and well
  148. supported in Smalltalk are not necessarily appropriate for C++.
  149. In particular, a slavish following of Smalltalk style in C++
  150. leads to inefficient, ugly, and hard to maintain C++ programs.
  151. The reason is that good C++ requires design that takes advantage
  152. of C++'s static type system rather than fights it. Smalltalk
  153. support a dynamic type system (only) and that view translated
  154. into C++ leads to extensive unsafe and ugly casting.
  155.  
  156. I consider most casts in C++ programs signs of poor design. Some
  157. casts are essential, but most aren't. In my experience, old-time
  158. C programmers using C++ and C++ programmers introduced to OOP
  159. through Smalltalk are among the heaviest users of casts of the
  160. kind that could have been avoided by more careful design.
  161.  
  162. In addition, Smalltalk encourages people to see inheritance as
  163. the sole or at least primary way of organizing programs and to
  164. organize classes into single-rooted hierarchies. In C++, classes
  165. are types and inheritance is by no means the only means of organizing
  166. programs. In particular, templates is the primary means for
  167. representing container classes.
  168.  
  169. I am also deeply suspicious of arguments proclaiming the need to FORCE
  170. people to write in an object-oriented style. People who don't want to
  171. learn can, on average, not be taught with reasonable effort and there
  172. is in my experience no shortage of people who DO want to learn. Unless
  173. you manage to demonstrate the principle behind data abstraction and
  174. object-oriented programming all you'll get is inappropriate ``barouque''
  175. misuses of the language features that support these notions - in C++,
  176. Smalltalk, or whatever.
  177.  
  178. See ``The C++ Programming (2nd Edition)'' and in particular Chapter 12
  179. for a more thorough discussion of the relation between C++ language
  180. features and design.
  181.  
  182.  
  183.  
  184.     Should I start using C++ as an OOPL or as a better C?
  185.  
  186. That depends. Why do you want to start using C++? The answer to that
  187. question ought to determine the way you approach C++; not some
  188. one-size-fits-all philosophy. In my experience the safest bet is
  189. to learn C++ ``bottom up,'' that is first learn the features C++
  190. provides for traditional procedural programming, the ``better C''
  191. sub-set, then learn to use and appreciate the data abstraction features,
  192. and then learn to use class hierarchies to organize sets of related
  193. classes.
  194.  
  195. It is - in my opinion - dangerous to rush through the earlier stages
  196. because there is too high a probability of missing some key point.
  197.  
  198. For example, an experience C programmer might consider the ``better
  199. C'' subset of C ``well known'' and skip the 100 pages or so of a
  200. textbook that describes it. However, in doing so he might miss the
  201. ability to overload functions, the difference between initialization
  202. and assignment, the use of the `new' operator for allocation, the
  203. explanation of references, or some other minor feature in such a way
  204. that it will come back to haunt him at a later stage where sufficient
  205. new concepts are in play to complicate matters. If the concepts used
  206. in the better C subset are known the 100 pages will only take a couple
  207. of hours to learn and some details will be interesting and useful.
  208. If not, the time spent is essential.
  209.  
  210. Some people have expressed fear that this ``gradual approach'' leads
  211. people to write in C style forever. This is of course a possible
  212. outcome, but nowhere as likely as proponents of ``pure'' languages
  213. and proponents of the use of ``force'' in teaching programming like
  214. to believe. The key thing to realize is that using C++ well as a
  215. data abstraction and/or object-oriented language requires the
  216. understanding of a few new concepts that have no direct counterpart
  217. in languages such as C and Pascal.
  218.  
  219. C++ isn't just a new syntax for expressing the same old ideas -
  220. at least not for most programmers. This implies a need for education,
  221. rather than mere training. New concepts have to be learned and
  222. mastered through practice. Old and well-tried habits of work have
  223. to be re-evaluated, and rather than dashing of doing things ``the
  224. good old way'' new ways have to be considered - and often doing
  225. things a new way will be harder and more time-consuming than
  226. the old way - when tried for the first time.
  227.  
  228. The overwhelming experience is that taking the time and making the
  229. effort to learn the key data abstraction and object-oriented techniques
  230. is worth while for almost all programmers and yields benefits not
  231. just in the very long run but also on a three to twelve month timescale.
  232. There are benefits in using C++ without making this effort, but most
  233. benefits requires the extra effort to learn new concepts - I would
  234. wonder why anyone not willing to make that effort would switch to C++.
  235.  
  236. When approaching C++ for the first time, or for the first time after
  237. some time, take the time to read a good textbook or a few well chosen
  238. articles (the C++ Report and the C++ Journal contain many). Maybe also
  239. have a look at the definition or the source code of some major library
  240. and consider the techniques and concepts used. This is also a good idea
  241. for people who has used C++ for some time. Many could do with a review
  242. of the concepts and techniques. Much has happened to C++ and its associated
  243. programming and design techniques since C++ first appeared. A quick
  244. comparison of the 1st and the 2nd edition of ``The C++ Programming
  245. Language'' should convince anyone of that.
  246.  
  247.  
  248.  
  249.     How long does it take to learn C++?
  250.  
  251. Again, that depends. It depends both on your experience and on what
  252. you mean by ``learning C++.'' The syntax and basics for writing C++
  253. in the better C style plus defining and using a few simple classes
  254. takes a week or two for a programmer. That's the easy part. The main
  255. difficulty, and the main fun and gain comes from mastering new design
  256. and programming techniques. Most experienced programmers I have talked
  257. with quotes times from a half year to one and a half year for becomming
  258. really comfortable with C++ and the key data abstraction and object-
  259. oriented techniques it supports. That assumes that they learn on the
  260. job and stay productive - usually by programming in a ``less adventurous''
  261. style of C++ during that period. If one could devote full time to
  262. learning C++ one would be comfortable faster, but without actual
  263. application of the new ideas on real projects that degree of comfort
  264. could be misleading. Object-oriented programming and object-oriented
  265. design are essentially practical - rather then theoretical - disciplines.
  266. Unapplied, or applied only to toy examples, these ideas can become
  267. dangerous ``religions.''
  268.  
  269. Note that learning C++ is then primarily leaning programming and design
  270. techniques, not language details. Having worked through a good textbook
  271. I would suggest a book on design such as
  272.  
  273.         Grady Booch:
  274.         Object Oriented Design with examples
  275.         Benjamin Cummings 1990.
  276.  
  277. which has the nice property of having longish examples in five different
  278. languages (Ada, CLOS, C++, Smalltalk, and Object Pascal) and is therefore
  279. somewhat immune to the language biogotry that mar some design discussions.
  280. The parts of the book I like best is the presentation the design concepts
  281. and the example chapters.
  282.  
  283. Looking at design contrasts sharply with the approach  of looking very
  284. carefully at the details of the definition of C++ - usually using the ARM
  285.  
  286.         Ellist&Stroustrup:
  287.         The Annotated C++ Reference Manual
  288.         Addison-Wesley, 1990
  289.  
  290. which is a book containing much useful information, but no information
  291. about how to write C++ programs. A focus on details can be very distracting
  292. and lead to poor use of the language. You wouldn't try to learn a foreign
  293. language from a dictionary and grammar, would you?
  294.  
  295. When learning C++, it is essential to keep the key design notions in mind
  296. so that one doesn't get lost in the language technical details. That done,
  297. learning and using C++ can be both fun and productive. A little C++ can
  298. lead to significant benefits compared to C, further efforts to understand
  299. data abstraction and object-oriented techniques yields further benefits.
  300.  
  301.     - Bjarne Stroustrup
  302.