Friday, August 3, 2012

C++11 is unsafe

With all due respect for Mr. Sutter, his claim that C++11 is "as clean and safe as any other modern language, and still the king of fast", is simply false. Clean and fast are up to the particular developer and benchmark but safe is more objective, and C++11 is not safe.

  1. It is important for C++ to maintain backwards compatibility with previous versions, so C++11 supports C++03. C++03 is not safe. So, by definition, C++11 is also not safe. For those who are new to the term, safety generally comes down to how bad my program can screw up. Even a wrong Java application will not segfault the VM. C++11 adds some tools to make causing this behavior harder, but it is by no means impossible. C++11 still has pointers. It still has pointer arithmetic.
  2. "But", you say, "we are talking about just the features C++11 added to the language". Still false. Take std::array, which was added in C++11. This code is unsafe: std::array<int 1> arr; arr[2] = 1;. And consider a lambda that captures a reference to a variable that goes out of scope. Perfectly valid C++11. Perfectly unsafe.
  3. Threading in C++11 also allows you to do unsafe things. Just try modifying two non-atomic variables concurrently. You have no guarantees of what will happen.
  4. "BUT", you yell, "he said 'modern language' so...." Indeed, so? I'm not sure what Herb Sutter considers a modern language but let's just take some languages that are somewhat popular today:
    • Java, C# - Considered safe.
    • Clojure, Scala - On the JVM, so one would consider them safe.
    • Python, Ruby, Perl - These are all considered safe languages. You cannot, without effort, access memory you should not.
    • Ada - Hah!
    • F# - Runs on .Net, safe.
    • Ocaml, Haskell - The languages themselves are considered safe but you can do whatever you want if you drop down to C.
    So which languages, exactly, is Mr. Sutter referring to when he says C++11 is as safe as any modern language? I have no idea.

The problem, though, is that it's OK to be honest about C++'s lack of safety. That is the compromise I am agreeing to when I use C++. I want the benefits of it and I understand that I am making a sacrifice. I don't want to be told C++ is something that it is not. This "C++11 is safe" talk is nonsense and not a reflection of reality.

You can follow the discussion on reddit:

Monday, July 9, 2012

The Erl Next Door

The Erl Next Door, also known as TEND, is a project created for SpawnFest2012 by MononcQc and I. The hope of TEND is to make playing with Erlang easier.

Ever wanted to show someone your cool Erlang hack or teach them a something about Erlang, but getting their project setup was too complicated? With TEND, you can now provide them a URL that will setup all of the dependencies.

Friday, July 6, 2012

So Pythonistas, you want to get rid of the GIL...

There is no shortage of hate for the GIL. There is a slight problem, though. The GIL might be the cause of Python's single-core-only utilization, but it's not the root reason.

Wednesday, June 6, 2012

My Mental Evolution In Making A Language

Over the past year I've been thinking about how to make my own language. It's a pretty big undertaking and I've been too busy with other projects to put any serious effort into it, so I just think about it when I go for a walk. Mostly I try to convince myself to not make a language but it sounds like a lot of fun. With the explosion of JavaScript, and so few people wanting to write JavaScript, it seems like other people think writing a language sounds like fun too. We have CoffeeScript, IcedCoffeeScript, Roy, JSX, Amber, Dart, and many others. And that's just a list of recent languages that compile to JavaScript. Many more languages have come out, relatively recently, such as Go, Fancy, Elixir and Loop. Most of these languages will be minor dents in the history of programming languages, but that is OK. Not everything has to be important to be worth doing. But they have gotten me thinking. Should I create a language? What would it have to offer? Is the effort worth it? Below are thoughts that have steered my decision process for when I get some time to start hacking on a language. The thoughts are targeted at me, someone who has little experience building a language, not a professional.

Thursday, May 17, 2012

Phantom type examples in Ocaml

What are phantom types

Phantom types are a way for multiple types to have the same exact underlying representation but still be seen as a distinct type by the compiler. The common example is units of measurements. Feet and meters are both distances and are best represented as an int or float, but it makes no sense to add a foot to a meter. Phantom types allow you to solve this and still represent a foot and a meter exactly the same. One can think of this as the opposite of duck typing. In duck typing, if you only care if the current value you are working with has four legs, a cat a dog and a table are the effectively the same thing. In phantom types you care about what something actually represents and that means they are different, even though underneath they are represented with the same type.