C++ Design Tips and Tricks
Programming ....
|
|
C++ Design Tips and Tricks by Dr. Michael Whalen
|
|

C++ Design Tips Tip 1:
Learn Patterns
Someone has done what you are doing before:
For Example:
- Extracting information from a complex graph of classes (visitor)
- Creating a framework to notify observers when some data-of-interest changes (observer)
Design Patterns are a way of expressing these ideas that give you an abstract vocabulary for a complex idea
Good Book: “Design Patterns” by Gamma et. al GoF
C++ Design Tips Tip 2:
Learn Idioms
Idioms are easy ways of expressing certain universal concerns in your programming language of choice.
- Example: Resource Manager
- Example: Template Specialization
- Example: Enumeration Classes
Many found in books:
- “C++ Programming Language” – Stroustrup
- “C++ Common Knowledge – Dewherst
- “Essential C++” - ???
Idiom Example: Resource Acquisition is Initialization (RAII) Create class for managing resource Maps resource lifetime to object lifetime
Responsibilities:
- Ensure that resource is properly initialized in the constructor
- Ensures that the resource is properly freed in the destructor
- Ensures that resources are properly freed in the presence of exceptions.
C++ Design Tips Tip 3:
Design an Error-Handling Strategy
C++ Has Several Good Mechanisms for Error Handling. Strategy depends on application
My view:
- Asserts – use liberally for situations that should never occur
- Exceptions – use for situations that are very unlikely to occur
- For system applications, define additional facilities: logging, reporting, queuing, etc.
C++ Design Tips Tip 4:
The answer is not always Object-oriented.C++ allows a range of programming styles
Not every problem is OO, nor should it be.
Ex: A numeric algorithm, such as a sequnce of matrix transforms, is a function. This kind of function doesn’t make sense as a class member. Use namespaces to group related functions that do not belong in a class.

Prashant Mhatre
Make Money, Programming, Bollywood, Marathi
^_*