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

Similar documents
Java Style Guide. 1.0 General. 2.0 Visual Layout. Dr Caffeine

CSCI 262 C++ Style Guide

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

Coding Standards for C

Coding Standards for C

Practical C++ Programming

CS 142 Style Guide Grading and Details

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

CSE 11 Style Guidelines

C Language Coding Standard

CS 251 Intermediate Programming Coding Standards

Absolute C++ Walter Savitch

Appendix G C/C++ Notes. C/C++ Coding Style Guidelines Ray Mitchell 475

EE 382 Style Guide. March 2, 2018

C++ Programming Style Guide

Full file at C How to Program, 6/e Multiple Choice Test Bank

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

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

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

Perl Basics. Structure, Style, and Documentation

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Coding Standards for Java

COMP 110 Project 1 Programming Project Warm-Up Exercise

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5.

CS : Programming for Non-majors, Fall 2018 Programming Project #2: Census Due by 10:20am Wednesday September

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

CSE 230 Intermediate Programming in C and C++ Functions

QUIZ Friends class Y;

CS 351 Design of Large Programs Coding Standards

CS : Programming for Non-majors, Summer 2007 Programming Project #2: Census Due by 12:00pm (noon) Wednesday June

CS 211 Programming Practicum Fall 2018

Problem Solving with C++

Short Notes of CS201

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.

CS201 - Introduction to Programming Glossary By

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

2. Numbers In, Numbers Out

Note : Your program must contain the following 6 functions :

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Course Coding Standards

Tips from the experts: How to waste a lot of time on this assignment

Java Programming Style Guide

CSCI 2101 Java Style Guide

POFT 2301 INTERMEDIATE KEYBOARDING LECTURE NOTES

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

Introduction to C Programming

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Program Organization and Comments

UEE1302 (1102) F10: Introduction to Computers and Programming

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Documentation Requirements Computer Science 2334 Spring 2016

LAT-TD-xxxxx-xx May 21, 2002

Have examined process Creating program Have developed program Written in C Source code

CSE 374 Programming Concepts & Tools

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

SML Style Guide. Last Revised: 31st August 2011

Guidelines for Writing C Code

Programming. C++ Basics

MULTIMEDIA TRAINING KIT INTRODUCTION TO OPENOFFICE.ORG WRITER HANDOUT

Use of scanf. scanf("%d", &number);

CS106A, Stanford Handout #30. Coding Style

2. Numbers In, Numbers Out

Computer Science C++ Placement Exam

CS 152 Computer Programming Fundamentals Coding Standards

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Chapter 10 Introduction to Classes

Computer Programming : C++

Creating Universally Designed Word 2013 Documents - Quick Start Guide

CMSC 201 Fall 2018 Python Coding Standards

CE221 Programming in C++ Part 1 Introduction

Creating Universally Designed Word 2010 Documents - Quick Start Guide

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

COSC345 Software Engineering. Documentation

The Program Specification:

Lecture 18 Tao Wang 1

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Using C++, design an Abstract Data Type class named MyGrades. The class must have the following private members :

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Formatting & Style Examples

CMPSC 111 Introduction to Computer Science I Fall 2016 Lab 2 Assigned: September 7, 2016 Due: Wednesday, September 14, 2016 by 2:30 pm

CS 470 Operating Systems Spring 2013 Shell Project

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

CS 2704 Project 2: Elevator Simulation Fall 1999

COMP26120 Academic Session: Lab Exercise 2: Input/Output; Strings and Program Parameters; Error Handling

COIMBATORE EDUCATIONAL DISTRICT

Appendix M: Introduction to Microsoft Visual C Express Edition

Decision Logic: if, if else, switch, Boolean conditions and variables

Lecture 02, Fall 2018 Friday September 7

19. Bulleted and Numbered Lists

CS 351 Design of Large Programs

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Programming in ROBOTC ROBOTC Rules

Objectives. In this chapter, you will:

Chapter 2 Basic Elements of C++

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

Transcription:

