C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

Similar documents
Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Tokens, Expressions and Control Structures

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

C CODING STANDARDS AND PRACTICES ENTC 489 EMBEDDED REAL TIME SOFTWARE DEVELOPMENT REVISION 1.0 CODESTD.DOC

Short Notes of CS201

Variables. Data Types.

CS201 - Introduction to Programming Glossary By

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

Practical C++ Programming

CS3157: Advanced Programming. Outline

APPENDIX A : Example Standard <--Prev page Next page -->

Chapter 15 - C++ As A "Better C"

CS 351 Design of Large Programs Coding Standards

Cpt S 122 Data Structures. Introduction to C++ Part II

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

1c) (iv) and (v) OR (v) only (hindsight showed that the question was more ambiguous than intended)

W3101: Programming Languages C++ Ramana Isukapalli

Coding conventions and C++-style

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

Chapter 2. Procedural Programming

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

SCIRun Developer Guide

Axivion Bauhaus Suite Technical Factsheet MISRA

C++ Programming: Polymorphism

CS11 Advanced C++ Fall Lecture 7

CSCI 262 C++ Style Guide

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

EE 382 Style Guide. March 2, 2018

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

G52CPP C++ Programming Lecture 9

OBJECT ORIENTED PROGRAMMING USING C++

Guidelines for Writing C Code

Basic Types, Variables, Literals, Constants

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

Axivion Bauhaus Suite Technical Factsheet AUTOSAR

Fast Introduction to Object Oriented Programming and C++

III. Classes (Chap. 3)

CS 251 Intermediate Programming Coding Standards

EEE145 Computer Programming

Course Coding Standards

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Abstraction in Software Development

Absolute C++ Walter Savitch

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Ch. 3: The C in C++ - Continued -

Introduction to Programming

Introduction to C++ Systems Programming

UEE1302 (1102) F10: Introduction to Computers and Programming

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Instantiation of Template class

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Programming. C++ Basics

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

printf( Please enter another number: ); scanf( %d, &num2);

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

PIC 10A Objects/Classes

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Problem Solving with C++

COMP322 - Introduction to C++

Design. 3.1 Memory Allocation Why a Memory Allocation Object Is Needed

CS201 Some Important Definitions

Basic Types and Formatted I/O

MRO Delay Line. Coding and Documentation Guidelines for Prototype Delay Line Software. John Young. rev June 2007

Coding Guidelines. Coding guidelines. Contents

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Lecture 7. Log into Linux New documents posted to course webpage

Motor Industry Software Reliability Association (MISRA) C:2012 Standard Mapping of MISRA C:2012 items to Goanna checks

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

CE221 Programming in C++ Part 1 Introduction

Computer Science 306 Study Guide

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Where do we stand on inheritance?

C Formatting Guidelines

A Fast Review of C Essentials Part I

Programming in C and C++

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Object Oriented Design

QUIZ. What are 3 differences between C and C++ const variables?

ME 461 C review Session Fall 2009 S. Keres

LECTURE 02 INTRODUCTION TO C++

Implementing Subprograms

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

pointers & references

OBJECT ORIENTED PROGRAMMING USING C++

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Transcription:

C++ Coding Standards and Practices Tim Beaudet (timbeaudet@yahoo.com) March 23rd 2015

Table of Contents Table of contents About these standards Project Source Control Build Automation Const Correctness Local Variables Passing / Returning Parameters Class Objects Header Files Header Protection Defining Functions (Inlining) Dos and Don ts Code Formatting Indentation Spacing Scope Braces Text Width Classes Namespaces Functions Naming Declarations / Prototypes Function Separators Documentation Variables Naming Type Casting Dos and Don ts Switch Statements Standard Template Library Macros / Preprocessor 2

