1 / 16

Introduction to Coding Standards

Introduction to Coding Standards. Phil Pratt-Szeliga CSE 784 Fall 2009. Overview. Definitions Motivation Small Example Coding Standard Conclusions References. Definitions. A coding standard defines the style of your source code including (but not limited to):

Download Presentation

Introduction to Coding Standards

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • http://en.wikipedia.org/wiki/Programming_style#Coding_conventions_for_languages • C,C++,C#,Java • Coding conventions for projects • http://en.wikipedia.org/wiki/Programming_style#Coding_conventions_for_projects • Linux Kernel

  16. References 1.http://en.wikipedia.org/wiki/Programming_style 2.http://www.possibility.com/Cpp/CppCodingStandard.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

More Related