C++ Style Guide 1.0 General The purpose of the style guide is not to restrict your programming, but rather to establish a consistent format for your programs. This will help you debug and maintain your programs, and help others (including your instructor) to understand them. In the long run, this will probably help you, since one focus of these courses is to build a foundation for later courses. 2.0 Visual Layout You should format your program source code making use of page boundaries and blank lines to separate code groups, functions and groups of logically related blocks. As a general guideline, no block should be longer than one page. It is generally better to have some blank space at the end of a page than to start a code block and run it over onto the next page. There are exceptions. Ask if you are uncertain; it saves a lot of guesswork. Output from a program should be both informative and professionally formatted. A person reading the output should be able to determine whether the program works reasonably, without knowing the internal details of the program. A printed page must be 8.5 x 11 inches. Margins must be set between ½ and 1 inch all around. You must use a fixed-width font (Courier, or Courier New works well) with a font size between 10 and 12 points. 3.0 Indentation and Whitespace Indentation is used to highlight the block structure of the code and should be used consistently. The proper use of whitespace and indentation makes the structure of the program evident from the layout of the code. You must use proper indentation, which in this class is three spaces. Use spaces rather than tabs, because printers and monitors usually have different tab spacing. Set your programming text editor to insert spaces for tabs. Vertical whitespace is the use of blank lines to help set off blocks of code and increases readability of the program. Code with no vertical whitespace is difficult to follow. No line of code should wrap beyond the right margin. If a line of code becomes too long, break it up into pieces with carriage returns. Remember the compiler largely ignores extra whitespace in your programs (but be careful with strings). The only thing that may follow an opening brace ( { ) on a line of code is a comment. The block of code enclosed by a set of braces must be indented one level (three spaces) inside the enclosing braces. With one exception discussed later, a closing brace must always be on a line by itself and must be at the same level of indentation as the line containing the corresponding opening brace. Lines of code may be indented more than three spaces when the readability of the code is improved. 3.1 Comments Comments should be indented consistently with the block of code they describe. Comments can be divided into two general categories, strategic and tactical. Strategic comments are used to give the reader a general overview of what is going on. These comments appear at the beginning of files and preceding class, method, and class variable declarations. Strategic comments tend to be written in sentences with an emphasis on explaining the big picture of what the block of code is designed to do. C.S. Eagle Page 1 of 7 Last Revised: 06-May-2007

Tactical comments are designed to explain tricky areas of code, what parameters do, and hints about the control flow of the program. These comments should be intermixed in the code when needed. They should be shorter and in bullet format, and may be in inline format If you use sensible and informative naming, block structure, indentation, and straightforward programming, your code will be mostly self documenting. This is what you should strive for. Keep comments simple. Do not attempt to explain what is obvious from the code itself. Numerical constants must have a comment explaining the value if it is not self-evident. 3.2.1 File Headers Each file must have a descriptive comment header block at the beginning as follows: // File: <name of this file,.h or.cpp> // Name: <your name> // Course: // Project # // Operating Environment: <Unix, Win95, Win98, etc> // Compiler: <gcc, g++, Borland, Visual Studio, etc> // Date: // Description: <Describe the file contents in brief detail> 3.2.2 Header Comments -- classes Each class declaration must have an appropriate comment header giving a brief description using the following format: // Class: <name of this class> // Purpose: <The purpose of this class> EXAMPLE: // Class: Date // Purpose: Represents a date object by storing and validating // month, day and year values class Date { public: Date(int, int, int); private: int month; int day; int year; } C.S. Eagle Page 2 of 7 Last Revised: 06-May-2007

3.2.3 Header Comments -- functions Each function declaration must have an appropriate comment header giving a brief description using the following format: // Function: <name of this function> // Return Value: <what the return value represents> // Parameter: <The name and purpose of each parameter> // Purpose: <The purpose of this function> EXAMPLE: // Function: cubeit(float) // Return Value: The cube of the supplied parameter // Parameter: value the number to cube // Purpose: Cube the number supplied float cubeit(float value) { return value * value * value; } Note: Function header comments should appear in BOTH the header (.h) and source (.cpp) files. 4.0 Control Structures The use of braces is required (for style purposes, not necessarily syntax purposes) for all control structures, including those with a single statement in the body. Avoid the overuse of continue and break in loops. Always use a default case in switch statements. Use a break statement to end all cases (including default) in a switch statement. The use of // end if, // end while, // end for and // end switch comments after the corresponding ending right brace is optional. For CS2900, you must place the opening brace of a block of statements on the same line as the statement to which it belongs. Indentation and formats for control structures are as follows: The if statement: if (boolean expression) { } else if (boolean expression) { // else if is optional } else { // else is optional } // end if <optional comment> The for statement: for (expression; expression; expression) { } //end for <optional comment> The do/while statement: do { } while (boolean expression); C.S. Eagle Page 3 of 7 Last Revised: 06-May-2007

The while statement: while (boolean expression) { } //end while <optional comment> The switch statement: switch (expression) { case <constant expression>: break; case <constant expression>: break; case <constant expression>: //falls through //a case with no break needs this comment default: //all switch statement should have default //these statements are optional break; //all cases should have a break } //end switch <optional comment> 5.0 Naming Variables must have meaningful names/identifiers. This must be balanced against using names which are too long, which can obscure the code. (Fifteen or so characters approaches the too long limit.) Single letter variables or constants should not be used. An exception to this rule is when it is common practice to identify something with a single letter. An example of this is the coordinate system (X, Y, and Z). A second exception occurs in the use of loop counter variables where it is common practice to use variables like i and j as counters in for loops. Only class names should begin with a capital letter, all other identifiers (function names and variable names) should begin with a lower case letter. An identifier consisting of multiple names shall have each name distinguished by making the first letter of each name part upper case (e.g. redcarcolor). Constants should be named in all upper case letters. Examples: const int RED_CAR_COLOR = 1; class RedCarColor { int redcarcolor; Do not use the lower case letter `L, or the letter `O in names unless they are part of normal words. This is to avoid confusion with the numbers 1 and 0. For example, is cel1 c- e - l ONE or c e l l? Avoid names that differ only in case, look similar, or differ only slightly. For example, InputData, InData and DataInput will certainly be confusing if used in the same program. Names of functions should reflect what they do (printarray), or what they return (getage). Boolean variable names should sound like Yes/No things isempty and isfinished are possible examples. 6.0 Globals A global is storage space defined outside the scope of any function. Globals may be constants or variables (depending on the use of the keyword const). The use of global variables is considered poor programming practice. For CS2900, the use of global variables is prohibited. C.S. Eagle Page 4 of 7 Last Revised: 06-May-2007

7.0 Classes Classes are declared in the following order: friend classes and functions public member functions Constructors Destructors Others public data members protected member functions Constructors <if any> Others protected data members private member functions Constructors <if any> Others private data members 8.0 Functions The main() function should be in a file of its own, must always return type int and contain a valid return statement. Every function, other than main(), should have a prototype. Each function shall have no more than one return statement. The only exception is when multiple return statements will allow returning only unnamed objects, but a single return statement would require return of a named object. Each function (starting with the function comment) must be separated from other code by at least two blank lines. 9.0 Files 9.1 Source Files Each source file must include all the necessary information to support its independent compilation. Do not add additional dependencies. Each file must include only the necessary information to support independent compilation. Do not #include the universe to see if that might help. 9.1.1 Header Files All header files will have a ".h" extension. For multi-file projects, each C++ source file must have an associated header with the same base name. Example: myfile.cpp, myfile.h. No header file is required for the source file containing the main() function. In CS2900, we will not define more than one class per header file. C.S. Eagle Page 5 of 7 Last Revised: 06-May-2007

All header file you create must use the #ifndef construct to avoid multiple inclusion. For a header named myfile.h, this looks like: #ifndef MYFILE_H #define MYFILE_H //File comment goes here, see section 3.2.1 //#include statements go here //using statements go here //Function prototypes go here #endif //End of file myfile.h 9.1.2 Source Files All source files will have a ".cpp" extension for Windows programs, and a ".C" or ".cc" extension for Unix programs. For classes, all member functions of a class will be defined in a single source file. For CS2900, a source file that contains class member functions may contain member from function from only a single class. Source file organization should look like the following (for a file named myfile.cpp): //File comment goes here, see section 3.2.1 //#include statements go here //using statements go here //Function bodies go here //End of file myfile.cpp 9.2 Data Files If you open a data file for reading/writing, close the file as soon as you are done reading/writing to it. Minimize multiple file opening/resets/closing if it can reasonably be avoided, as disk access times can be time-intensive and prone to failures. Avoid using absolute path addressing when referring to data files unless referring to a file on the floppy disk drive. Use relative addressing based upon the location of the executable if possible. 10.0 Miscellaneous Important Stuff Always use the most appropriate operator or construct for the work you want it to do. Good design and clarity take precedence over optimization. Do not declare or use more variables than are necessary. Always use the principle of least privilege (grant only that access necessary to do the desired work and no more). Numerical constants ("magic numbers") must not be coded directly. The only allowable exceptions are for 0, 1 or -1, and those which are not "likely" to change; for example code determining if a number is even can use the number "2" since it is not likely to change. Numerical constants must have a comment explaining the value if it is not evident from the name. C.S. Eagle Page 6 of 7 Last Revised: 06-May-2007

You should test floating point numbers with <= or >=. Never use exact comparisons with floating point numbers, even when comparing to 0.0. The appropriate means of comparison is to define a value (in engineering lingo, epsilon ) that represents the maximum difference by which two values may differ and still be considered equal. You may only use old C style input/output routines like scanf or printf when they are the specific topic of an assignment or lab exercise. If you use any public domain or shareware code, or code from one of the textbooks, there are three requirements: First, you must ask my permission. Second, you must credit the source in the header block and be specific as to where it comes from. Third, if it is going into your program, you MUST understand it completely. If you cannot explain what it is doing, then it does NOT go into a program that you turn in for grading. If asked to explain it and you cannot do so to the instructor s satisfaction, you will not get credit for it and will have your grade adjusted accordingly. The goal is to have you understand what is going on in the program. Other student s code does NOT qualify as shareware or public domain code for the purposes of this course (i.e. YOU MAY NOT USE ANY OTHER STUDENTS CODE WITHIN YOUR PROGRAMS). You may not use gouge code from former students within your programs. You may not confer with any other student in this class regarding the coding phase of any project. Any students turning in identical or nearly identical work will receive grades of zero for that project. Original programs are like fingerprints, no two are identical. You should test your code. It should produce the correct results for normal test conditions as well as boundary conditions like short or empty lists. DO NOT modify project specifications without explicit permission from your customer (the instructor)! Even if you think you have explicit permission to modify project specifications, modifications from the written assignment provided must be clearly and completely documented in your code and in your project documentation. 11.0 Project Guidance: What to turn in You must turn in the following information for each project: 1. Hard copy printout of each source file you create for the project 2. An electronic copy of each of your source files. E-mail me all of your source files as attachments. Email only the.h files and.cpp files that you have created, along with a Makefile to build your project. Do not send any.obj,.exe, or related files. 3. Any other deliverables asked for by the assignment. C.S. Eagle Page 7 of 7 Last Revised: 06-May-2007