About these standards These standards are put in place so all personal, collaboration and professional code will remain easily readable, clear and concise. This allows engineers using the standard to leverage the most from C++ compilers. Above all else, code should be written for readability and maintainability. Never write a portion of code with I am glad I will never see this again. in mind it will be seen again and it will need to be maintained. Preparing code for readability will help avoid future mishaps. Project Must be built at the highest warning settings possible, with warnings treated as errors! Files and directories must be named with all lower case letters, underscore between words. my_file.txt Source Control SVN must be used on any project that; May possibly take more than 48 hours to complete. Has more than a single developer. Build Automation Note: The following is not yet in place as a standard, but is a goal that is being worked towards. In the future every project should be capable of being added to a nightly build system that will pull the latest from the project repository, run an automated build script and run tests associated with the project. Given this is not currently implemented, the following should be taken lightly. All projects should use premake4 to create the Visual Studio, XCode or other IDE project. 3

Const Correctness Strive to use const wherever possible and correct to do so; const modifiers do not incur performance penalties, but does make intent clear and prevents unintended changes to an object. Local Variables Create local variables as constants whenever they will not change within their scope, this includes holding a value from a called function, prefer holding a const reference over value. const Type& value(getsomevalue()); const Type value(getsomevalue()); const int localvariable = 10; //Never writes to localvariable Passing / Returning Parameters If a function does not modify an incoming parameter, it should be clearly marked using const. void SomeFunction(const int withcount); void SomeFunction(const Type& withcount); void SomeFunction(const Type* const withcount); Class Objects Make the object itself const if a method will not change any internals by adding the const modifier to the end of the method declaration. This will force the this* within to be const. class CSomeClass int MethodOfClass(void) const; ; 4

Header Files Header Protection Every header should internally protect itself from multiple includes using the following technique: #ifndef _ProjectName_FileName_h_ #include FileName.h #endif #ifndef _ProjectName_FileName_h_ #define _ProjectName_FileName_h_ /* All header contents */ #endif /* _ProjectName_FileName_h_ */ Defining Functions (Inlining) A header file should be used for declarations of types and functions only, however if code must be inlined it should be placed under all declarations within the header file. This is for readability, when looking through a header, one should not be concerned with implementation details. namespace Example class MyType MyType& GetSomething(void); // // inline MyType& Example::MyType::GetSomething(void) /* code */ 5

The exception is for one line accessors and mutators, that simply set or return a value. Dos and Don ts Don t use using within a header file. It will force any includers to automatically use. Do minimize including by forward declaring types whenever possible. Do include or forward declare everything necessary for the header file to compile. Do not define variables within a header file, only declare them as needed. Code Formatting Indentation Use tabs for indenting code, braces etc. Tabs should be set to four spaces. (This shouldn t matter if using tabs properly) Spacing A space must precede and follow binary operators: +,, *, << etc: myvariable+=10; myvariable += 10; Scope Braces Scope braces should be placed on separate lines, and the contents of the scope tabbed. while (condition) //contents of loop while(condition) //contents of loop 6

while (condition) //contents of loop Text Width All lines of code should be less than 120 characters wide, use the IDE to insert a marking line. If a line extends a few characters beyond, then feel free to leave it if it more readable left that way, otherwise break it up into smaller lines as needed for readability. Try to aim for lines of less than 120 characters, rather than breaking the line into multiple parts. Broken lines are difficult to read. Classes Every class should resemble one simple concept, and only one concept. Hide members and implementation details as much as possible. Always explicitly disable, or enable, copy constructors and assignment operators. If a class has virtual methods, it must have a virtual destructor as well. Must have public members than methods, followed by protected and finally private. class Example int mpublicdata; /* Other public members */ void PublicMethod(void) const; /* Other public methods */ protected: int mprotecteddata; /* Other protected members */ void ProtectedMethod(void) const; /* Other protected methods */ private: int mprivatedata; 7

/* Other private members */ ; void PrivateMethod(void) const; /* Other private methods */ Overridden virtual functions must be marked with virtual, ( and override if using C++11). Always use explicit constructors for single argument constructors to avoid implicit casting. class Example Example(const int startvalue); ; class Example explicit Example(const int startvalue); ; Never give direct access to contained objects. Even returning read only contained objects makes code more dependant on other parts. Sometimes this is necessary, though it should never be necessary to give writable access. class Example ObjectType& GetObject(void) return mobject; ObjectType* GetObjectPtr(void) const return &mobject; private: ObjectType mobject; ; Acceptable: (with reasoning) class Example 8

