Cppstandards

Page 1

C++ Coding Standards

Fall 2014

Contents 1

Introduction

2

2

Platform Independence

2

3

Project and Source Code File Names

3

4

Compiler Directives

3

5

Required Comments

4

6

Identifiers

5

7

Variables

5

8

Functions

5

9

Source Code Formatting

6

10 Grading Criteria 10.1 Catastrophic Errors . . . . . . . . . . . . . . . . . . . . . . . . . 10.2 Major Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.3 Minor Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CS 161

Page 1

7 7 8 8

September 19, 2014


C++ Coding Standards 1

Fall 2014

Introduction

The purpose of this document is to communicate the required programming style for all programs submitted for grading in this class. All code must conform to these standards in order to receive full credit. In other words, a program that merely works properly is not good enough - the program must be properly written and submitted in order to receive full credit.

2

Platform Independence

All code must conform to the current ISO/IEC C++ standards, and avoid systemspecific syntax. Code should compile and execute in any standard C++ environment on Windows, OS X, or Linux systems. The following standards help enforce platform independence: 1. The declaration of the main function must be one of the following: (a) int main() (b) int main(int argc, char **argv) 2. Avoid using the system command, for example system("PAUSE") 3. Avoid using the stdafx library - this is specific to Visual Studio. 4. Avoid using any system-supplied library that ends in .h, e.g. stdlib.h. These are C libraries, not C++ libraries. 5. Avoid any C++ code that results in undefined, unspecified, or implementationdefined behavior (don’t worry - these terms will be covered in CS 161). This is necessary to ensure your code behaves the same on different systems. A common source of problems in this area is Visual Studio’s use of Windowsspecific file templates that contain non-standard C++ data types. If your Visual Studio project contains #include "stdafx.h" or

CS 161

Page 2

September 19, 2014


C++ Coding Standards

Fall 2014

int tmain(int argc, TCHAR* argv[]) then your submission is incorrect.

3

Project and Source Code File Names 6. All projects for all assignments should be named main. You will need to develop some method to distinguish one project from another that does not involve changing the name of the project. 7. The source code file that contains the main function must be named main.cpp 8. The source code file that contains the compiler directives for your program must be named main.h 9. The only compiler directive permitted in the main.cpp file is the #include for main.h.

4

Compiler Directives

A compiler directive or preprocessor command is a statement in your source code that begins with a #, such as a #include <iostream> statement. Compiler directives are instructions to the compiler that affect how your source code is compiled, but do not generate any machine code themselves. You may use several different compiler directives in your C++ programs, and they should conform to the following standards: 10. Every submission must include a main.h header file that contains compiler directives for the assignment. 11. All #include and #define statements used in the project must be declared in the main.h file, except: (a) The main.cpp file should contain a #include ‘‘main.h’’ statement. (b) File scope parameterized macro definitions (not taught, but permitted, in CS 161) should be placed in the .cpp file where they are used

CS 161

Page 3

September 19, 2014


C++ Coding Standards 5

Fall 2014

Required Comments

The first line of your source code files are used by the grading software to identify the person who will receive a grade for the assignment. If this line is omitted or improperly formatted then your grade may not be properly recorded. 12. The first line of all source code files must be a single-line commend that begins with // and contains (a) The course CRN, e.g. 12206 (b) Your G-Number, e.g. G012345678 (c) Your PCC email address, e.g. cdjones@online.pcc.edu (d) The assignment name, e.g. Program 1 and is formatted with single spacing, such as //41708 G012345678 cdjones@online.pcc.edu Program 1 The format for remaining comments in your source code is up to you. You must, however, include comments in your source code if you worked in a group or used any code you did not develop by yourself, i.e. 13. If you worked with other persons to complete the assignment you must include a comment in your source code that lists the names of all persons who helped you complete the assignment 14. If you use any code that you did not wholly create you must cite your sources in the program comments. (a) If you found code on the Internet and used it (even if you changed a few things) then you must list the website URL where you found the code in your program comments (b) If you found code in a book and used it (even if you changed a few things) then you must list the book’s title, author, and page number where you found the code in your program comments Give credit where credit is due - otherwise you are in violation of PCC’s Academic Integrity Policy.

