Most of my work is done in Python, but I am about to embark into a serious project demanding a lot of C++. Throughout my Python experience, I've found the Zen of Python (presented as an appendix below) to be a serious source of inspiration.
I know (or I think I know) that in order to get somewhere in C++ I need to follow closely the advice in the books "The C++ Programming Language" (how the language was designed) and "Effective C++" (how to actually use it). In addition to those two fine sources of information, is there some sort of "Zen of C++" I could follow?
I am looking for actionable, non-obvious but common-sense pointers on how to be effective.
So far the responses seem to agree on the following (wording needs to be polished).
- Compile time errors are better than run time errors
- Prefer references over pointers
- static_cast is better then dynamic_cast
- Don't use casting to fix bad design decisions
- Single inheritance is better than multiple inheritance
- One class per file is important
- The object that called new should be the one that frees that memory
- The user should not suffer any disadvantage because of a language or library feature s/he does not use ("you don't pay for what you don't use")
- Anything unsafe should be made explicit; the more unsafe, the more explicit.
- There should generally be a way to do everything that could be reasonably conceived of within the Standard
- Interoperability with C is a Good Thing
- Code should be expressive, but not lengthy
- The language shouldn't force you to use a given paradigm
- If you can do it in one line don't do it in one line
- Always know where your things are and what they hold
- NULL is not so null unless you define it
- The fact that it is there doesn't mean it should be there
- Never use something unless you know it's useful
- void is not necessarily empty
- classes are better than structs except when they're not
- Pass function parameters as const references as much as possible
- RAII - Resource Acquisition Is Initialization
- If possible, prefer standalone functions to class member functions
- Use the paradigm that makes sense
- Use the standard library when possible
I've seen little about "smart pointers", and I think it should be added to the list. From Wikipedia:
In computer science, a smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. These additional features are intended to reduce bugs caused by the misuse of pointers while retaining efficiency. Smart pointers typically keep track of the objects they point to for the purpose of memory management.
Of course, certain aspects of the "Zen of Python" also apply to C++.
Appendix: The Zen of Python
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Complex is better than complicated.
- Flat is better than nested.
- Sparse is better than dense.
- Readability counts.
- Special cases aren't special enough to break the rules.
- Although practicality beats purity.
- Errors should never pass silently.
- Unless explicitly silenced.
- In the face of ambiguity, refuse the temptation to guess.
- There should be one-- and preferably only one --obvious way to do it.
- Although that way may not be obvious at first unless you're Dutch.
- Now is better than never.
- Although never is often better than right now.
- If the implementation is hard to explain, it's a bad idea.
- If the implementation is easy to explain, it may be a good idea.
- Namespaces are one honking great idea -- let's do more of those!
For reference, I'm also adding the Zen of Perl. It is interesting to notice the differences with the Zen of Python; understanding language philosophy is important for being effective.
Appendix 2: The Zen of Perl (taken from Perlmonks)
- Beauty is subjective.
- Explicit is recommended, but not required.
- Simple is good, but complex can be good too.
- And although complicated is bad,
- Verbose and complicated is worse.
- Brief is better than long-winded.
- But readability counts.
- So use whitespace to enhance readability.
- Not because you're required to.
- Practicality always beats purity.
- In the face of ambiguity, do what I mean.
- There's more than one way to do it.
- Although that might not be obvious unless you're a Monk.
- At your discretion is better than not at all.
- Although your discretion should be used judiciously.
- Just because the code looks clean doesn't mean it is good.
- Just because the code looks messy doesn't mean it is bad.
- Reuse via CPAN is one honking great idea -- let's do more of that!