const ObjectType& GetObject(void) const return mobject; const ObjectType* GetObjectPtr(void) const return &mobject; private: ObjectType mobject; You will notice in the wrong example, a non const pointer to mobject can be returned from a const method on the Example object, which can be indirectly changing the Example object. Namespaces Use namespaces to keep a collection of similar concepts contained. Never use a namespace or part of a namespace within a header file. Place function declarations within a namespace, then use the Scope Resolution Operator to define the function. Allow the compiler to help spot type safety errors that would be linker errors. Header File: namespace Example void DoSomething(const int withvalue); Source File: namespace Example void DoSomething(const int withvalue) /* codes */ void Example::DoSomething(const int withvalue) /* codes */ Functions Naming Must start with an uppercase letter, and uppercase first letter of each word thereafter. 9

Must be clear what the function does from reading the name. Accessors should be named as follows: GetVariableName(), SetVariableName(). Never abbreviate function names when the full word is more descriptive. HandleMouseClick(); Declarations / Prototypes Must always exist in a class or namespace of similar functions. Must use void when taking no parameters: void DoSomethingWithNothing(void); Getters and Setters should be named: GetVariable(), SetVariable() IsVariable() is also an acceptable getter for a boolean value. Function Separators Before and after each function a separator line should be placed to enhance readability, a new line should exist before and after the separator line. Separator line should extend from left margin to the max width specified in the Code Formatting section of this document. // // void Example::DoSomething(void) /* insert code */ // // Documentation Must follow the doxygen format, and be placed above the declaration of the function. Variables 10

Naming Must start with a lowercase, and uppercase first letter of each word thereafter. Must be clear what the variable is holding or purpose is, based on the name. Must never use single letter variables, even for temporary variables except for: loop counters, using (i, j or k) Never abbreviate variable or function names when the full word is more descriptive, except for: itr within a loop iterating a container. Member variables should start with an m following uppercase letter. Constants should start with a k following uppercase letter. Global variables should be used very rarely, if ever. Start with a g following uppercase letter, alternatively starting with the is also acceptable, typically reserved for singletons. Locals / Parameters: elapsedtime Class Members: misvisible Constants: kthisneverchanges; Globals: gvisualsystem, thevisualsystem Type Casting Should be avoided when possible (including int to float, unsigned to signed, or base to derived). Allow the type safety in the C++ language to show weak points in design by avoiding the need for type casting. When a cast is necessary, use C++ style casts: static_cast<>, dynamic_cast<> etc. Avoid reinterpret_cast<> at all costs since it is essentially undefined behavior. When a cast is needed, be sure to check value ranges and values to be as safe as possible. No C Style casts are allowed. They are potentially very dangerous, properly use static_cast, const_cast, reinterpret_cast and dynamic_cast; in general stick with static_cast. Note: dynamic_cast can only be used if RTTI is turned on, usually disabled by default. float val = 30.0f; int intval = (int)val; float val = 30.0f; int intval = static_cast<int>(val); 11

Dos and Don ts Do always initialize a variable to some expected value, NEVER allow it to be undefined! Switch Statements Should have a case for every value of an enum, consider if using the default case is appropriate to make this happen. Standard Template Library The STL is part of the C++ standard, use it instead of customized containers. Do not using namespace std, instead be explicit by writing: std::string value; When in need of a container use std::vector by default, otherwise choose the best container. Use std::string instead of C style char arrays. Use std::string::c_str() to exchange if needed. Macros / Preprocessor Macros should be avoided at nearly all cost, instead use Enums, Typedefs, Templates, and other built in C++ language features. When using a macro, be sure the use of a macro actually enhances readability, and avoid using clever preprocessing features that may change from compiler to compiler. Macro names shall be all lowercase with underscores between words. 12