Wednesday, February 11, 2009

A more Effective C++

As stated before, C++ has a lot going for it -- things I wouldn't want to reinvent. Compiler support, well-understood syntax, native run-time execution, the list goes on. There are parts I want to get rid of. Support of C-based programming idioms, header files, global variables, these things have got to go. Well, I take that back. We need the ability to support legacy code, but we don't have to like it.

Let's pretend we have a new evolution of C++ that let's us be more effective (intentional reference to a Scott Meyers book here). Let's call it C++ - C, or C' (hmm C Prime seems to be taken), or how about E++ (that's kinda taken too), too bad for someone. I'll go with C'' (C-Prime-Prime), for now, but I like C Prime better.

C'' supports:
  • Object-oriented and other class-based development techniques
  • Inclu[de]-sion of other declarations classes by name instead of header file
  • Templates and all other C++ goodies we know and love
C'' drops support for:
  • #define (fewer pitfalls for programmers to fall into)
  • Global variables (reduce coupling in classes and modules)
  • Loose procedures and functions (only supports class methods, increases potential for achieving cohesion)
C'' is still backwards compatible with C++ and C.

Screeeeetch! Contradiction. The past two "feature" sets are mutually exclusive. Aren't they? Okay, obviously, I have a trick up my sleeve or some nasty kludge to hit you with.

Not really. If our goal is to utilize and leverage the strength of the C++ language and tools, then let's do so. If we devise a C-prime-prime "compiler" to take as input our C-prime-prime file, and then output a C-plus-plus file, we simultaneously realize a number of significant benefits:
  • Permit a more efficient, header-less coding style
  • Allow us to restrict ourselves to the desired OO- programming idioms, while ridding ourselves of trouble-laden C idioms
  • We can re-use and leverage all existing C++ tools, processes, methodologies, build environment to build our binary executables
  • We support legacy C/C++ code side-by-side with our C'' code
So, our C'' "compiler" is closer to a preprocessor than a compiler. It takes a text input file, and generates two output files -- intermediary C++ source and headers file, which in turn are compiled by standard C++ toolsets and toolchains.

C'mon, this is so easy. Has anyone done this before?

1 comment:

  1. A note about the name. I like C Prime because it has the reference to a Calculus derivation. Is it a derivation, though? I'm not sure, it's certainly a subset, but this idea /is/ derived from C++. I'd be interested to hear ideas and thoughts about naming.

    ReplyDelete