
On the evolution of programming style: How far C++ has moved from its C roots - ehamberg
http://blog.feabhas.com/2012/03/on-the-evolution-of-programming-style/
======
alextgordon
Let me give one other way of doing it: "C++ without classes"

    
    
        std::pair<double, double> findRoot(double a, double b, double c) {
            if (a == 0.0)
                throw std::invalid_argument("a has value 0.0!");
        
            double root1 = (-b + sqrt(b*b - 4*a*c)) / (2*a);
            double root2 = (-b - sqrt(b*b - 4*a*c)) / (2*a);
        
            return std::pair<double, double>(root1, root2);
        }
    

It seems too many C++ programmers see a problem and think "Hey, let's use
classes!".

~~~
alexchamberlain
What's std::pair if it isn't a class?

~~~
YogSothoth
a struct ;-) ... <http://www.cplusplus.com/reference/std/utility/pair/>

~~~
alexchamberlain
I was waiting for that! :-P

------
ajross
The subject matter is interesting enough. But if it's a discussion on style,
why is the code presented so unbelievably bad?

Needless duplication of code; simple one-line declarations (i.e. a struct with
three doubles) spread out over 5; empty constructors with trivial initializer
lists defined outside the class header; needless empty destructors defined
(again, outside the class declaration) for classes without virtual functions;
"} else {" spread out across three lines (pet peeve of mine).

The disaster of an Error enumerant deserves a paragraph all by itself. It's
got only two values, "OK" and "FAIL". Guess which one evaluates to boolean
truth? (And yes, I'm aware of how errno and the like work -- but error codes
tend to be something other than "FAIL").

This looks like code generated by an IT employee at a local soap manufacturing
company, not something to be seriously considered by people interested in C++
evolution.

~~~
mseebach
> This looks like code generated by an IT employee at a local soap
> manufacturing company

Too bad you go and ruin a perfectly reasonable comment with that crap.

~~~
ajross
Which moral calculus allows you to call someone else's work "crap", where I
can't cast aspersions on the quality of technology adoption in the soap
manufacturing industry?

~~~
mseebach
I didn't know the phrase "cast aspersions", so I went to look it up. It means
"to slander".

I'm sorry I slandered your slander.

~~~
ajross
To be clear: I was just pointing out the hypocrisy in pulling out my
(admittedly) mildly ad hominem slur comparing the OP author with an IT robot
and calling it "crap". You can't reasonably demand civil discourse if you
aren't willing to practice it.

But to be honest: I don't think my quip was really that bad. Did I hit too
close to home? Do you do soap automation?

~~~
Danieru
Mseebach was insulting your words, based on their content.

You were insulting people, based on their jobs.

And now you're attacking mseebach. In truth you don't sound very repentant.

~~~
ajross
So my crime is the use of a mild (and very common) ad hominem in a context
where I would expect most people would see the humor, not being uncivil? And
it's OK to call things "crap" just so long as I don't make an ad hominem
argument in doing so?

Sorry, your argument is steaming crap. The idea that one can't invoke the
_pervasive_ metaphor of bad IT-generated code (or, I guess, imply that the
local soap industry might do it) but that it's OK to flame away at the author
who does is just dumb.

And for the record: I don't believe I expressed any repentence. :)

------
ajax77
It should be noted, it's very clear his executable sizes are for a Debug
compilation. I tossed the same code for the final example into VC10, and sure
enough in release mode the compiled size is 10KB, and in debug it's just less
than the size he stated. This is for 64-bit.

I'm not sure what point it serves to compare debug mode executable sizes, but
that should be clearly stated in the article. As other people stated, the
final example really ought to lose the wrapping class in the spirit of C++11
brevity and functional decomposition.

------
michaelhoffman
I know the point is to discuss style, but I wish he had done it in a way that
didn't include a textbook example of floating point programming that can yield
inaccurate results. An accurate way wouldn't be so difficult:

[http://en.wikipedia.org/wiki/Quadratic_equation#Floating_poi...](http://en.wikipedia.org/wiki/Quadratic_equation#Floating_point_implementation)

------
Impossible
It's interesting that with a modern C++ compiler all of these programs are
equally valid. This is both the biggest strength and the biggest flaw of C++.

I imagine the size differences are overhead from bringing in STL and maybe
template instantiation and are relatively fixed? I would have liked to see
performance benchmarks as well as executable size to see if the program has
gotten slower, faster, or remained basically the same.

~~~
greyfade
No, the majority of that overhead is from the use of `iostream`s (which are
known to be pretty bloated compared to `printf` and friends), the use of
exceptions (try/catch is a morass of stack-unwinding code), and the added
bloat of class information (RTTI and symbols in particular).

If it were compiled with full optimizations and the final binaries stripped,
they'd be much closer in size. Moreso if exceptions are disabled or if the
class is replaced with a simple function taking/returning tuples.

~~~
alextgordon
Not only is iostream bloated, but it often injects a static constructor into
every translation unit (which visibly angers the – usually calm – LLVM style
guide[1]). Oh and it's really godawfully slow unless you turn off a magic flag
somewhere[2].

Friends don't let friends use iostream.

[1]: <http://llvm.org/docs/CodingStandards.html#ll_iostream>

[2]: [http://stackoverflow.com/questions/9371238/why-is-reading-
li...](http://stackoverflow.com/questions/9371238/why-is-reading-lines-from-
stdin-much-slower-in-c-than-python/9371717#9371717)

------
KonradKlause
Looks like the author of the C solution has never written a lot of C, but too
much C++. ;-D

------
malkia
Pointless... Because at the time you want to suck out performance of any of
those, you would have to stick your guns to the provided macros or inline
functions from Intel, ARM, IBM (PowerPC/AltiVec), etc. - and reimplement this
using them - SSE2, AVX, whatever.

This is not even funny....

And packing three doubles in structures - good programming technique? Okay..
That's enough (it might be good sometimes, but not always)

------
bfrog
I think the overusage of classes is directly related to schools teaching Java
instead of C or lisp or... well anything else.

------
cheez
This is a dumb example. Should have used a root finder. C sucks with those.

------
azth
Page not found :/ Does anyone have a cache of the article's text?

~~~
atesti
Here you go, courtesy of bing cache:
<http://pastehtml.com/view/bswjqr9ry.html>

~~~
azth
Thanks! :)

