CSCI 262 C++ Style Guide

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

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

Coding Standard for ECE3090 August 24, 2005

CSE 11 Style Guidelines

C Formatting Guidelines

Assignment Marking Criteria

CS 251 Intermediate Programming Coding Standards

EE 382 Style Guide. March 2, 2018

CS 142 Style Guide Grading and Details

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

CS 351 Design of Large Programs Coding Standards

Course Coding Standards

C Coding Standards. Alan Bridger UK Astronomy Technology Centre Mick Brooks, Jim Pisano National Radio Astronomy Observatory

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

Expanded Guidelines on Programming Style and Documentation

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

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

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

C Language Coding Standard

CSI33 Data Structures

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

Supplement D: Expanded Guidelines on Programming Style and Documentation

Formatting & Style Examples

A Fast Review of C Essentials Part II

Coding conventions and C++-style

EECS 280 C++ Coding Standards

Coding Standards for C

The Program Specification:

Perl Basics. Structure, Style, and Documentation

Coding Standards for C

This sheet must be the cover page for every programming assignment. Total

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

CSCI 1060U Programming Workshop

CS 152 Computer Programming Fundamentals Coding Standards

C++ Programming Style Guide

Documentation Requirements Computer Science 2334 Spring 2016

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Programming in C++ 4. The lexical basis of C++

Document Version: 0.6 Document Date: 15 April 2011 Prepared by: Y. Ermoline for ATLAS Level-1Calorimeter Trigger

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

2/25/2010. MITK Style Guide. Beautiful Code

Programming Style Guide v1.1

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

CSE 142/143 Unofficial Style Guide

Programming. C++ Basics

UEE1302 (1102) F10: Introduction to Computers and Programming

Chapter 2 Basic Elements of C++

Software Coding Guidelines

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

CSCI 2101 Java Style Guide

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

For the purposes of this course, the following coding guidelines are in effect.

Procedures, Parameters, Values and Variables. Steven R. Bagley

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

Chapter 2.5 Writing maintainable programs

TDDE18 & 726G77. Functions

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

Fundamentals of Programming Session 4

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

Computer Science C++ Placement Exam

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

INTRODUCTION 1 AND REVIEW

CSCI 161: Introduction to Programming I Lab 1b: Hello, World (Eclipse, Java)

Java Programming Style Guide

CE221 Programming in C++ Part 1 Introduction

Fundamentals of Programming Session 12

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Computational Physics - Fortran February 1997

Separate Compilation of Multi-File Programs

LECTURE 02 INTRODUCTION TO C++

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

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

PROGRAMMING STYLE. Fundamentals of Computer Science I

CS 351 Design of Large Programs

Section we will not cover section 2.11 feel free to read it on your own

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

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

Software Implementation (Writing Quality Code)

COMP 110 Project 1 Programming Project Warm-Up Exercise

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

III. Classes (Chap. 3)

C++ Programming Basics

CMSC 201 Fall 2018 Python Coding Standards

Functions and Recursion

Log4Delphi Coding Standards

3. NetBeans IDE 6.0. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

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

P802.3cf YANG Style improvements

Initial Coding Guidelines

Note : Your program must contain the following 6 functions :

Java Program Structure and Eclipse. Overview. Eclipse Projects and Project Structure. COMP 210: Object-Oriented Programming Lecture Notes 1

Guidelines for Writing C Code

User Interface Programming OOP/Java Primer. Step 3 - documentation

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Topic 2: C++ Programming fundamentals

Fundamentals of Programming

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

Key Differences Between Python and Java

Magic Set Editor 2 Template Creation Tutorial

Transcription:

CSCI 262 C++ Style Guide Christopher Painter-Wakefield and Mark Baldwin and Alex Anderson Last updated: 1/18/2018 Modified from: C++ Student Coding Standards Mark Baldwin Version 1.02 5/21/2013 2012 Mark Baldwin mark@baldwinconsulting.org http://baldwinconsulting.org Permission is granted to use and modify this document for academic purposes. Original authorship must be included in any derivative works.

