Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Coding Standards Phil Pratt-Szeliga CSE 784 Fall 2009.

Similar presentations


Presentation on theme: "Introduction to Coding Standards Phil Pratt-Szeliga CSE 784 Fall 2009."— Presentation transcript:

1 Introduction to Coding Standards Phil Pratt-Szeliga CSE 784 Fall 2009

2 Overview  Definitions  Motivation  Small Example Coding Standard  Conclusions  References

3 Definitions  A coding standard defines the style of your source code including (but not limited to): Indentation, Bracket Placement Variable, Function and Class Names Comments Declaring Pointers Higher level aspects (often to avoid obscure pitfalls) And more  A standard is defined for a language or project  If everyone follows the standard the code is easy to read

4 Motivation  A coding standard can be useful for the following reasons Programmers can quickly read someone else’s code on a project New people can get up to speed quickly People new to a language are spared the need to develop a personal style (adapted from reference #2)

5 Indentation and Bracket Placement Examples if (hours < 24 && minutes < 60 && seconds < 60) { return true; } else { return false; }  ANSI C++ Style

6 Indentation and Bracket Placement Examples if (hours < 24 && minutes < 60 && seconds < 60) { return true; } else { return false; }  Kernighan and Ritchie Style

7 Indentation and Bracket Placement Examples if ( hours< 24 && minutes< 60 && seconds< 60 ) {return true ;} else {return false ;}  Hopefully no one’s style

8 Indentation and Bracket Placement Example  Clearly we want to avoid the use the last option.  The coding standard can define that the last option shouldn’t happen  It also chooses between the two reasonable options This makes all the code on a project look the same

9 Commonly used Identifier Case  Upper Case with Underscores: THIS_IS_AN_EXAMPLE  Lower Camel Case: thisIsAnExample  Upper Camel Case: ThisIsAnExample  Lower Case with Underscores: this_is_an_example

10 Example Coding Standard – Variable Names  Classes have Upper Camel Case ex: PizzaFactory  Functions have lower Camel Case ex: myFunction  Member variables begin with m and have Upper Camel Case ex: mMemberVariable  Local variables have lower case with underscores ex: my_local_variable

11 Example Coding Standard – Indentation, Spaces and Tabs  No tabs are used, set your editor to enter spaces when you hit tab  When you hit tab, 2 spaces are entered  There is only one space between tokens  K&R Placement of braces if(condition) { basic_block } else { basic_block }

12 Example Coding Standard – Declaring Pointers  C codestyle int *z;  C++ codestyle int* z;  But what about two variables?  C codestyle int *z, *y;  C++ codestyle int* z, y; (y is an int, not a pointer in this case)

13 Example Higher Level Aspect  In C++ the order of initialization of member variables depends on the order they are declared in the header file  It won’t work if you write code that tries to initialize them in a different order class MyClass { int x; int y; }; MyClass::MyClass() : y(0), x(y) {}  In the coding standard it could be specified that you should order your initialization in the order of declaration New people to the language will appreciate it

14 Conclusions  Some employers or projects will require you to conform to the coding standard This may make life easier for you if you sometimes ask: “What is the best way to do it?”  When you join a project you can ask: “Is there a coding standard?”  The coding standard could be specified by referencing the project’s coding standard document in the “Process Requirements” section of the B-Spec.  If a coding standard is specified you should use a tool to automatically check the standard during qualification

15 Coding Standards on the Web  Coding conventions for languages ng_style#Coding_conventions_for_lang uages ng_style#Coding_conventions_for_lang uages  C,C++,C#,Java  Coding conventions for projects ng_style#Coding_conventions_for_proj ects ng_style#Coding_conventions_for_proj ects  Linux Kernel

16 References 1.http://en.wikipedia.org/wiki/Progra mming_style 2.http://www.possibility.com/Cpp/Cp pCodingStandard.html#important 3.http://en.wikipedia.org/wiki/The_C _Programming_Language_(book) 4.http://en.wikibooks.org/wiki/C%2B %2B_Programming/Code_Style


Download ppt "Introduction to Coding Standards Phil Pratt-Szeliga CSE 784 Fall 2009."

Similar presentations


Ads by Google