On 25/05/2007, at 1:19 PM, Erik de Castro Lopo wrote:

André Pang wrote:

Personal opinion: I go so far as to declare all my local variables as
const, and will sometimes restructure code to ensure that const is
used on as many variables as possible.  I actually wish that C++09
would have a compiler flag to enforce const being the default, and
that you'd have to put the keyword 'mutable' in front of variables
you want to change :).

If you are going to this length aren't you better of actually coding
in Haskell (or Ocaml)? :-)

Absolutely not. I love Haskell, but C++ is a powerful-enough language that doing most things in it isn't that painful. (It might not look very pretty, but that's an acceptable for me.)

The pain for me mostly comes when I don't understand Yet Another Subtle C++ Language Thing. I ran into a lovely little one the other day when I did this:

  std::set foo(...); // has some elements
  std::set bar(...); // has some other elements
  std::set mySet;
std::set_difference(foo.begin(), foo.end(), bar.begin(), bar.end (), mySet.begin());

Veteran STL coders would probably spot the error right away, but it took me an hour of Googling around to figure out how to fix the dozen template error messages that showed up. (Solution: I had to use std::inserter(mySet, mySet.begin()) as the output iterator for set_difference, instead of just mySet.begin(). Oops; subtle indeed.)

However, thankfully those kind of problems obscure C++ problems are rare enough these days for me that I can get doing most tasks in C++ reasonably well. Unfortunately, there's still little competition to C ++ when it comes to high-performance, cross-platform code that _has good tool support_ on all platforms. (And good tool support makes a massive difference!) It's too much effort for me right now to integrate Haskell with system libraries and the standard industry tools on Mac OS X and Windows, two important platforms for me.

Getting back on topic though, the greatest thing about Haskell is that it teaches you an entirely new style of programming. I eschew mutable state wherever it's possible in imperative languages. (Cocoa is great about this: it has immutable collections that are used by default, with mutable subclasses of the collections if they're required. i.e. NSMutableDictionary inherits from NSDictionary.) Obviously avoiding mutable state isn't entirely possible in C++ since the entire idea of 'objects' is to encapsulate mutable state, but now that I know Haskell, I view C++ objects in a similar light to monads: use objects where they help and realise that you're in a monadic (object) context, rather than a purely functional context. Using const aggressively is an easy thing to do, and I'm always thankful that I use it when I go back to look at code I wrote six months earlier. It makes code a lot more maintainable. How often _do_ you modify your local variables, anyway? I bet you it's less than 50% on average, which I think is a pretty reasonable case to making const the default :).

As an aside, the next language I want to look at is Dylan. I've heard great things about its design, and it seems like a very pragmatic middle-ground between functional and OO programming. (I may prefer a functional paradigm to OO, but that's irrelevant when it comes to integrating with many other libraries or languages that are OO. It's an OO world, whether we like it or not!)


--
% Andre Pang : trust.in.love.to.save  <http://www.algorithm.com.au/>



_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders

Reply via email to