Advice for programming in C++.md (1453B)
1 +++ 2 title = 'Advice for programming in C++' 3 +++ 4 # Advice for programming in C++ 5 1. When you program, you create a concrete representation of the ideas in your solution to someproblem. Let the structure of the program reflect those ideas as directly as possible: 6 7 1. If you can think of ‘‘it’’ as a separate idea, make it a class. 8 9 2. If you can think of ‘‘it’’ as a separate entity, make it an object of some class. 10 11 3. If two classes have a common interface, make that interface an abstract class. 12 13 4. If the implementations of two classes have something significant in common, make that commonality a base class. 14 15 5. If a class is a container of objects, make it a template. 16 17 6. If a function implements an algorithm for a container, make it a template function implementing the algorithm for a family of containers. 18 19 7. If a set of classes, templates, etc., are logically related, place them in a common namespace. 20 21 2. When you define either a class that does not implement a mathematical entity like a matrix or acomplex number or a low-level type such as a linked list: 22 23 1. Don’t use global data (use members). 24 2. Don’t use global functions. 25 3. Don’t use public data members. 26 4. Don’t use friends, except to avoid [a] or [c]. 27 5. Don’t put a ‘‘type field’’ in a class; use virtual functions. 28 6. Don’t use inline functions, except as a significant optimization.