1. Introduction Most companies use coding standards to ensure commonality and readability of code. The below standards are simplified versions of what can be found in industry. All coding assignments need to meet these standards. There are several reasons for this document: - Help prevent programming errors. - Encourage good programming practices. - Make it easier for instructors, tutors, and graders to read your code. These styles try to strike a balance between discipline and flexibility. There are times you may need to violate these standards. If you feel you have found such a situation, please discuss it with your instructor. Note that these style guidelines may not agree with the style guidelines/suggestions in your textbook or supplementary materials! Also, the naming style may conflict with the style used in third-party libraries; there are ways to minimize the impact of this, but they are beyond the scope of this document (and the course); just use the third-party names of things where needed. 2. Files 2.1. There should be one primary.cpp file named after the project or named main.cpp (either is acceptable). If you are given starter code, do not change the names of the files provided. 2.2. For very small projects, it is acceptable to combine all code (including template and class definitions) in a single.cpp file. 2.3. For large projects, each class will have two files, a.h file that contains the class declaration, and a.cpp file that contains the class definitions. The files must be named after the class. 2.4. For large projects, each class template will have a single.h file that contains the declaration of the class template and all of its methods. 2.5. Every header file must be guarded against multiple inclusions. Use one of the following techniques #pragma once (note, this only works with some compilers, so it might be good practice to use the alternative below) or // foo.h #ifndef FOO_H #define FOO_H. header body. #endif // end of foo.h

3. Names 3.1. All class, variable, method, and parameter names are lower case, with underscores separating parts of the name ( snake case ). Examples: class auto_structure string message_length; void resize(int new_size) 3.2. Exception: constants should be uppercase, with underscores separating parts of the name. const int DAYS_IN_YEAR = 365; 3.3. Use descriptive names Good int days_since_start = 0; // days since the start of the project Bad int d = 0; // days 3.4. Exception: temporary integer variables such as those used for loop counters are traditionally single letters (i, j, k). This is preferable to longer names. 4. Globals and Constants 4.1. Global Variables 4.1.1. Do not use global variables. All variables should be local to a method or to an object. 4.1.2. Exception: constants may be global. 4.2. Constants 4.2.1. Constants should be in all caps. 4.2.2. Do not use #define but instead use const construct. 4.2.3. Constants should be used for magic numbers (i.e. constants that have meaning) and strings wherever possible. 0 and 1 are not magic numbers. Good const int DAYS_IN_YEAR = 365; days = years * DAYS_IN_YEAR; Bad days = years * 365;

5. Indentation and Braces 5.1. Braces are optional only for single statements following a control structure and only if the single statement is on the same line as the control structure. Multiple control structures should not be on a single line. if (foo > max_foo) foo = max_foo; 5.2. Brace alignment 5.2.1. There are many accepted styles for aligning braces; one of the following two styles is recommended. Whatever style you adopt, you must be consistent throughout your program code. 5.2.2. Option 1: Opening brace is on same line as preceding code; closing brace is vertically aligned with left of preceding code: if (a == b) { a = b + 1; b = 0; else { a = 0; 5.2.3. Option 2: Braces align vertically: if (a == b) { a = b + 1; b = 0; else { a = 0; 5.3. Code inside of braces should be indented and aligned. If your code editor auto-indents, use the default indent for the editor (however, you may need to re-indent starter code if different). Acceptable choices for indentation are a tab character or four spaces. 5.4. Indentation should be consistent throughout a code file. To fix bad indentation, many editors have an auto indent function. It may be helpful to find out how this function works within your editor.

5.5. Never place more than one line of code on the same line. Only create one-line functions when your function code is only one line. Bad example: int pop() { _back--; _count--; return data_array[_back]; Good example: int pop() { _back--; _count--; return data_array[_back]; int peek() { return data_array[_back]; 6. Comments 6.1. The purpose of comments is twofold. First, they are there to provide understanding of what exactly the code is doing, both for the programmer and any third party (including the instructor). Second, they are a powerful tool in writing effective code. Hint: Try writing all your primary comments before creating code. Just like an outline for a paper, the comments then become the framework by which you can then provide the details of the actual code. 6.2. Generally, there are two types of comments. 6.2.1. Inline comments appear on the same line as the thing they describe. These are useful for presenting small bits of information about the particular line of code. 6.2.2. Block comments are one or more lines of comments that appear by themselves. These comments generally are used at the start of files, objects and methods, but they are also used to describe specific blocks of code and what is being accomplished. 6.3. All files should start with a block comment similar to this: /* */ File Name: the name of the file Author: your name Course and Assignment: Course number and assignment Description: Describe what is contained in the file

6.4. All class and class template declarations should start with a block comment similar to this: /********************************************************** class name describe what the class does **********************************************************/ 6.5. Function definitions are ideally preceded with a block comment explaining the purpose of the function and its parameters, unless these are abundantly clear from the context and names of the function and its parameters. 6.6. Variable declarations may be followed by an inline comment describing the purpose of the variable, but clear naming of variables is preferred over commenting. 6.7. Comments are needed in the body of a routine if the logic or code is not obvious. 6.8. Do not comment on the obvious. Comments like below are worthless counter++; // increment the counter