2010-03-10

C++0x: A const * const Odyssey

This is a presentation I and my colleague Anders Schau Knatten held at an internal software seminar this February.

It was meant to cover both some introductory material, best practices we have acquired, new features to look forward to and things that might (or should) scare you.

However not one member of our audience had less experience with C++ than Anders and myself have together, so we quickly jumped down into details.

You should probably view the presentation full screen and open the speaker notes to get the most out of it.

You might find this additional note on slide 43 interesting:  A non-virtual destructor once made it to the board of directors..

2010-03-08

Why C++ is Male

Through recent frustrations, and needless to say, core dumps, I have realized that C++ as a language have some distinct features often associated with men: It can only do one thing at a time, and it cannot communicate. In this post I'll elaborate on these.

It Can Only Do One Thing at a Time

In C++ threads or the notion of multiprocessing simply does not exist. The language has neither a thread concept nor any thread synchronization primitives like mutexes, semaphores and locks.

Even relatively simple constructs like static members simply do not work in a multi-threaded context. As described by Scott Meyers and Andrei Alexandrescu in C++ and the Perils of Double-Checked Locking, no matter how many volatile keywords or extra locking you add in the mix, it still won't work consistently. Some compilers try to help you by fixing this for you, but since that is compiler implementation dependent it does more harm than help.

C++ has no thread-safe memory model, and does not provide any means for a memory barrier. Which means that any "clever" compiler or instruction dispatcher is free to reorder your statements in whichever way provides the same result in a single thread of execution.

C++ Cannot Communicate

What is the de facto standard for communicating with the outside world, and has been for the last two decades? The Internet Protocol, or more specifically, the TCP and UDP protocols on top of it. But the C++ language does not support this protocol. The only means of communication provided by the language are streams for input and output, and they can only be used for talking to files or to the console.

You Can Teach C++ New Tricks

In the recent years I've learned the art of walking and chewing gum at the same time. I can even pay attention to a conversation while doing other stuff, or at least I think I can.

Being a married man I have also learned to cope with my lack of communication skills, to some extent. Neither of these feats came easy. They were achieved after failing a lot and suffering many crashes.

I have never tried teaching a man to do these things, but having been taught these capabilities has provided some insight into the requisite effort and amount of frustration involved.

Trying to teach C++ to do several things at the same time while communicating with others can also be accomplished. It is however probably equally frustrating as teaching your significant other something he obviously wasn't designed to do. Both capabilities require that you deal with the more primitive foundation the individual has evolved from (Neanderthals and C), and relying on cultural constructs (operating system interfaces) that have been added on top of evolution and behave differently from individual to individual.

In many circumstances the resulting behavior is undefined, and will certainly cause some surprises. You also have to meticulously and in great detail instruct your friend on every step required to perform the task, in a language he understands.

The Modern Man: C++0x

Men have evolved, and so has C++ as well. In C++0x the language has grown the capability of doing several things at once, but it still prefers to do so alone in a dark corner.

2010-02-24

UML in a new way

Once again I found myself in the need of writing some UML. My last attempt nearly got me carpal tunnel syndrome and a broken mouse.

Yet I once more went on the quest of finding a UML editor, bracing myself for a crunch down on the little pointer with a tool that would eat all my RAM and spit it back out.
This time around however, I discovered PlantUML. The process of writing for instance an action diagram became this easy:
@startuml
(*) --> "Download PlantUML"
--> "Run it"
--> "Open Notepad"
--> "Write Some Text"
--> "Hit Save"
--> "Look at the generated diagram"
--> (*)
@enduml
The tool supports Sequence, Use Case, Class, Activity, Component, and State diagrams, and only requires GraphViz.
And if Notepad isn't your tool of choice, you can choose between Word macros, Eclipse plugins, Ant tasks, or 10 other ways of running the tool.
Since it's text you can easily store it in version control, source code comments, pass it along in emails, tweet it, generate it, and parse it.
Batteries included, mouse not required. Highly recommended.

2010-01-14

New version of XPath XML editor

Recently I needed to use my XPath XML editor to tweak a number of POMs. Only catch was that I was required to do this on a Windows machine.
What's the catch you ask? XML and Java work just as fine on Windows. However, my editor requires a list of POM file names on the command line, something I quite easily fixed with
`find . -name pom.xml`
This isn't easy in CMD, but even Windows has evolved recently. Enter Windows PowerShell. The PowerShell replacement is pretty simple once you get to know the PowerShell basics:
$(ls -include pom.xml -recurse -name)
If you'd like to tinker with it, I've also pushed the xmleditor to GitHub.

2009-10-13

Six questions on TDD

Read Uncle Bob's TDD Triage:

"Is TDD a replacement for architecture?
Is TDD a replacement for design?
Should TDD be used for every line of code?
Well, if you are going to write some tests afterwards, why not write all tests afterwards?
Given that we accept the need for tests, why the resistance to test-first?
Wouldn’t it be faster without such high test coverage?"
"TDD
The Design Pattern Religion
Minimizing Concurrency"
Very good reads indeed. It is of course triggered by all the controversy of Joel Spolsky's blog on Jamie Zawinski being a Duct Tape Programmer.
Here are some of my comments:
  1. Learn concurrency principles. You'll need it. Moore's Law no longer gives you more clock cycles per second, it gives you more processor cores. To effectively utilize a modern CPU you have to keep all cores busy. And if you struggle keeping two quad-cores busy, imagine keeping 1,000 cores busy. It probably requires a fundamental programming paradigm change like the proposed fork-join framework for Java.
  2. Use high level building blocks like java.util.concurrent and java.util.concurrent.atomic. It gives you well tested code that is maintained by Other Smart People, relieving your brain to work on more important things. Two classes to study: ExecutorService and BlockingQueue.
  3. TDD is a good way of writing safe concurrency code. Design each element as a single threaded execution flow, and assert state before and after. Pay attention to locks and shared data.
  4. Architecture is Important. You don't build a skyscraper or a space station in the same way as a dog house or a residential house.
  5. Testing is a good way of exploring unknown APIs or legacy code. You basically assert the expected output or behavior of the code you are exploring.
  6. Duct Tape is good for many things. You can even build a sailboat out of it. But you probably wouldn't enlist for the Volvo Ocean Race with it?

2009-08-24

Versions Maven Plugin

Ever struggled with keeping versions of dependencies or inter-module links up to date? Now the Versions Maven Plugin comes to the rescue! It has readily available goals for tasks like:

2009-07-28

HTTPS connection explained

If you wonder how HTTPS works, you could read this one:
The First Few Milliseconds of an HTTPS Connection
After that, please come back and read my post TLS: A Broken Trust Model about the flaws of how HTTPS establishes a trust relationship between client and server.