Friday, February 13, 2009

C Exhausted, Moving On to D

So, I've been pining away for a programming language that emancipates us from the tyranny of C, has the modern features of Java and C#, while also keeping the familiarity and native compilation of C++. I want my three-tiered cake, and you can bet I'm going to eat it too.

I wanted to call it C-prime, E++, and eventually settled on C-prime-prime. Gone would be the relics of C: farewell to #define and your preprocessor ilk, bug-prone-and-confusing array handling would be slain, and never again would we be forced to write redundant declarations within headers files. We would honor the new traditions and better paradigms with operator overloading, exception handling, run-time type identification, and template programming.

At last, we reveal that D can be a suitable champion for this cause. With so many people using C++, it makes sense that some intelligent minority would seek to build a better C++, building on its strengths, while realizing the benefits and advanced made in newer languages and even of its own accord. D is not my holy-grail of languages, but after researching it, my most important desires for an improvement over C++ seem fulfilled.

First the D language discards the antiquated relics left behind by C:
  • D drops #define and the C preprocessor - a giant source of programming pitfalls, when better alternatives exist.
  • D does not require forward declarations - a detriment to efficient code writing; now we can lay code out naturally without writing additional and redundant lines of code, which simply make maintenance more difficult.
  • D does not have header files, which happen to be an even bigger impediment to efficiency; D requires no additional files with declarations of redundant code fragments and end up drawing out compilation time
In keeping with C++, D continues many of the C++ language's best features:
  • D supports object- and interface-oriented programming paradigms
  • D supports operator overloading (in a simpler, more efficient way than C++!)
  • D has templates as a meta-programming paradigm
  • D continues exception handling and run-time type information
Further, it brings the best features of Java and C# without the interpreted code that must be run on a virtual machine:
  • D compiles to native machine code, so once you've compiled, your computer is ready to chomp those bits
  • D adds a garbage collector; this is a feature I'm on the fence about, but it does have a significant boon to writing-efficiency and bug-catching. GC has its own realm of issues, but it should lessen the number of developmental pitfalls
  • D removes the need for pointers; pointers are still available, but passing objects is implicitly by reference.
  • D improves type checking, making things such as typedef create stronger types. Overall, D is a more strongly typed language than C++.
  • D supports synchronization as a language feature, instead of as a library feature
  • D increases security of arrays by tracking dimensions, and attempts to make array declaration more consistent and readable.
  • D differentiates between invariant objects -- objects whose data is absolutely read-only -- from those object which are constant -- objects whose data is obligated to only be read, regardless of whether the data underneath is modifiable by other means. Further reference semantics are far more sensical, and D offers more control of const-ness and invariance when multiple levels of indirection are required.
Of course, D is not without its drawbacks:
  • D chose to permit function-data programming paradigm, so data-driven, iterative programming remain viable. This is something that remains troublesome for very large projects, but is viable and valuable for smaller programs, so I can live with this.
  • D permits global variables. Globals are probably the number one thing a programmer can use to make a module more difficult to understand and debug, as it thwarts data abstraction and data-hiding motivations. I know banning it seems awfully authoritarian and extreme, but I can temper myself while adding a warning to programmers to be extremely wary of global variables.
On to the D language's more core issues:
  • D is young, meaning there are less tools and development environments that are "D-ready." You won't find Microsoft Visual D, but there are Eclipse open source plug-ins.
  • The D language's standard library is well, non-standard at the moment. There are two variants, Phobos and Tango, which are currently being integrated into D 2.0.
  • By bringing in Garbage Collection (mentioned earlier), there exist situations where you may need to maintain a weak reference to an object, when a full reference might result in a circular reference, meaning a reference that indirectly ends up referring back to the referrer making a full circle. In D, there is no built-in support for these weak references.
  • Finally, D is not backwards compatible with C++, in the way that we could have our own improved C++, while keeping everything from before. However, D does support linking with C libraries, while it does not fully support linkage with DLLs that use C++ link tables.
Overall, D seems to be designed with people like me in mind. It's far more efficient, it has numerous modern features intended for large-scale projects, and is practical, yet efficient in its intent, design, and implementation. For more information about the D programming language, check out Digital Mars:
http://digitalmars.com/d/2.0/index.html

and check out the Wikipedia page:

http://en.wikipedia.org/wiki/D_programming_language

No comments:

Post a Comment