News for hackers. Stuff that matters.
Re:C++ in Coders at Work by cletus on Tuesday February 15, 2011 @08:59AM (#2221167)
The real shame about C++ is that it's the only mass market C-level language with advanced features built on top of it. Don't get me wrong: I agree with the author and his interviewees. I think C++ is a horrible language for the very reasons described: everyone uses a different subset of it and it's hideously complicated.

For a more current view, see Linus Torvalds' comments [1] [2].

What I would like to see is a language very much like C (being low-level) with a few features built on top. What those features are I guess is the real trick.

One thing I like about C++ is the constructor/destructor mechanism. It's very explicit. The trend now is towards garbage collection (which, admittedly, you can implement in C++). This is a trend I largely agree with. It's quicker and cheaper to write software that way (IMHO). But there is still a place for simper schemes as all GCs I've come across have issues.

One thing I think such a language shouldn't be is object-oriented. To quote Joel Spolsky [3]:

> A lot of us thought in the 1990s that the big battle would be between procedural and object oriented programming, and we thought that object oriented programming would provide a big boost in programmer productivity. I thought that, too. Some people still think that. It turns out we were wrong. Object oriented programming is handy dandy, but it's not really the productivity booster that was promised.

Or at least such objects should be very limited in scope. Linus addresses some of the reasons for this, such as knowing which function is being called by simply looking at a code snippet (typically a patch in his case).

When writing low-level code I certainly do much prefer C over C++. But having the tools to build automatic reference counting into C would be really nice at times.

[1]: http://thread.gmane.org/gmane.comp.version-control.git/57643...

[2]: http://www.realworldtech.com/forums/index.cfm?action=detail&...

[3]: http://www.joelonsoftware.com/articles/APIWar.html

Re:C++ in Coders at Work by spitfire on Tuesday February 15, 2011 @02:11PM (#2222714)
It's funny because back in the 80's we had a language that was already at where c++ and java were going. It's called Ada. Back in 83 it had built in concurrency, generics, etc.

The problem was, back in 83 the compiler was the size and complexity of a C++ or java compiler from 1995. Couple that with some horrible implementations and Ada never took off much beyond Avionics/security/life critical applications.

Which is a shame, because comparing ada to C++ or java today and imnsho Ada wins hands down. The compilers are faster, the code very nearly as fast (In Ada you get things like runtime array checking which slows you down). Unfortunately the programming language wars aren't about technical merit (Java would never have been popular otherwise) and more about social popularity contests/network effects.

EDIT: I might add that at this point ada is /far/ simpler than both c++ anda java. Jean Ichbiah thought carefully about what was needed for large, long lived projects (millions of lines, 30+ years life) and it shows. They got pointers right the first time - access types. verifiability - testing your code is all the rage now, 27 years late. etc. Best yet, the ecosystem around the language is mature. That is unlike c++ you don't have a dozen different approaches taken (template hell), and unlike Java you don't have bureaucratic overload.

Re:C++ in Coders at Work by X-Istence on Tuesday February 15, 2011 @09:14AM (#2221222)
People keep bringing up Linus' C++ in git rant, and I don't understand why it is used as an example, as it contains no real substantive reasons as to why to dislike it.

There is no real argument made one way or another. If we look at the linked article there are real arguments as to why people don't like C++. Templates, multiple inheritance, and a whole other range of stuff. All Linus is yelling is "I hate C++, I hate STL, them Boost guys aren't worth anything" with absolutely no reasoning what so ever.

As a project leader it is okay for him to disagree about using C++ within his source code base, and within context it is a perfectly valid email, but to use as an example as to why C++ is bad, it is a very bad email to quote.

Re:C++ in Coders at Work by grammaton on Tuesday February 15, 2011 @10:22AM (#2221487)
From my impressions of how he conducts himself, Linus is usually yelling with no reasoning what so ever.
Re:C++ in Coders at Work by beagle3 on Tuesday February 15, 2011 @12:34PM (#2222221)
He explained it more than once: Nuances are lost in the internet on one hand, and (apparently) Finnish culture is very in-your-face with everything made more extreme for dramatic effect.

You may not like it, but it is extremely effective, and Linus _is_ herding thousands of developers, he has to be effective.

Re:C++ in Coders at Work by grammaton on Wednesday February 16, 2011 @11:03AM (#2226898)
Nuances are lost on the internet? Because I've seen videos of him giving talks, and he acts pretty much the same way. What nuances are being lost?

Why do we excuse bad behavior from people?

Re:C++ in Coders at Work by subbu on Tuesday February 15, 2011 @01:23PM (#2222476)
Benevolent dictator
Re:C++ in Coders at Work by mkramlich on Tuesday February 15, 2011 @05:31PM (#2223677)
i happen to think that's the ideal government. it requires a dictator with the right qualities, and they are rare. Thus, what is possibly the 2nd best government type: democracy.
Re:C++ in Coders at Work by eru on Thursday February 17, 2011 @05:34AM (#2230496)
Like at Futarchy for an alterative model. (Though you could classify it as democracy.)
Re:C++ in Coders at Work by bonch on Wednesday February 16, 2011 @01:44AM (#2225401)
The problem, of course, is that what is considered "benevolent" is entirely up to the dictator to decide. That alone makes it a non-ideal government type.
Re:C++ in Coders at Work by loup-vaillant on Wednesday February 16, 2011 @10:50AM (#2226813)
No, it is up to the coherent extrapolated volition[1] of the dictator's people. (At least, it seems like a good guess.) What makes a government less than ideal is its failure to apply that volition. The question then is how hard a given type of governance is likely to fail.

Now I agree that for a host of reasons (like taking one's own volition for the country's CEV), dictatorships do not rank high.

[1]: http://singinst.org/upload/CEV.html

Re:C++ in Coders at Work by bonch on Wednesday February 16, 2011 @12:46PM (#2227358)
The point of a dictatorship is that the people don't have a say. The dictator does what he wants and portrays himself as benevolent through state-run media.
Re:C++ in Coders at Work by X-Istence on Tuesday February 15, 2011 @10:21PM (#2224799)
Absolutely, and it works well for him. In this case the yelling is being held up as an example of why not to use C++ which does NOT work as a valid argument.
Re:C++ in Coders at Work by barrkel on Tuesday February 15, 2011 @10:14AM (#2221458)
I strongly reject the idea that object orientation hasn't provided a big boost to programmer productivity. In my view, it's enabled building systems that are orders of magnitude larger and more complex because of the discipline it enforces in modularity and the discoverability it lends to APIs.

I mean, do you remember the C written in the 80s? Often, the only unit of modularity in common use was the translation unit, with static globals used freely, structures with no visibility protection, having their inner guts manipulated by different modules, frequently by manually tweaking the pointers etc.? If you had an API that protected structural guts, you were almost always essentially programming to an object oriented style, only with no help from the language and compiler.

Re:C++ in Coders at Work by jon_hendry on Tuesday February 15, 2011 @05:19PM (#2223631)
I think a lot of the benefits of OO reuse have been lost due to platform shifts. If you build a system on NeXTSTEP in the early 90s, then throw it out and switch to Java, then throw it out and switch to C#, you're naturally going to lose a lot of the efficiencies you theoretically could have taken advantage of had you somehow managed to stay on NeXTSTEP or OpenStep all along.

And then, of course, you have mergers and acquisitions leading to code being thrown away or warehoused or merged with other systems, or new management deciding to start from scratch.

Basically, the industry doesn't really have a long enough attention span to really make the most of OO code reuse.

Re:C++ in Coders at Work by barrkel on Tuesday February 15, 2011 @06:06PM (#2223818)
People have long talked about how OO failed to generate code reuse, but I don't understand how they do it with a straight face. You reuse masses of OO code every time you write a non-trivial program using a modern standard library.

I think the people making this criticism are looking in the wrong place for the reuse, and / or had weird ideas of what could be reused, and how it could be reused, to begin with.

The reuse that OO gives us comes from raising the level of abstraction with which the program is written, in particular because runtime libraries are so much larger ("batteries included", etc.). The hierarchical namespaces and encapsulated structure of modern class libraries reduces their cognitive overhead. For example, in .NET, you can work with WebRequest, TcpClient or Socket, depending on what level of control you need; and the conventions for working with these things are pretty uniform across the board. Larger applications are self-similarly written at a higher level, reusing code within themselves in a framework-like way.

But the people who thought there was going to be some kind of central library of domain-specific classes in your company, that you would reuse in multiple disparate applications, I think that was always fairly naive; reuse implies coupling, and coupling of things that are individually subject to change in ways that affects their users is fraught with problems, and always has been.

The best candidates for reuse aren't usually representations of the domain concepts that are central to a business, because each application in the business will probably be wanting to do something quite unique with those domain classes. Rather, it's concepts that are self-contained, universal, not likely to change much over time nor need different intrinsic behaviour from application to application, which are best suited to reuse.

Re:C++ in Coders at Work by ori_b on Wednesday February 16, 2011 @01:21PM (#2227527)
"You reuse masses of OO code every time you write a non-trivial program using a modern standard library."

How much of that is due to OO, though? I reuse masses of code without OO all the time.

Re:C++ in Coders at Work by jon_hendry on Wednesday February 16, 2011 @03:47PM (#2228212)
"But the people who thought there was going to be some kind of central library of domain-specific classes in your company, that you would reuse in multiple disparate applications, I think that was always fairly naive"

Well, perhaps, but I've seen it done successfully in NeXTSTEP/OpenStep shops back in the day, such as an investment bank. Not one big library, but a few frameworks so there's some separation.

The team I was on had our own frameworks, shared among the apps we developed. We also used/built our frameworks on frameworks from the bank's Architecture group, which were also used by other development groups.

Re:C++ in Coders at Work by Stormbringer on Wednesday February 16, 2011 @12:05AM (#2225097)
The biggest problem with OO is the abuses. I recently worked with a large codebase where /none/ of the behaviour for objects was in the objects themselves. Some Architect had come along and beaten it out of them with an extreme application of the 'too many patterns' anti-pattern.

One way they justify this is to 'reduce coupling', and so your comment about coupling tripped a red flag for me. Whenever I want to take the piss out of an Architect I just tell them that we should 'add an extra layer of indirection' to the design. 99% of the time they agree without realising that I'm satirising them.

Re:C++ in Coders at Work by dkersten on Thursday February 17, 2011 @09:01AM (#2230893)
"where /none/ of the behaviour for objects was in the objects themselves"

Actually, this could be a good thing, depending on what the goals were. For example, non-member non-friend classes are often more OO than putting everything into the class[1]. Furthermore, if you want to use function overloading for multiple dispatch, the keeping the functions separate from the objects is also useful. If your system is highly concurrent, keeping the code and data separate can be quite helpful. Also keeping data in structure-of-arrays form and using external functions to operate on these arrays can make huge differences in terms of cache usage, potential parallelism and vectorization of instructions.

Coupling might be a reason to do this, but its certainly not the first reason I think of. There are plenty of better reasons (and as always, not all reasons apply to all codebases).

TL;DR: There are lots of reasons why doing this could be a good thing.

(fwiw, I like to do this with my core data structures because I believe code and data should be kept separate (and that data is the more important of the two). I do like to provide normal objects as an API though, because I often feel that its a natural interface, but the internals of my code are rarely very OO in the C++/Java sense).

[1] http://www.gotw.ca/gotw/084.htm

Re:C++ in Coders at Work by dkersten on Tuesday February 15, 2011 @12:32PM (#2222206)
I agree. I'm not a big fan of OO, preferring functional approaches in most cases, but I usually use some kind of OOP either to tie things together or to provide easy to use interfaces to the system, as I find constructing and passing objects to modules is often a natural way of interfacing with them, especially if the system is complex and the objects are used to "insert" code into the system (eg, algorithmic skeleton style). So even though I dislike OO as a whole, I think it has a very useful and important role in programming and it certainly helps us bring ever-more complex problems to a level we can reasonably deal with.

Having said that, there are some things OO is bad at, eg concurrency.

Re:C++ in Coders at Work by georgieporgie on Tuesday February 15, 2011 @04:02PM (#2223275)
How does OO have any effect on concurrency? I'm genuinely curious here.
Re:C++ in Coders at Work by barrkel on Tuesday February 15, 2011 @04:28PM (#2223401)
Mutable state.

OO prioritizes encapsulation ahead of immutability as a way of making the problem of a mutable struct tractable. Most OO programs are graphs (picture as a complex web) of mutable objects holding references to one another, synchronously passing control from one object's method to the next (picture as a spider travelling around the web, making modifications). Concurrency means there is more than one logical point of control flowing around the graph, making modifications. To ensure modifications are consistent, one must now fight against the interconnected nature of this graph, and make certain areas mutually exclusive; that means you need to set up gates on all the edges leading into those areas (often called semaphores or mutexes).

But OO gives you precious little help in setting aside these areas, and guaranteeing internal consistency in the face of multiple mutating points of control.

Programs in a functional style are different, with their heavy emphasis on immutability. In this case, the web, as it were, is fixed and cannot be changed; instead, if there is to be a modification, a new copy is created (possibly only of the local area of change, and the unchanged portion included via references). Since there is no way to change data, there can be as many points of control performing operations as desired. (Actually, one of the models of computation used for functional programs is that of graph reduction, which pictures the program as a web of expressions rather than structures; and that big expression is iteratively made smaller by calculating different parts of it. In principle, the more points of control you have doing these calculations, or reductions (in the same way as '1 + 2' can be reduced to '3'), the better.)

Programs written in an agent / actor style are also different. Here, there is no single point of control flowing from one node in the graph (web) to the next; instead, each node has its own little point of control, and it reacts to messages coming in from each edge, and sends out messages along edges in response. This makes the program locally single-threaded, but globally concurrent.

Re:C++ in Coders at Work by dkersten on Tuesday February 15, 2011 @05:30PM (#2223669)
You said it better than I could have! :)
Re:C++ in Coders at Work by georgieporgie on Tuesday February 15, 2011 @05:23PM (#2223651)
Code organization aside, how is OO any different from procedural programming with regards to concurrency?
Re:C++ in Coders at Work by barrkel on Tuesday February 15, 2011 @05:36PM (#2223689)
I would say that procedural code is less inclined towards both immutability and encapsulation than OO; I would say to a first order approximation that it tends to be worse than OO for concurrency.

But there are moderating factors. Procedural programs that don't enforce strict modularity conventions tend to be bound in size by their complexity, a complexity that works against the level of understanding necessary to make things both concurrent and correct. Meanwhile, procedural programs that do enforce strict modularity will probably be using conventions that emulate a different paradigm, and will inherit that paradigms' costs and benefits WRT concurrency.

Re:C++ in Coders at Work by nicolaus on Wednesday February 16, 2011 @03:29AM (#2225666)
In 15 years of experience where Java sadly pays the bills, for example, systems written in that language that had a modeling approach that tended toward immutability and tended to limit the visibility of mutable state throughout the system were also more understandable, more amenable to change and therefore better than those that weren't. Irrespective of concurrency and parallelism!
Re:C++ in Coders at Work by eru on Thursday February 17, 2011 @05:28AM (#2230480)
Yes. Arguing that immutability is possible in Java, is a bit like arguing for good code in PHP. It's possible and advisable.

But Java naturally tends towards mutability. And you will have to work harder to make it immutable. Java also does not give you as many tools for this kind of style as functional languages usually give you.

First class functions, a rich type system and a library full of immutable data types come to my mind.

Re:C++ in Coders at Work by dkersten on Thursday February 17, 2011 @08:45AM (#2230845)
Also OOP (as seen in C++ and Java; the original ideas of passing messages to objects sounds like it would have suited concurrency a lot better) encourages mutable state. Sure, you can program with immutable state (and many people do - I certainly do), but it feels like its going somewhat against the grain of OOP in those languages.

Worse, OOP tends to hide the mutable state away internally in objects (or in objects stored inside objects etc) - OOP's data hiding and abstraction support can go against you here. Sure, good programming practices and discipline (eg, const correctness in C++) helps, but the languages definitely encourage mutable state.

I wonder how using methods-as-messages and turning OOP into a message passing system not dissimilar to the actor model would work in practice (both from a usability/syntax and concurrency view).

Re:C++ in Coders at Work by loup-vaillant on Wednesday February 16, 2011 @10:26AM (#2226693)
I'd say the biggest boost came from modules, and the control of side effects (by, among other things, limiting the use of global variables).

Now, do you see a significant additional boost that were provided by the various OO styles out there? More specifically, what significant advantage do classes have over modules?

Re:C++ in Coders at Work by alexk7 on Tuesday February 15, 2011 @09:29AM (#2221277)
I'm designing such a language. It's called C³. I would like some feedback on my design "rules of thumb"[1]. I'm literally still at the whiteboard! [2]

[1]: http://blog.c3lang.org/aims-and-rules-of-thumb-for-the-desig...

[2]: http://c3wife.com/?p=523

Re:C++ in Coders at Work by cageface on Tuesday February 15, 2011 @05:42PM (#2223710)
Just a tip - the superscript makes googling for or talking about your language a hassle.
Re:C++ in Coders at Work by Jach on Tuesday February 15, 2011 @03:13PM (#2223055)
I like your comment overall, though I don't find Linus' C++ rants all that useful. Also, no, you can't implement a GC in C++. (Which in turn makes its operator overloading and exception handling defective and throws reflection out the window.)

http://yosefk.com/c++fqa/heap.html#fqa-16.26

"C++ does not feel pain. It can't be reasoned with. Starting a fight is a big mistake. If you want to use C++, you must learn to love it the way it is, in particular, manage your memory manually."

Re:C++ in Coders at Work by sedachv on Tuesday February 15, 2011 @03:47PM (#2223203)
The Boehm-Demers-Weiser collector is available for C++.
Re:C++ in Coders at Work by eru on Thursday February 17, 2011 @05:30AM (#2230488)
I guess the grand-parent commentator meant moving GC?
Re:C++ in Coders at Work by suraj on Tuesday February 15, 2011 @11:09AM (#2221708)
I am keeping my eye on Clay http://tachyon.in/clay/
Re:C++ in Coders at Work by jdf on Tuesday February 15, 2011 @11:03PM (#2224898)
Similarly, I've been hoping that Rust (which is somewhat run by Mozilla labs folks) would turn out well:

https://github.com/graydon/rust/wiki/

Re:C++ in Coders at Work by gms on Tuesday February 15, 2011 @09:05AM (#2221187)
Perhaps Google's Go will end up being the answer.
Re:C++ in Coders at Work by sausagefeet on Tuesday February 15, 2011 @09:40AM (#2221327)
I think many people were hoping for it but Go fell fatally short in 2 key areas IMO:

1 - Error handling - Go doesn't have any powerful mechanism for error handling. The code I have seen is rather inconsistent and it doesn't solve the main problem from C of forcing people to handle errors.

2 - Polymorphism - They left any kind of generics out. I think this could go a long ways for (1) too, just look at Either or Maybe type in Haskell. Combine this with some pattern matching and you have a really powerful way to enforce error handling and have polymorphic data structures and functions. IMO this is the biggest failing of Go.

I think the failure to provide those two things also makes Go a future mess. Adding them later either breaks a lot of outstanding code or it means you have to versions of Go, the original and new which is a mess.

Re:C++ in Coders at Work by dchest on Tuesday February 15, 2011 @09:55AM (#2221369)
Adding them later either breaks a lot of outstanding code or it means you have to versions of Go, the original and new which is a mess.

Actually, the creators of Go are still not afraid of breaking things. For example, recent release (2011-02-01.1¹) changed the syntax of non-blocking channel operations, and it broke code.

[1] https://groups.google.com/d/topic/golang-nuts/uHfjRyO1Q6c/di...

Re:C++ in Coders at Work by sausagefeet on Tuesday February 15, 2011 @11:19AM (#2221763)
That is good to see. But I would argue that the change described here is a rather small change while the 2 missing features I described have a larger effect on how you design your application.
Re:C++ in Coders at Work by jbooth on Tuesday February 15, 2011 @11:47AM (#2221947)
Well, adding generics shouldn't break working code.. it's an additive change.

It says something that for the built-in collection types, Slice and Map, they had to hack around their own API and implement this-case-only ad hoc generics. Slice and Map should be fully implementable within the language.

Re:C++ in Coders at Work by supersillyus on Tuesday February 15, 2011 @12:47PM (#2222278)
Why should Slice be fully implementable within the language? C++ is another systems language that decided that everything should be able to be done in libraries, and we've all seen the complexity that results.
Re:C++ in Coders at Work by jbooth on Tuesday February 15, 2011 @01:42PM (#2222566)
Well, I'll back off slightly from the assertion "fully implementable" because you have the f = s[i] syntax which wouldn't be fully implementable unless you allow the user to define [], which I agree is crazytown.

However, the make() functions for slice and map are the only functions in the language that allow you to pass in a type and get that type back as a return function. Why can't users have that functionality? Generics seem to be the easiest way to get it.

Re:C++ in Coders at Work by supersillyus on Tuesday February 15, 2011 @06:48PM (#2224011)
I think I agree, but I'm not good enough at language design to be sure. :) In the current language, all uses of type literals need to be known at compile time; if I could pass a variable containing a type to "make" or use it to instantiate a map, the language needs generics to deal with that, and all of the complications that come with it. The current idiom seems to be (if I wanted my own "make"):

myslice := mymake((MyType)(nil), 10).([]MyType)

which is much uglier than the built-in way. However, I'm kinda okay with reflection-laden runtime tomfoolery looking a bit messy, mostly because I rarely use it, and when I do I like it to stand out and be obvious what is going on.

Re:C++ in Coders at Work by supersillyus on Tuesday February 15, 2011 @12:39PM (#2222238)
I don't agree with #1. I prefer Go error handling to pretty much any other style I've seen.
Re:C++ in Coders at Work by loup-vaillant on Wednesday February 16, 2011 @11:07AM (#2226918)
To date, I have seen no language except Qi that (1) supports sum types, and (2) isn't a statically typed functional language.

Anyway, it's not like a wannabe OO language would adopt Algebraic Data Types. "Those are for these ivory tower languages that no one use in the real world", one might say.

Qi: http://en.wikipedia.org/wiki/Qi_%28programming_language%29

Re:C++ in Coders at Work by eru on Thursday February 17, 2011 @05:19AM (#2230465)
Or try D.
Re:C++ in Coders at Work by masklinn on Tuesday February 15, 2011 @09:37AM (#2221317)
No. And thank science for that.
Re:C++ in Coders at Work by steipete on Tuesday February 15, 2011 @10:06AM (#2221427)
You are talking about Objective-C.
Re:C++ in Coders at Work by Stormbringer on Wednesday February 16, 2011 @12:10AM (#2225108)
The thing that OO does, and does really well, is that it makes handling special cases really easy. Anybody who has come across 'business logic' (a beast related to 'military intelligence') knows that special cases are going to be the bane of your existence. Ta da! Problem solved.

Why then, you might complain, have we not seen the benefits of OO? Because the majority of programmers (usually by their own admission) suck at design.

Re:C++ in Coders at Work by zeugma on Tuesday February 15, 2011 @10:11AM (#2221447)
About constructor/destructor: in C++ you call allocate object on the stack, destructor will be called when you get outside the scope of the variable. That feature, dumbed RAII in C++ lingo, may be the simple GC your looking for : no explicit delete, good locality of the destruction. It also allow correct handling of exception.
Re:C++ in Coders at Work by barrkel on Tuesday February 15, 2011 @10:19AM (#2221479)
Ironically, it's copy constructors which show up to break exception safety horribly if you're trying to be ultra-safe - cf. no stack<T>::pop() that returns the value popped. What C++ adds with one hand, it takes away with two more hands that popped up out of nowhere like mutant growths.
Re:C++ in Coders at Work by dzorz on Tuesday February 15, 2011 @03:02PM (#2222994)
C++0x solves that issue by introducing rvalues.