Article Discussion
Elegance and Other Design Ideals
Summary: Bjarne Stroustrup talks with Bill Venners about many aspects of software design, including growing small applications into larger ones, avoiding class distinctions between designers and users, the dangers of premature generalization, and the essence of elegance.
35 posts.
The ability to add new comments in this discussion is temporarily disabled.
Most recent reply: March 16, 2004 9:54 AM by Eric
    Bill
     
    Posts: 409 / Nickname: bv / Registered: January 17, 2002 4:28 PM
    Elegance and Other Design Ideals
    February 22, 2004 9:00 PM      
    Bjarne Stroustrup talks with Bill Venners about many aspects of software design, including growing small applications into larger ones, avoiding class distinctions between designers and users, the dangers of premature generalization, and the essence of elegance.

    Read this Artima.com interview with the creator of C++:

    http://www.artima.com/intv/elegance.html
    • Matt
       
      Posts: 62 / Nickname: matt / Registered: February 6, 2002 7:27 AM
      Re: Elegance and Other Design Ideals
      February 24, 2004 6:20 PM      
      Bjarne Stroustrup: The problem with C++ and GUI is not that there's no C++ GUI library, for instance. The problem is that there are about 25 reasonable C++ GUI libraries, of which a few are rather good. But people say, "Well, there's no C++ GUI. It's not in the standard." C++ has no common meeting place like there is for Python, for example, where you know you can find libraries. It's a problem of riches, plurality, and a lack of marketing.

      Funny thing about this comment is that Python has the same problem in this case, though perhaps not on the same scale. There are several GUI libraries to choose from, which is in some ways worse than only one choice...

      Bjarne Stroustrup: That's the new cast syntax. ... But the new syntax was made deliberately ugly, because casting is still an ugly and often unsafe operation.

      This is interesting, because it also contrasts a bit with the desire to get people to adopt the new method. I remember in many case specifically choosing to use the old-style casts with thier concomitant shortcomings over the new style, just because the new style was so ugly.

      Bjarne Stroustrup: One of the things that amazes people is when you compare good C++ code to good code in other languages, the C++ code tends to be shorter.

      So I guess COM code doesn't qualify. I think that one of the things that really hoses up elegance in C++ is macro abuse. COM and MFC are rife with macros and while I have been obliged to use them both, it has always been with distaste. Even ATL is not very elegant. Of course, COM can be used in a clean and simple way from VB (or better, Python or C#, etc.) and it could be argued that that was the original intent, but I see no reason why it wouldn't also be possible to make the C++ usage elegant, as well.
      • Kristian
         
        Posts: 4 / Nickname: chryler / Registered: December 2, 2003 5:09 AM
        Re: Elegance and Other Design Ideals
        February 25, 2004 6:06 AM      
        >So I guess COM code doesn't qualify.

        I think we can all agree that it most certainly does not.
        The Comet Library (http://www.lambdasoft.dk/) makes an attempt at C++'ifying COM. I don't know how well it does it as I don't use it but it sure looks better than plain COM.
      • Bjarne
         
        Posts: 48 / Nickname: bjarne / Registered: October 17, 2003 3:32 AM
        Re: Elegance and Other Design Ideals
        February 25, 2004 9:10 AM      
        > Bjarne Stroustrup: One of the things that amazes
        > people is when you compare good C++ code to good code in
        > other languages, the C++ code tends to be shorter.

        >
        > So I guess COM code doesn't qualify.

        Definitely not. Unfortunately, many have the curious notion that COM, or the styles popularized by MS/COM, is what C++ is, or what C++ is supposed to be.

        > I think that one of
        > the things that really hoses up elegance in C++ is macro
        > abuse. COM and MFC are rife with macros and while I have
        > been obliged to use them both, it has always been with
        > distaste.

        I use macros only to control #ifndef to deal with platform dependencies. All else is unnecessary *unless* you have to (or simply decides to) *directly* deal with a seriously misdesigned interface (rather than excapsulating it).

        > Even ATL is not very elegant. Of course, COM
        > can be used in a clean and simple way from VB (or better,
        > Python or C#, etc.) and it could be argued that that was
        > the original intent, but I see no reason why it wouldn't
        > also be possible to make the C++ usage elegant, as well.

        Neither do I. However, cleaning up an existing major mess is one of the hardest tasks there is. As long as users insists "doing things the old way" (because that feels comfortable because they are used to it) or "being able to doing every trick we could do before", the old stuff will pervasively affect the new.

        Better examples of more modern style can be found on www.boost.org.

        -- Bjarne Stroustrup: http://www.research.att.com/~bs
    • Steven
       
      Posts: 1 / Nickname: pkdick / Registered: February 26, 2004 8:07 AM
      Re: Abstraction and Efficiency
      February 26, 2004 6:56 PM      
      Hi,

      first I apologize for commenting on the "Abstraction and Efficiency" part of the article series in the "Elegance and Other Design Ideals" thread, but this discussion seems to be the one that is currently alive, and I discovered www.artima.com just today.

      >Abstraction is a mechanism by which we understand things. Expressing a solution in terms of math, for instance, means we really did understand the problem.
      >... If somebody has a theory, such as a theory for matrix manipulation, you can just work at the level of those concepts and your code will become shorter, clearer, and more likely to be correct.
      > ... The only code faster than the fastest code is no code. By abstracting to matrix manipulation operations, you give the compiler enough type information to enable it to eliminate many operations. If you were writing the code at the level of arrays, you would not eliminate those operations unless you were smarter than just about everybody.

      It is exciting to read this: I am in the field of interactive theorem proving and the above thoughts about programming pretty much reflect the feeling that you get when doing theorem proving on the computer (and probably everywhere else in mathematics): if you find the right abstraction level for a theorem, then most of the time it becomes much simpler to supply a proof for that theorem (and to formulate it right, in the first place). So that reinforces my growing belief that theorem proving and programming are very very similar.

      Concerning the efficiency of "abstract" code: Does anybody know about a programming language that accepts hints from the programmer at the chosen abstraction level (or failed attempts at such a language)? For example, when I have a class representing an equation, and I have an operation SYM that for a specific equation A=B returns the new equation B=A, then obviously SYM(SYM(x)) is just x, and I would like the compiler to take care of this. Of course I could to this myself in the source code, but if this constellation occurrs after inlining, then the only one who can do this is the compiler.

      It would be hard to imagine that in C++, because there should be somehow a way to convince the compiler that the transformation is allowed.

      Any comments?
    • Todd
       
      Posts: 27 / Nickname: tblanchard / Registered: May 11, 2003 10:11 AM
      Re: Elegance and Other Design Ideals
      February 25, 2004 9:54 AM      
      A lot of the things I call elegant you'll find are short compared to alternatives. One of the things that amazes people is when you compare good C++ code to good code in other languages, the C++ code tends to be shorter. C++ code is shorter when it's written in terms of things like the standard library or other good libraries, because you can express ideas very succinctly

      I find this terribly ironic from the designer of a language which requires implementation of at least 4 methods per class (default ctor, copy ctor, op=, dtor) as well as requiring added syntax to enable dynamic dispatch (virtual methods).
      • Matt
         
        Posts: 62 / Nickname: matt / Registered: February 6, 2002 7:27 AM
        Re: Elegance and Other Design Ideals
        February 25, 2004 11:47 AM      
        Maybe Bjarne will weigh in on this himself, but I think (perhaps, in the context of this article combined with the earlier articles in the series) that he may be refering to the implementation code that uses a set of well-designed classes. In that case, using those objects is (or can be) very clean and simple.

        I think that it makes sense that library implementations require a bit more rigor than the client code.

        By the way, none of the things you mentioned are actually required. It sounds like you are refering to one of Scott Meyers' suggestions (not to be confused with requirements of the language) from Effective C++?
      • indranil
         
        Posts: 4 / Nickname: indranilb / Registered: February 25, 2004 11:41 PM
        Re: Elegance and Other Design Ideals
        February 26, 2004 4:58 AM      
        Not really. The language doesnt require you to implement those methods, the compiler will generate default versions of these methods.

        The rule of thumb is:- if you need to write one of copy ctor, dtor or assign op, then you will almost certainly need to write the other two methods as well.

        Exactly the same thing would happen in Java. Say you had a class that created a database connection in its constructor and assigned it to a member variable. Then you would need to write a Clone method and a Dispose method. The only difference is that the user of the Java class would have to call Dispose themselves.

        As for the virtual keyword, this comes from the C++ philosophy of using only what you need. Most often I dont need dynamic dispatch for my methods , so why have the overhead of always going to the vtable? In Java you'd have to use the final keyword to disable dynamic dispatch on a method.
        • Todd
           
          Posts: 27 / Nickname: tblanchard / Registered: May 11, 2003 10:11 AM
          Re: Elegance and Other Design Ideals
          February 26, 2004 1:22 PM      
          >Not really. The language doesnt require you to implement those methods, the compiler will generate default versions of these methods.

          And 90% of the time does it badly. This compiler generation of methods as well as its over-eagerness to do type conversions via construction of temporaries, user defined conversion operators and so forth results in frequent programmer surprise. The amount of context the developer must hold in his head to understand what he is seeing is daunting. The exact same syntax will be interpreted in many different ways depending on the existence of available conversion paths.

          C++ appears to have been designed according to the principle of most surprise.
          • indranil
             
            Posts: 4 / Nickname: indranilb / Registered: February 25, 2004 11:41 PM
            Re: Elegance and Other Design Ideals
            February 26, 2004 3:00 PM      
            > And 90% of the time does it badly. This compiler
            > generation of methods as well as its over-eagerness to do
            > type conversions via construction of temporaries, user
            > defined conversion operators and so forth results in
            > frequent programmer surprise.

            Well you've changed the subject, from your original post about how much code a C++ programmer is forced to write, to something equally incorrect.

            The compiler doesnt generate any type conversion operators at all. As the name suggests 'User defined' conversions operators have to be coded explicitly by a programmer. I dont see much room for programmer surprise there. There could be unexpected conversions with single arg constructor, but we have the explicit keyword to suppress that. Personally I've never experienced the things you're complaining about
          • indranil
             
            Posts: 4 / Nickname: indranilb / Registered: February 25, 2004 11:41 PM
            Re: Elegance and Other Design Ideals
            February 26, 2004 3:32 PM      
            > >Not really. The language doesnt require you to implement
            > those methods, the compiler will generate default versions
            > of these methods.
            >
            > And 90% of the time does it badly.

            Not really. Its usually fine. As I said the rule is if you find that you need to write any one of copy constructor, assignment operator or destructor then you will almost certainly need to write the other two. This happens quite often , but considerably less than 90% of the time.

            You certainly wouldnt be immune from this in Java for example. If you needed anything more than the default shallow copying of references, then you have to provide a clone() method. If your class was responsible for managing a resource then you'd provide a dispose() or close() method.

            Not to mention the fact if you have to override equals() you must remember to override hashCode(). Dont forget to call super() in your constructor. And if you dont override toString() you really arent a professional.

            I wont even begin my rants about endless casting to get things in and out of collections or ever growing exception specifications or being forced to implement an entire Listener interface when I just want to override one method or repeating the same clean up code in every finally block in my program.
            • Todd
               
              Posts: 27 / Nickname: tblanchard / Registered: May 11, 2003 10:11 AM
              Re: Elegance and Other Design Ideals
              February 27, 2004 7:55 AM      
              You certainly wouldnt be immune from this in Java for example. If you needed anything more than the default shallow copying of references, then you have to provide a clone() method. If your class was responsible for managing a resource then you'd provide a dispose() or close() method.

              The vast majority of the need for writing copy ctors and op= involves sorting out ownership. In a GC system like Java, this problem vanishes. C++ Example:

              class Foo { Something* _c; public: Foo(Something* c) : _c(c){} ~Foo() { delete _c; }}

              Inadvertent copying of Foo (inadvertent copying in C++ happens a *lot*) will result in a double free.

              You can fix this using a reference counting pointer - but that typically requires additional support in the class you are counting and if its not your class you're stuck.
              • Merriodoc
                 
                Posts: 14 / Nickname: brandybuck / Registered: March 24, 2003 6:00 AM
                Re: Elegance and Other Design Ideals
                February 27, 2004 10:33 AM      
                > You certainly wouldnt be immune from this in Java for
                > example. If you needed anything more than the default
                > shallow copying of references, then you have to provide a
                > clone() method. If your class was responsible for managing
                > a resource then you'd provide a dispose() or close()
                > method.

                >
                > The vast majority of the need for writing copy ctors and
                > op= involves sorting out ownership. In a GC system like
                > Java, this problem vanishes. C++ Example:
                >
                > class Foo { Something* _c; public: Foo(Something* c) :
                > _c(c){} ~Foo() { delete _c; }}
                >
                > Inadvertent copying of Foo (inadvertent copying in C++
                > happens a *lot*) will result in a double free.
                >
                > You can fix this using a reference counting pointer - but
                > that typically requires additional support in the class
                > you are counting and if its not your class you're stuck.

                Since there is no
                new
                , you don't need the
                delete
                . In the initialization all that is happening when you do the member initialication is that _c is pointing to the same thing that c is. At least, that's what my debugger is showing me. In the case you have here, Foo isn't responsible for managing the memory. It's just another reference to something.

                If you wanted to do what you are talking about doing, you would have to copy the memory from c to _c. Or not call delete. Since there is no explicit memory allocation, I don't see why you would have tried to explicitly deallocate memory.

                I'm not sure what's being inadvertently copied. Other than a contrived example of something that theoretically is a problem, where would something like this show up in a production application? If you had to copy something it would be something like this:


                class Foo
                {
                Something* _c;
                public: Foo(Something* c)
                {
                _c = new Something;
                memcpy(_c, c, sizeof(c));
                }
                ~Foo() { delete _c; }
                }


                Or better yet, if it's some kind of container, you would most likely use a template. Then it would be typesafe and reusable.
                • Todd
                   
                  Posts: 27 / Nickname: tblanchard / Registered: May 11, 2003 10:11 AM
                  Missed it completely
                  February 27, 2004 0:19 PM      
                  My point that is.

                  I'm not sure what's being inadvertently copied. Other than a contrived example of something that theoretically is a problem, where would something like this show up in a production application?

                  Well of course its contrived - it has to fit on this forum. But your example is better than mine for illustrating my point.

                  class Foo
                  {
                  Something* _c;
                  public:
                  Foo(Something* c)
                  {
                  _c = new Something;
                  memcpy(_c, c, sizeof(c));
                  }
                  ~Foo() { delete _c; }
                  }

                  The memcpy of an instance is particularly evil and do you realize you are only copying a pointer sized chunk of memory?

                  However, even in this case your code will crash under this circumstance:

                  {Foo f1(new Something); { Foo f2(f1); } }

                  thanks to the compiler generated copy ctor. Equally (perhaps more) common is people forgetting to make function args take a reference which results in stack created temporaries. This is the usual cause of inadvertent copying.

                  Anyhow, I see no reason to continue to argue with a fictional character lacking a basic grasp of memory management.
                  • Merriodoc
                     
                    Posts: 14 / Nickname: brandybuck / Registered: March 24, 2003 6:00 AM
                    Re: Missed it completely
                    February 27, 2004 1:45 PM      
                    > Anyhow, I see no reason to continue to argue with a
                    > fictional character lacking a basic grasp of memory
                    > management.

                    It was a quick sample put together in about 5 seconds. Alas, I wasn't the one initially deleting memory I didn't allocate in the first place, which I would say also shows somebody lacking of a basic grasp of memory management. Or perhaps I can use your convenient handwaving you use here?

                    >Indeed, why are you deleting something you didn't >create?

                    >Perhaps an unusual creational pattern (factory or >something) is used. Its rather beyond the scope of the >example though - let us say that this class's role is as >an adopter of its constructor argument.

                    I know memcpy is evil. I also know that a 5 second example hastily thrown together (in 6 seconds it probably would have read sizeof((*c)) or something equally horrible) to prove a point is not terribly indicative of anything, as you ably demonstrated with your initial example and the further need to rationalize it above. The point being that you need to allocate something before deallocating it.

                    I don't see a reason to be unduly harsh, but hey, whatever floats your boat. I guess I should be happy that I gave you a better code snippet to prove your point than you put together?

                    I've had no experience with smalltalk, so I can't comment on how it works with copy issues, but I know Python, a dynamically typed language, has some issues that are specifically addressed by the copy module

                    http://www.python.org/doc/current/lib/module-copy.html

                    with objects containing objects and reference semantics.

                    Have a good one, Todd. Sorry to have wasted your time.
                • Matt
                   
                  Posts: 62 / Nickname: matt / Registered: February 6, 2002 7:27 AM
                  Re: Elegance and Other Design Ideals
                  February 27, 2004 0:20 PM      
                  Indeed, why are you deleting something you didn't create? Is this an example of how to create a "dumb pointer" object in C++?

                  Also, Merry, why would you use memcpy() in C++ instead of a copy contstructor?

                  Maybe both of these things illustrate a big problem that does occur in C++: many people still use a C approach in their so-called "C++" code. It is the downside of the multi-paradigm capability.

                  Once again, this may be due in large part to Microsoft's influence, as this kind of thing is especially rampant in thier libraries and example code, where about the only C++ish elements are the "//" comment syntax and the file extension. (and even the "//" comment is standard C, now, I think). I guess they just have a lot of old programmers who agreed to change their file extensions to '.cpp' but stubbornly refused to actually learn C++. The old argument for releasing libraries in plain C used to be for platform-independence (I guess), but I don't see any reason why Microsoft doesn't release their new libraries in pure C++ these days.
                  • Todd
                     
                    Posts: 27 / Nickname: tblanchard / Registered: May 11, 2003 10:11 AM
                    Re: Elegance and Other Design Ideals
                    February 27, 2004 0:31 PM      
                    Indeed, why are you deleting something you didn't create?

                    Perhaps an unusual creational pattern (factory or something) is used. Its rather beyond the scope of the example though - let us say that this class's role is as an adopter of its constructor argument.
                  • Merriodoc
                     
                    Posts: 14 / Nickname: brandybuck / Registered: March 24, 2003 6:00 AM
                    Re: Elegance and Other Design Ideals
                    February 27, 2004 4:43 PM      
                    > Indeed, why are you deleting something you didn't create?
                    > Is this an example of how to create a "dumb pointer"
                    > r" object in C++?
                    >
                    > Also, Merry, why would you use memcpy() in C++ instead of
                    > a copy contstructor?

                    Reflex. I've been going through a lot of C lately and I didn't give this a whole lot of thought. It should probably be more like

                    _c = new Something(c);

                    So yeah, I'm guilty as charged about having a C background and occasionally bring some of that over to C++. I occasionally use () around the condition in if statements in Python, too. More baggage from one language to another that isn't needed.

                    I regularly work in 4 different languages and my mind occasionally gets a little muddled.

                    That doesn't change the fact that generally speaking deleting memory you did not allocate, like in this example, is not a good practice. Personally, I think it's rather appropriate that a fictional character argue about a fictional code sample.
      • Bjarne
         
        Posts: 48 / Nickname: bjarne / Registered: October 17, 2003 3:32 AM
        Re: Elegance and Other Design Ideals
        February 25, 2004 10:34 PM      
        > A lot of the things I call elegant you'll find are
        > short compared to alternatives. One of the things that
        > amazes people is when you compare good C++ code to good
        > code in other languages, the C++ code tends to be shorter.
        > C++ code is shorter when it's written in terms of things
        > like the standard library or other good libraries, because
        > you can express ideas very succinctly

        >
        > I find this terribly ironic from the designer of a
        > language which requires implementation of at least 4
        > methods per class (default ctor, copy ctor, op=, dtor) as
        > well as requiring added syntax to enable dynamic dispatch
        > (virtual methods).

        How can you find a fact ironic?

        Have you actually looked at equivalent code in several languages? One recent example is the comparative paper on generic programming from OOPSLA last year. Another was a comparison of programs in various OO languages from about a decade ago by one the top Stepstone guys.


        -- Bjarne
        • Sakesun
           
          Posts: 8 / Nickname: sakesun / Registered: August 18, 2003 10:00 PM
          Re: Elegance and Other Design Ideals
          February 26, 2004 4:11 AM      
          >> I find this terribly ironic from the designer of a
          >> language which requires implementation of at least 4
          >> methods per class (default ctor, copy ctor, op=, dtor) as
          >> well as requiring added syntax to enable dynamic dispatch
          >> (virtual methods).

          >How can you find a fact ironic?

          Now it's even more ironic. I've once read your comment on c++ syntax that the syntax beauty depends on taste of the evaluator. When have C++ beauty now become a fact ?

          >Have you actually looked at equivalent code in several
          >languages?

          I'll be glad if you have time to give us some examples.

          >One recent example is the comparative paper on
          >generic programming from OOPSLA last year. Another was a
          >comparison of programs in various OO languages from about a
          >decade ago by one the top Stepstone guys.

          Base on what I've heard people complains about C++, I strongly believe you need more than these two evidences to convince your fact.
          • Bill
             
            Posts: 409 / Nickname: bv / Registered: January 17, 2002 4:28 PM
            Re: Elegance and Other Design Ideals
            February 26, 2004 11:20 AM      
            > Base on what I've heard people complains about C++, I
            > strongly believe you need more than these two evidences to
            > convince your fact.

            There's a comparison of how to print out 99 bottles of beer on the wall in 621 languages. Appropriately enough, it has several different versions of C++ program, each with in a different style. The length varies widely:

            http://99-bottles-of-beer.ls-la.net/c.html

            When Bjarne made the claim in the interview, I imagined him to be talking more about algorithms than program infrastructure. Bjarne, you can correct me if I was wrong about that. So for example, looking at what code -- the while loops and for loops and data structures -- it would take to implement an algorithm that does something specific. Not the access specifiers and object infrastructure you might want to put around that algorithm if it were to be part of a large system.

            I can't remember who said this and in what context, but somewhere I heard someone talking about how it is ironic that the bigger the language, the smaller the code in that language. In other words, the more features a language has in it, the more ways a programmer can say what needs to be said -- including possibly a very succinct way -- so and the smaller the programmer can make the code. I thought it was Matz who said this about Ruby in his interview, but I can't find it. Regardless, C++ has a lot of features, and perhaps that helps make code shorter.

            After one of Bjarne's talks at the JAOO conference where we did the interview, someone in the audience asked Bjarne about readability. And Bjarne's answer was that many people confuse readability with familiarity. It occurred to me that if you handed both me and Bjarne a book written in Danish and a C++ program, I would find both much harder to read than Bjarne. I think that different languages kind of aim at a different level of familiarity than others, so that's another factor in play. Languages that enable more terse code probably also assume the readers of the code are going to have spent more effort becoming familiar with all the various aspects of the language.

            I'm curious, Bjarne, if you were talking about C++ compared to scripting languages like Ruby and Python and Perl, or just C++ compared to systems languages like Java and C# and Eiffel. And if it is more about comparing just the algorithms versus an entire application. Because my experience is that at the application level, Java was less verbose than C++, and Python is less verbose than Java. Although in Python I tend to write in a structured design style, not an OO style, so it is kind of an apples to oranges comparison. Whereas I have built the same kind of OO sytems in Java that I used to do in C++. That said, I was programming in C++ long before the STL, so I was not using a modern C++ style.
            • Tommy
               
              Posts: 1 / Nickname: tompa / Registered: April 13, 2003 5:59 AM
              Re: Elegance and Other Design Ideals
              February 26, 2004 0:54 PM      
              "Although in Python I tend to write in a structured design style, not an OO style"
              Now you are making me curious ;-)
              Why is it that you tend to program in a structured style with Python?
              • Bill
                 
                Posts: 409 / Nickname: bv / Registered: January 17, 2002 4:28 PM
                Re: Elegance and Other Design Ideals
                February 26, 2004 11:20 PM      
                > "Although in Python I tend to write in a structured
                > design style, not an OO style"

                > Now you are making me curious ;-)
                > Why is it that you tend to program in a structured style
                > with Python?

                It's really just a matter of size for the most part, probably. For the kind of tasks I use Python for, objects seem like overkill. For example, the script I use to do redirects looks like this:


                import sys,os

                queryString = os.environ.get("QUERY_STRING")
                i = queryString.find('url=')
                url = queryString[i + 4:]
                print "Location: " + url + "\n\n"


                No need to make a class here. The most recent Python script I put into production parses an Apache log file and imports data into a database table. It's probably 400 lines of code, but once again objects seemed like overkill. I just have a main method that loops through each line and calls one of three or four other methods depending upon which regular expression matched, and those methods call a handful of other helper methods to do specific tasks the calling methods have in common. That's just a functional decomposition of the problem, which is what I was referring to as "structured design."

                But on the other hand, I do feel that Python, because it doesn't force me to put every method inside a class as Java does, doesn't drive me in the object-oriented direction. Java tends to push me rather firmly towards one kind of object-oriented design, which is a kind I feel is a very natural fit for building larger systems. The other installments of this interview with Bjarne Stroustrup, he talks about Java's object-oriented single-mindedness:

                http://www.artima.com/intv/goldilocks2.html

                He also talks about multiple programming styles with C++:

                http://www.artima.com/intv/modern2.html
            • Bjarne
               
              Posts: 48 / Nickname: bjarne / Registered: October 17, 2003 3:32 AM
              Re: Elegance and Other Design Ideals
              February 26, 2004 3:27 PM      
              >
              > When Bjarne made the claim in the interview, I imagined
              > him to be talking more about algorithms than program
              > infrastructure. Bjarne, you can correct me if I was wrong
              > about that. So for example, looking at what code -- the
              > while loops and for loops and data structures -- it would
              > take to implement an algorithm that does something
              > specific.

              If I remember correctly, I was thinking of programs written with adequate support from modern libraries. One related thing that I sometimes mention is that "In the language itself, just about anything is hard; with a suitable library, just about anything becomes easy - that's true for any language". Obviously, the distinction between "the language" and "a library" is sharper in some languages than in others.

              > I can't remember who said this and in what context, but
              > somewhere I heard someone talking about how it is ironic
              > that the bigger the language, the smaller the code in that
              > language. In other words, the more features a language has
              > in it, the more ways a programmer can say what needs to be
              > said -- including possibly a very succinct way -- so and
              > the smaller the programmer can make the code. I thought it
              > was Matz who said this about Ruby in his interview, but I
              > can't find it. Regardless, C++ has a lot of features, and
              > perhaps that helps make code shorter.

              There's something to that. For example, a C++ class offers support for both value types and object types. If you think all types should be object types (or conversely that all types should be value types), you'll find C++'s support overelaborate. However if you write a program that uses both kinds, say a complex number type and a graphics hierarchy, the support in C++ will favor users compared to users of a language that supports only the one or the other. Obviously, my opinion is that most programs can benefit from both kinds of types.

              > After one of Bjarne's talks at the JAOO conference where
              > we did the interview, someone in the audience asked Bjarne
              > about readability. And Bjarne's answer was that many
              > people confuse readability with familiarity.

              I think that's a separate issue. I think "shorter" is a fairly objective notion.

              > Languages that enable more
              > terse code probably also assume the readers of the code
              > are going to have spent more effort becoming familiar with
              > all the various aspects of the language.

              Maybe, but complex z2 = z2*4+z4; is readable without any training (in programming) - even if the code implementing the complex library is not.

              > I'm curious, Bjarne, if you were talking about C++
              > compared to scripting languages like Ruby and Python and
              > Perl, or just C++ compared to systems languages like Java
              > and C# and Eiffel. And if it is more about comparing just
              > the algorithms versus an entire application. Because my
              > experience is that at the application level, Java was less
              > verbose than C++, and Python is less verbose than Java.
              > Although in Python I tend to write in a structured design
              > style, not an OO style, so it is kind of an apples to
              > oranges comparison. Whereas I have built the same kind of
              > OO sytems in Java that I used to do in C++. That said, I
              > was programming in C++ long before the STL, so I was not
              > using a modern C++ style.

              I was thinking about languages such as C, C++, Java, and C#. The OOPSLA paper that I referred to elsewhere considered ML, Haskell, C++, etc. and C# and java extended with generics. The old OOPSLA experiment included languages that were popular 10 years ago, such as Smalltalk, Objective C and C++.

              I did not think of scripting languages as such. They have an inherent advantage from requiring less "boiler plate", though C++ comes out pretty well give the STL, a regular expression library (e.g. see boost).

              I did a course on distributed computing using Java, C#, and C++. I found C++ on average shorter on the algorithmic and data representation parts, but - as for all single person comparisons - that could simply have been my greater experience with C++.

              -- Bjarne Stroustrup; http://www.research.att.com/~bs
              • Bill
                 
                Posts: 409 / Nickname: bv / Registered: January 17, 2002 4:28 PM
                Re: Elegance and Other Design Ideals
                February 26, 2004 11:57 PM      
                > I did a course on distributed computing using Java, C#,
                > and C++. I found C++ on average shorter on the algorithmic
                > and data representation parts, but - as for all single
                > person comparisons - that could simply have been my
                > greater experience with C++.
                >
                This gets to the other idea I was thinking about during this exchange: the main point to take away from Bjarne's answer about elegance isn't the extent to which C++ may help you make code shorter, but that in whatever language you happen to be working, brevity is a good thing. Programming is writing in the sense that people must read your code. One of the main tenets of good writing is to avoid having lots and lots of useless words tacked on when just a few will do, including words in clauses that really just say the same thing as something that was already said earlier in the sentence, and what's worse is run-on sentences like this one that seem to never end, which become very tedious to try to read, and that's the same as code that goes on and on to say something hat could be said much more simply. As Strunk and White says in The Elements of Style:

                Vigorous writing is concise.
              • Todd
                 
                Posts: 27 / Nickname: tblanchard / Registered: May 11, 2003 10:11 AM
                Re: Elegance and Other Design Ideals
                February 27, 2004 9:02 AM      
                >I was thinking about languages such as C, C++, Java, and C#.

                Oh, well, those are all essentially the same language with subtly different runtime models.

                Except C++ has this ADT/OO dual nature.

                C++ as an OO language (objects on the heap) is probably the weakest and most error prone OO language in existence.

                But the ADT nature - (the stuff you do with custom data types/generics that are typically allocated on the stack) is rather unique and I think probably C++'s sweet spot. I think it makes it particularly good for efficient implementation of algorithms.

                Right tool for the job and all that.
        • Todd
           
          Posts: 27 / Nickname: tblanchard / Registered: May 11, 2003 10:11 AM
          Re: Elegance and Other Design Ideals
          February 26, 2004 1:16 PM      
          >How can you find a fact ironic?

          Its a claim - open to dispute.

          >Have you actually looked at equivalent code in several languages?

          I'm about equally fluent in C, C++, Smalltalk, Objective C, and Java. I used to be quite good at F77 as well but its been awhile. Of these, I find Smalltalk to be the most compact and elegant in all cases.

          If you have an online pointer to the paper in question, I would be interested in seeing it.
          • Bjarne
             
            Posts: 48 / Nickname: bjarne / Registered: October 17, 2003 3:32 AM
            Re: Elegance and Other Design Ideals
            February 26, 2004 2:25 PM      
            > >How can you find a fact ironic?
            >
            > Its a claim - open to dispute.

            "Ironic" is not an argument or a productive word in a technical discussion. It tends to inflame and distract.

            Especially, as your mention of constructors, etc. was diverting the discussion from programs to language features.

            I made an observation based on my experience. I didn't back it up with serious academic study - people rarely does, unfortunately.

            > >Have you actually looked at equivalent code in several
            > languages?
            >
            > I'm about equally fluent in C, C++, Smalltalk, Objective
            > C, and Java. I used to be quite good at F77 as well but
            > its been awhile. Of these, I find Smalltalk to be the
            > most compact and elegant in all cases.

            That's not the same as having looked at equivalent code. For example, comparing Smalltalk to C++ without a good container library is just silly. (No, I'm not suggesting you did that, but I have experienced many cases where people failed to do such basic "homework").

            > If you have an online pointer to the paper in question, I
            > would be interested in seeing it.

            OOPSLA 2003 Garcia, et al: A comparative study of language support for generic programming. pp 115-134.

            I don't know if it's online.

            The old short study, which I do not have a reference to, ws by Tom Love (of Stepstone fame).

            -- Bjarne Stroustrup: http://www.research.att.com/~bs
            • Tor
               
              Posts: 3 / Nickname: ext / Registered: July 10, 2003 9:32 PM
              Re: Elegance and Other Design Ideals
              February 26, 2004 4:12 PM      
              The paper on generics is available at http://www.osl.iu.edu/publications/pubs/2003/comparing_generic_programming03.pdf and the code is available here http://www.osl.iu.edu/research/comparing/ . I would be very interested in seeing a Smalltalk version of this library which is more compact and elegant than the BGL.
              • Todd
                 
                Posts: 27 / Nickname: tblanchard / Registered: May 11, 2003 10:11 AM
                Re: Elegance and Other Design Ideals
                February 27, 2004 0:27 PM      
                I would be very interested in seeing a Smalltalk version of this library which is more compact and elegant than the BGL.

                It would be considerably more compact simply because the extensive type notations would be absent. Dynamically typed languages support generic programming natively without templating mechanisms - the participants need only conform to expected protocols to allow substitution of one class for another.

                IOW, templates put back the ability to separate protocol from type that is inherent in dynamically typed languages and was missing from C++ prior to the introduction of templates.
            • Daniel
               
              Posts: 5 / Nickname: dyokomiso / Registered: September 17, 2002 2:50 PM
              Re: Elegance and Other Design Ideals
              February 26, 2004 3:25 PM      
              [snip]

              > > If you have an online pointer to the paper in question,
              > I
              > > would be interested in seeing it.
              >
              > OOPSLA 2003 Garcia, et al: A comparative study of language
              > support for generic programming. pp 115-134.
              >
              > I don't know if it's online.
              >

              It seems to be available here:

              http://www.osl.iu.edu/publications/pubs/2003/comparing_generic_programming03.pdf

              Google cache for this paper:

              http://www.google.com/search?q=cache:CNwEcvMa0lcJ:www.osl.iu.edu/publications/pubs/2003/comparing_generic_programming03.pdf+%22+A+comparative+study+of+language+support+for+generic+programming%22&hl=en&ie=UTF-8

              The paper is in my read list so I can't comment on it. But after skimming through the examples and the conclusions it seems that the authors have some bias toward C++, as they try use the C++ solution to the problem to every other language.

              IMHO it would be better to present a problem to different groups using different languages and see they implementing it. This approach was used in "Haskell vs. Ada vs. C++ vs. Awk vs. ... An Experiment in Software Prototyping Productivity" (available at http://citeseer.nj.nec.com/hudak94haskell.html). In this paper the Naval Surface Warfare Center (NSWC) required a prototype for a Geometric Region Server from ten different groups (contractor and academics). The resulting programs and development metrics were reviewed by a committee chosen by the Navy. The resukts are shown below:


              Language LOC L. of doc. Dev. time (hours)
              (1) Haskell 85 465 10
              (2) Ada 767 714 23
              (3) Ada9X 800 200 28
              (4) C++ 1105 130 –
              (5) Awk/Nawk 250 150 –
              (6) Rapide 157 0 54
              (7) Griffin 251 0 34
              (8) Proteus 293 79 26
              (9) Relational Lisp 274 12 3
              (10) Haskell 156 112 8


              An interesting fact is that the tenth program was written by a newly hired college graduate with no experience in Haskell, other than a previous ten-day self study of the Haskell Report.

              We could also use the sources for each ICFP available, as several different languages are used to solve the same problem, each using a different design.

              There's also a relevant paper called "DSL Implementation in MetaOCaml, Template Haskell, and C++", comparing meta-programming techniques in the three languages.

              http://www.cs.rice.edu/~taha/publications/preprints/2003-12-01.pdf


              > The old short study, which I do not have a reference to,
              > ws by Tom Love (of Stepstone fame).
              >
              > -- Bjarne Stroustrup: http://www.research.att.com/~bs

              Best regards,
              Daniel Yokomizo.
              • Bjarne
                 
                Posts: 48 / Nickname: bjarne / Registered: October 17, 2003 3:32 AM
                Re: Elegance and Other Design Ideals
                February 26, 2004 4:00 PM      
                >> >
                > > OOPSLA 2003 Garcia, et al: A comparative study of
                > language
                > > support for generic programming. pp 115-134.
                > >
                > > I don't know if it's online.
                > >
                >
                > It seems to be available here:
                >
                > http://www.osl.iu.edu/publications/pubs/2003/comparing_gene
                > ric_programming03.pdf

                Thanks

                >
                > The paper is in my read list so I can't comment on it. But
                > after skimming through the examples and the conclusions it
                > seems that the authors have some bias toward C++, as they
                > try use the C++ solution to the problem to every other
                > language.

                Fair language comparisons are very hard - that's why I don't do them - but clearly authors and the OOPSLA program committee didn't think the paper was sloppy.

                > IMHO it would be better to present a problem to different
                > groups using different languages and see they implementing
                > it.> ...
                >
                > An interesting fact is that the tenth program was written
                > by a newly hired college graduate with no experience in
                > Haskell, other than a previous ten-day self study of the
                > Haskell Report.

                It is nice to see someone trying to get experimental data. It's very hard to consider all factors, though.

                > We could also use the sources for each ICFP available, as
                > several different languages are used to solve the same
                > problem, each using a different design.
                >

                That reminds me: From ICFP Programming Contest 2003 rules: "The contest offers direct, head-to-head comparison of language technology and programming skill. We have a range of prizes for the winners: cash awards, books, invitations to the conference for students, and, of course, unlimited bragging rights. The prizes will be awarded at ICFP 2003 in Uppsala this August."

                The results are here:

                http://www.dtek.chalmers.se/groups/icfpcontest/results.html


                -- Bjarne Stroustrup: http://www.research.att.com/~bs
                • Daniel
                   
                  Posts: 5 / Nickname: dyokomiso / Registered: September 17, 2002 2:50 PM
                  Re: Elegance and Other Design Ideals
                  February 28, 2004 4:10 PM      
                  [snip]

                  > > The paper is in my read list so I can't comment on it.
                  > But
                  > > after skimming through the examples and the conclusions
                  > it
                  > > seems that the authors have some bias toward C++, as
                  > they
                  > > try use the C++ solution to the problem to every other
                  > > language.
                  >
                  > Fair language comparisons are very hard - that's why I
                  > don't do them - but clearly authors and the OOPSLA program
                  > committee didn't think the paper was sloppy.

                  Just to clarify I don't think the paper is sloppy. AFAICS it's pretty good. But I still think the authors are trying to code a C++ solution in languages other than C++, not trying to solve a problem.

                  > > IMHO it would be better to present a problem to
                  > different
                  > > groups using different languages and see they
                  > implementing
                  > > it.> ...
                  > >
                  > > An interesting fact is that the tenth program was
                  > written
                  > > by a newly hired college graduate with no experience in
                  > > Haskell, other than a previous ten-day self study of
                  > the
                  > > Haskell Report.
                  >
                  > It is nice to see someone trying to get experimental data.
                  > It's very hard to consider all factors, though.

                  Yes. The authors acknowledge this, saying the experiment doesn't prove anything. But still is quite amusing :)

                  > > We could also use the sources for each ICFP available,
                  > as
                  > > several different languages are used to solve the same
                  > > problem, each using a different design.
                  > >
                  >
                  > That reminds me: From ICFP Programming Contest 2003 rules:
                  > "The contest offers direct, head-to-head comparison of
                  > language technology and programming skill. We have a range
                  > of prizes for the winners: cash awards, books, invitations
                  > to the conference for students, and, of course, unlimited
                  > bragging rights. The prizes will be awarded at ICFP 2003
                  > in Uppsala this August."
                  >
                  > The results are here:
                  >
                  > http://www.dtek.chalmers.se/groups/icfpcontest/results.html

                  I was part of a team trying to compete in ICFP 2003, so I knew about the winning entry. AFAIK C++ didn't give a competitive edge to the winner, the algorithm did (and it could be written in any language). Also if the author didn't have access to 16 dual processor machines he could never use this tactic, so I don't think it is a valid argument for C++ (or any language this year). Seeing the results of the other years we can see a pattern of winning entries. It's usually of smart hackers using FPLs, so either FPLs give them an edge or FPLs attracts smart people ;)


                  > -- Bjarne Stroustrup: http://www.research.att.com/~bs
                  • Bjarne
                     
                    Posts: 48 / Nickname: bjarne / Registered: October 17, 2003 3:32 AM
                    Re: Elegance and Other Design Ideals
                    February 29, 2004 5:43 PM      
                    >
                    > I was part of a team trying to compete in ICFP 2003, so I
                    > knew about the winning entry. AFAIK C++ didn't give a
                    > competitive edge to the winner, the algorithm did (and it
                    > could be written in any language). Also if the author
                    > didn't have access to 16 dual processor machines he could
                    > never use this tactic, so I don't think it is a valid
                    > argument for C++ (or any language this year). Seeing the
                    > results of the other years we can see a pattern of winning
                    > entries. It's usually of smart hackers using FPLs, so
                    > either FPLs give them an edge or FPLs attracts smart
                    > people ;)

                    Actually, I was surprised that *any* C++ programmers would enter that forum. Most would never have heard of it. Maybe the offer of "unlimited bragging rights" attracted attention beyond the usual functional programming community.

                    I have noticed a pattern: When C++ programs succeed it's very often credited to "other factors" or it is even claimed that the success was "despite the use of C++". On the other hand, a failure is attributed to C++ without any possibility of excuses.

                    Unfortunately, I can't say I'm surprised, and the effect can be observed for any successful language. However, C++ is so prominent that the effect is embarrasingly (for the professional software community) common and obvious.

                    For a sampling of successful C++ projects, see http://www.research.att.com/~bs/applications.html . I have actually heard the claim, that these were *all* lucky accidents, but I find that a bit hard to believe.

                    -- Bjarne Stroustrup: http://www.research.att.com/~bs
                  • Todd
                     
                    Posts: 27 / Nickname: tblanchard / Registered: May 11, 2003 10:11 AM
                    Re: Elegance and Other Design Ideals
                    March 1, 2004 8:55 AM      
                    That reminds me: From ICFP Programming Contest 2003 rules:
                    "The contest offers direct, head-to-head comparison of
                    language technology and programming skill. We have a range
                    of prizes for the winners: cash awards, books, invitations
                    to the conference for students, and, of course, unlimited
                    bragging rights. The prizes will be awarded at ICFP 2003
                    in Uppsala this August."

                    The results are here:


                    http://www.dtek.chalmers.se/groups/icfpcontest/results.html

                    Interesting. You need a whole team of programmers to solve that problem? With better programming models you can get kids to do it in an afternoon:

                    http://www.squeakland.org/school/drive_a_car/html/Drivecar12.html

                    Of course, looking at the rules, the squeak etoys programming model is much too alien to fit into the problem description as given.
    • Eric
       
      Posts: 2 / Nickname: ejain / Registered: March 3, 2003 9:38 PM
      Re: Elegance and Other Design Ideals
      March 16, 2004 9:54 AM      
      "designers should give people enough rope to shoot themselves in the foot"

      I'd argue that there are tools better suited for shooting in the foot than rope :-)