Sunday, August 3, 2014

Project – Better C++

The primary goals for this project was to get back to C++ programming, to learn using it more effectively after more than a decade doing only C programming, and to learn GUI programming.  Creating an incremental (interactive) BASIC compiler seemed to be an ideal way to accomplish this.  The GUI programming came later with the selection and use of the Qt toolkit.  Learning about the CMake build system and the git distributed revision control also came along the way.

After reading the book API Design For C++ by Martin Reddy recommended by a coworker, I started to get more of a taste for what C++ is capable of.  Even though this project is not an API (Application Programming Interface), when you think about it, any C++ class is an like API for other code that uses it.

I then picked up an old C++ book I purchased over two decodes ago when I was first learning C++, but couldn't get past the first chapter at the time.  I won't bother mentioning the title since this 1989 book is way out of date now (for example, C++ did not even have templates or the delete[] operator when the book was written).  Any way, I read the book and it wasn't really that bad, so I decided to pick up a more up to date book on C++.

I selected the book The C++ Programming Language (Fourth Edition) by Bjarne Stroustrup (the creator of C++).  I never liked older editions of this book, but decided to give this book a try since it includes the new C++11 standard.  I've picked up quite a bit that I did not previously know about C++ and I'm barely into the book at this point.  What I have realized that parts of what has been implemented in the project fails at being good C++ code.

One example is the memory management of tokens.  A lot of work went into tracking tokens.  There were two separate implementations in the attempt to make sure the ownership of the tokens and allocation of tokens worked properly - and to report any errors upon termination of the application.  The C++ facility of overloading the new and delete operators was used to accomplish this.  There is a better C++ facility using existing library classes that can be used to drastically simplify this.

Another example is the code table that holds information for all the BASIC program elements (commands, operators, functions, etc.).  The current solution is very much a straight C solution with C++ wrapped around it.  The current method on how code IDs are assigned is very kludgy (via an awk script that scans for special comments in a source file).  The method of how  associated codes are setup is also very kludgy (via elaborate C macros).  As a result, it is very easy to setup table entries incorrectly.  There must be a better way to accomplish this with C++ (though I'm not sure what that is yet).