ListPod.net

13

Features that Make the D Programming Language Better than C++

Sure, this can be a bit controversial, but if you feel differently, make a list of ways that C++ is better than D (and there are reasons).  D has hundreds of incremental improvements to C++, but this list highlights some of the biggest.

About D (From digitalmars.com/d):

D is a systems programming language. Its focus is on combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python. Special attention is given to the needs of quality assurance, documentation, management, portability and reliability.  The D language is statically typed and compiles directly to machine code. It's multiparadigm, supporting many programming styles: imperative, object oriented, and metaprogramming. It's a member of the C syntax family, and its appearance is very similar to that of C++.


Also see this feature comparison matrix.

Items



8

No Header Files

D understands the concept of modules (where each file is a module) like Java and C#.  This prevents the cascade of unwanted namespace pollution that happen in C++ when one header file includes several others.

And without header files, a typical D project has half as many files as the equivalent C++ project and 10-20% less code.
7

Optional Garbage Collector

D's garbage collector is optional and can be completely disabled; you are then responsible for deleting objects yourself just like in C++.  Alternatively, you can disable it inside performance-sensitive code and only run it when convenient.

Garbage collection reduces LoC and bugs since you no longer have to write copy constructors, reference counters, and destructors or worry about ownership of memory.

C++ has some add-on garbage collectors, such as Boehm, but without language support, all stack/heap memory values are treated as pointers which prevents collection due to false references.  Although D also uses a conservative garbage collector, extra type info from the language vastly reduces false pointers (need to verify this).
6

More flexible templates

Templates can take string parameters, or even aliases of functions and symbols as parameters.  With 'static if' is compile time 'is' expression checks you can write compile time conditionals based on type information just as easily as you can regular conditionals based on run-time values.
6

Faster Compilation Time

I compared two projects that were both near 15KLoC on Windows, using DMD 1.056 and Visual C++ 2008 compilers, both were built on a P4 2.8ghz with 1GB ram with debug info enabled and optimizations disabled.

DMD 1.056: 4 seconds
Visual C++ 2008: 132 seconds

If someone has a more scientific comparison, please edit/replace this item with your results.  However, with an order of magnitude (33x) difference in compilation time, it's safe to say that D is much faster.  This translates to direct productivity.
6

Compile-Time Function Evaluation

Also known as CTFE; if D has enough information to evaluate a function at compile time, it will do so.  If you write factorial(12)*sin(0)*180/3.14, the result will be compiled directly into the program.
5

Contracts and Unit Testing

D has built-in support for:
  • Contracts--which verify the input/output of a function
  • Class invariants--to ensure integrety of the state of a class between calls.
  • Unit Tests--easy enough to add in C++, but one less thing you have to do.
5

Nested Functions

In D, one function can be nested inside another, allowing for better compartmentalization of code.  Although this can still be done in C++ with nested classes, D's approach is simpler.  Additionally, nested functions/classes in D can access their parent's scope.
5

Improved Array Operators

  • Arrays can be concatenated directly with the ~ and ~= operators.  
  • Although this makes the base pointer 8 bytes instead of 4 (on a 32-bit system), arrays store their own length with the .length property.  C style arrays can still be used with a pointer.
  • D's operator[] allows access to multiple elements, returning a slice of the array.

Source: digitalmars.com/d/2.0/arrays.html
4

Safety

D supports array bound checking (which can be disabled via a compiler switch), and using the Safe-D subset (a very large subset) of the language, you get all of the memory safety of languages like C# or Java.

More info on SafeD
3

A Better Type System

Types have much shorter names, so you get ulong instead of monsters things like unsigned long long.  Also, types are always guaranteed to be the same size across all platforms and implementations.