CS 161

Page 4

September 19, 2014


C++ Coding Standards 6

Fall 2014

Identifiers

An identifier is a name, and you will create many different identifiers in your C++ programs. Any identifier that you create must conform to the following standards: 15. Identifiers must begin with a lower case letter for variables, functions, constants, and declarations 16. Identifiers must contain 3 or more characters 17. Identifiers must not contain the underscore character (“ ”) 18. Identifiers must use “camel case” to separate words, e.g. thisIsMyVariableName

7

Variables

A variable is a location in memory with an address, size, and encoding that is used to store one or more values. The rules for using variable identifiers in CS 161 are: 19. Global variables are not permitted in CS 161, including extern and file scope variables. All identified variables must be file-scope variables. 20. All local variables declared in a block must be initialized when they are declared 21. All variables created by a new command must be released using a delete command before your program terminates

8

Functions

A function is a named block of code that performs some task in your program. You will create several different functions in CS 161, and your functions must conform to the following: 22. All user-defined functions must be explicitly declared before being defined. A function declaration consists of the return type, name, and parameter data types only. A function definition contains all of these, plus parameter names and the body of the function. For example:

CS 161

Page 5

September 19, 2014


C++ Coding Standards

Fall 2014

• Sample function declaration: int main(int, char **); • Sample function definition: int main(int argc, char **argv) { cout << ‘‘Hello World’’ << endl; return 0; } 23. All user-defined functions must be declared before the main function is defined 24. All data passed to a function must be passed as a parameter 25. Functions may not modify their parameters in any way 26. All non-void functions must exit through an explicit return statement. void function may omit the return statement.

9

Source Code Formatting

All source code submitted in this class must conform to the following standards, regardless of what tools were used to develop the code. 27. An indent level is 4 spaces 28. Tab characters should not be used for indentations - use spaces instead 29. Indentation standards: (a) Indent new lines 1 level within parentheses and code blocks (b) Indent case contents (c) Indent case labels 30. New line standards: (a) Open braces ({) should appear on a line by themselves indented with their parent line

CS 161

Page 6

September 19, 2014


C++ Coding Standards

Fall 2014

(b) Close braces (}) should appear on a line by themselves indented evenly with their open brace (c) else statements should appear on a new line indented evenly with their corresponding if (d) while clauses should appear on a new line indented evenly with their corresponding do statement 31. Spacing standards: (a) Insert a space between function names and the open parentheses of parm lists (b) Insert a space between arguments in a function’s parm list (c) Insert space before and after commas (d) Insert space before and after all binary operators (e) Insert space before and after assignment operators 32. All control expressions must include operators

10

Grading Criteria

All program submissions are graded on a scale that ranges from 0 (very bad) to 10 (very good). No resubmission is permitted once a grade is issued for an assignment, i.e. once a grade is awarded you may not correct the errors and try again.

10.1

Catastrophic Errors

If any of the following conditions occur your submission will receive zero (0) points: 1. Any violation of PCC’s Academic Integrity Policy 2. Submission does not contain the correct files(s) with the correct format(s) and names(s) 3. Files can not be compiled or linked. 4. Submission is not a solution to the assigned problem. CS 161

Page 7

September 19, 2014


C++ Coding Standards

Fall 2014

The main theme here is that you must make sure your program compiles and executes before submission. If you are unable to successfully compile your program contact your instructor before the submission deadline.

10.2

Major Errors

Each of the following errors will result in a 1 point (10%) reduction of your grade: 5. The first line of the main.cpp file is not the required comment line (item 12 of the coding standards) 6. Program does not accept the required input 7. Program does not produce the required output 8. Program does not terminate 9. A specified assignment requirement is not satisfied. • Each missed requirement is an additional 1 point (10%) reduction. The main theme here is that your program should work as required.

10.3

Minor Errors

10. Each line of source code that contains a violation of the class coding standards will reduce your grade by 0.1 points (1%)

CS 161

Page 8

September 19, 2014


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.