Thursday, 22 June 2017

My C++ reminders

I don't spend all my time doing the same thing. I hop from one project to another and end up using different tools and different languages as appropriate for the job. It becomes difficult to remember the best practice for any particular tool. I need to maintain a quick reference sheet.

Coding Standards For C++

Use variables instead of hard coded 'magic number' to make code more readable.
Use const variables instead of #define for static members because the variable name is visible in the debugger.

I prefer all my configurable options at the start of the file.
I use #define where I do not want a variable to have global scope but I want all the configurable items in that block of code at the start of the file.

Do not use #define within a function unless it cannot be avoided. An example where it might not be avoidable is where extra code may be needed only for debug purposes or where code is specific to only one environment.

I put all my comments in the header (.h) file including the licence details and feature list.

Preferred Naming Convention

These are my preferences.
UPPER_CASE for global scope const members or #define macros.
camelCase starting with a lowercase word for variables.
CamelCase starting with uppercase for function or method names (less common style.)
No differences for public, private or protected members.

Licenses

I release most of my code under an MIT, Microsoft Permissive or similar style licence unless, any of the work it is based on, forces another licence. I try to avoid referencing works released under the GNU public licences (GPL) because I think they are too restrictive. The Lesser GPL is just about acceptable.

**

Licence (MIT licence):

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sub-license, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

**

Where appropriate I use a single line version which I believe has the same meaning:
'This work is free to use with no warranty'

==

References:
http://www.cplusplus.com/
http://www.learncpp.com/

==

No comments :