views:

87

answers:

3

I've been developing an interpreter in C++ for my (esoteric, if you want) programming language some time now. One of the main things that I have noticed: I start with a flexible concept, and the further I code (Tokenizer->Parser->Interpreter) the less flexible the whole system gets.

For example: I didn't implement an include function at first, yet the interpreter was already up and running - I had extreme difficulties implementing it and it was just like "patching something out" later on. My system had lost flexibility very quickly.

  • How can I learn to keep relatively small C++ projects as flexible and extensible as possible during development?
A: 
  1. Define the structure of the project before you start coding. Outline your main objectives and think about how can you achieve that.

  2. Code the headers.

  3. Look if it's possible to implement every feature using this set of interfaces

  4. If no -> go back to (2)

  5. If yes -> code .cpp files

  6. Enjoy.

Of course, this doesn't apply to really large projects. But if your design is modular, there shouldn't be any problems to divide the project into separate parts.

buratinas
+1  A: 

If you need to keep

C++ projects as flexible and extensible as possible during development

then you haven't got a product specification, you have no real goal and no way of defining a finished product.

For a commercial product this is the worst situation to be in. To paraphrase one well known blogger (can't remember who) "you haven't got a product until you define what you aren't going to do."

For personal projects this might not be a problem. Chalk it up to experience and remember for future reference. Refactor and move on.

Skizz
A: 

Don't fear Evolution (Refactoring).

If there are many class that fit a theme, create a common base class.

Instead of hard coding data members, use pointers to an abstract base class.
For example, instead of using std::ifstream use std::istream.

In my project, I have abstract classes for Reading and Writing. Classes that support reading and writing use these interfaces. I can pass specialized readers to these classes without changing any code. A data base reader would inherit from the base Reader class, and thus can be used anywhere a reader is used.

Thomas Matthews