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

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

Review of the C Programming Language for Principles of Operating Systems

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

Tokens, Expressions and Control Structures

Review of the C Programming Language

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

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

Coding Standards for C

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

Coding Standards for C

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Fundamentals of Programming

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

INTRODUCTION 1 AND REVIEW

C Formatting Guidelines

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

Axivion Bauhaus Suite Technical Factsheet MISRA

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

Motor Industry Software Reliability Association (MISRA) C:2012 Standard Mapping of MISRA C:2012 items to Goanna checks

3. Java - Language Constructs I

Programming for Engineers Iteration

C Language, Token, Keywords, Constant, variable

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

Variables. Data Types.

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Fundamental of Programming (C)

CSCI 171 Chapter Outlines

Motivation was to facilitate development of systems software, especially OS development.

DEPARTMENT OF MATHS, MJ COLLEGE

6.096 Introduction to C++ January (IAP) 2009

C Syntax Out: 15 September, 1995

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

MRO Delay Line. Coding and Documentation Guidelines for Prototype Delay Line Software. John Young. rev June 2007

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

C-LANGUAGE CURRICULAM

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

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

A Fast Review of C Essentials Part I

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

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

Motivation was to facilitate development of systems software, especially OS development.

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

EE 382 Style Guide. March 2, 2018

Practical C++ Programming


CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

MISRA-C:2012 Standards Model Summary for C / C++

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

C Coding standard. Raphael kena Poss. July Introduction 2. 3 File system layout 2

Chapter 2. Procedural Programming

C: How to Program. Week /Mar/05

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

CSE 374 Programming Concepts & Tools

CS201 Some Important Definitions

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Computer Science C++ Placement Exam

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Chapter 2 - Introduction to C Programming

Basic Types, Variables, Literals, Constants

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Chapter 2: Functions and Control Structures

UNIT- 3 Introduction to C++

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

COMPUTER APPLICATION

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

CSCI 262 C++ Style Guide

Introduction to C# Applications

Chapter 2: Introduction to C++

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Full file at

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Chapter 2: Using Data

Programming. C++ Basics

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Axivion Bauhaus Suite Technical Factsheet AUTOSAR

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

CT 229 Fundamentals of Java Syntax

Introduction to C++ Systems Programming

Model Viva Questions for Programming in C lab

Introduction to Programming

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

from Appendix B: Some C Essentials

Transcription:

APPENDIX A : Example Standard <--Prev page Next page --> If you have no time to define your own standards, then this appendix offers you a pre-cooked set. They are deliberately brief, firstly because standards should not be too long as no-one will remember them all, and secondly because of space limitations in the book! They are targeted at a fairly stable organization with about average programmers (neither wizards nor neophytes). In comparison to other known sets of standards they are fairly middling, more detailed than some and less detailed than others. The brevity means that there are no explanations as to why standards are selected, although reasons are available in the rest of this book. Other space-saving tricks are also used, such as including standards within a template format and emphasizing the basic principles to use in the event of there being no specific standard for a given situation. As an alternative to taking these standards as a complete set, you may use them as a basis for you own standards, rejecting those parts you really cannot agree with, and adding parts you consider to be missing. MegaByte Company Coding Standards These are the C coding standards for the MegaByte company. All project team are expected to adopt and ensure usage of these standards. Variations are permitted if agreed by the project team council. 1. Basic Principles The fundamental purpose of these standards is to promote maintainability of the code. This means the code must be readable, understandable, testable and portable. The Four Cornerposts Always keep the cornerposts in mind. Where there is conflict, compromise towards readability. 1. Keep it simple. Break down complexity into simpler chunks. Clearly comment necessary complexity. 2. Be explicit. Avoid implicit or obscure features of the language. Say what you mean. 3. Be consistent. Use the same rules as broadly as possible. 4. Minimize scope. This includes logical and visual scope. Be more explicit about items with wider scope. Other general points Minimize performance tweaks. Be aware of real savings made. Use a well defined error handling and diagnostics system. Ensure data integrity and enable recovery. Make data and process work together to reflect the problem. Where you have a coding decision and there is no direct standard, then you should always keep within the spirit of the standards. Files Divide header files to reflect the files they serve. For each subsystem, have: one or more interface files; one common file; lower level files as required. #include "megabyte.h" in all files - this includes common items such as definitions of TRUE and FALSE. Keep files less than 1000 lines or about 20 pages. Make files functionally cohesive. Aim for one public function per file, plus its private functions. Allow more public functions if they share several private functions. Minimize coupling between modules. 2. Commenting Make every comment count (but don't use this as an excuse for under-commenting). Keep code and comments visually separate. Use header comments for all files and functions. Smaller comments are ok for smaller, private functions.

Use block comments regularly. Use trailing comments for special items. see Layout Template section for examples. 3. Naming Name separate words each with an initial capital (e.g. VariableName). Use abbreviations consistently, and document common usages (eg. 'Buf' for buffer). Use two/three capitals plus underscore to show functional prefix on all global items (functions and data) (e.g. XY_VariableName). Document prefixes used. Use between 2 and 20 characters per name. Use longer names for items with wider scope. Name functions with verb-noun (e.g. ProcessVariable); name variables with nounabstract_noun (e.g. WallHeight). Use the following prefixes to denote special types: p = pointer, a = array, u = unsigned, b = boolean. (e.g. pawall = pointer to an array). 4. Layout Aim for one action per line. Use 'comb' rather than 'toothbrush' layout. Use vertical alignment. Separate code 'chunks' with blank lines or comments. No spaces within object references. No space between unary operator and operand (but space on other side). Balance spacing around binary operators (usually one space). Use parentheses to emphasize chunks in expressions. Use precedence rules to guide wrapping of expressions. When wrapping lines, indent to past logical start of wrapped item. Always use braces, even for a null statement. See Layout Template below for examples and further standards. 5. Usage Use the appropriate construct for the appropriate situation. Avoid deep nesting (of statements, parentheses and structures). Aim for normal maximum of three levels (deeper excursions should be short and infrequent). Minimize use of conditional expressions. Don't use implicit evaluation order in equality-expressions (use separate statements instead). Minimize use of comma operator. Use casts to maintain type in expressions, assignments and parameters. In 'if..else', put the major action in the 'if' clause. In control loops, aim for the controlling equality-expression to remain true for most of the loop. Use 'for' expressions only for control of the loop. Make 'case's short and independent of one another. Always use a 'default'. Only use 'goto' for error handling, jumping outwards and downwards. Use 'break' to escape a loop under abnormal, rather than normal, conditions. Avoid 'continue'. Use 'if..else' instead. Always declare function type, even 'int' and 'void'. Use function prototypes over 'original' style declarations. Avoid functions with large numbers of parameters. Match the type of actual and formal parameters (possibly using casts). Don't re-#define 'C'. Minimize macro usage, especially tricky usage. Parenthesize parameters and the overall expression. If it contains multiple statements, use 'do macro body while( FALSE )'. In declaring integers, default to using 'int'. Use 'char' and 'short' only to save space and only use 'unsigned' and 'long' where explicitly needed. Use up to two 'register' variables only at specific performance enhancement points. In declaring floating points, default to 'double', only using 'float' to save space. Compare floating point numbers only with '<=' or '>='. Use 'typedef' over #define or 'struct Tag..;'. Do 'typedef' common types, but don't over-use 'typedef'.

Minimize use of global data. Rather, hide data with or in owning functions. Keep structures functionally cohesive. Nest them where appropriate, using a consistent approach (nesting actual structures or pointers). Minimize use of unions. Use array indexing over pointers into arrays. Use NULL for the null pointer. Cast it, too. Always cast pointers when moving between types. Never use 'magic numbers' - always #define. (Exception: simple usage, such as initializing a count to 0). Use enum over #define.

6. Layout template 6.1 Header File /H FILENAME : filename.h DESIGN REF: XXXXNN DESCRIPTION : Simple description of functional areas served USAGE : Description of when file should be #include'd NOTES : Other help for the reader, including references. Copyright (c) MegaByte Co. 1990, 1992. All rights reserved. CHANGES : REF NO DATE WHO DETAIL DDMMMYY Name First version XXNNNN DDMMMYY Name Name of item changed H/ #ifndef filename_h #define filename_h / INCLUDE FILES / /---- system and platform files -----------------------------------------/ #include <filename.h> header files not defined within the program /---- program files -----------------------------------------------------/ #include "megabyte.h" Company standard: includes TRUE, FALSE, etc. #include "program.h" 'global' header file for program #include "filename.h" other files required / FILE CONTEXT / #define's, typedef's, struct's, union's, enum's for use in more than one functional area below. <page break> /A NAME: Simple name for functional area served by this section USAGE: Description of where, how, etc. items below are used NOTES: Other information to help the reader. CHANGES : REF NO DATE WHO DETAIL DDMMMYY Name First version XXNNNN DDMMMYY Name Name of item changed A/

/---- context ---------------------------------------------------/ #define's, typedef's, struct's, union's, enum's for use in more than one item below. /---- data descriptions -------------------------------------------------/ Use typedef's over struct's and #define's Use enum's over #define's /---- extern data declarations ------------------------------------------/ extern type VariableName; / comment / /---- extern function prototypes ----------------------------------------/ extern FunctionName( type Parm1, type Parm2 ) <page break> /A NAME: next functional area (as previous section) #endif / filename_h ------ END OF FILE ------/ 6.2 Header file for global data /H FILENAME : filename.h DESIGN REF: XXXXNN Header comment same format as normal header file H/ #ifndef GLOBAL #define GLOBAL extern #endif #ifndef filename_h #define filename_h Layout file in similar manner to normal header file GLOBAL type VariableName; Prefix all declarations with 'GLOBAL' #endif / filename_h / #undef GLOBAL /------- END OF FILE ------/ Then, to define data in global data file: #define GLOBAL #include "filename.h"..and to declare external data in source code file: #include "filename.h" 6.3 Code file /H FILENAME : filename.c DESIGN REF: nnnnnn DESCRIPTION : Simple description of main function of file

PUBLIC FUNCTIONS : type FunctionName( Parameter1, Parameter2 ) NOTES : Other help for the reader, including references. Copyright (c) MegaByte Co. 1990, 1992. All rights reserved. CHANGES : REF NO DATE WHO DETAIL DDMMMYY Name First version F.NNNN DDMMMYY Name Name of function/item changed H/ static char ID_filename[] = "filename:copyright MegaByte Co. 1990, 1992" / INCLUDE FILES / /---- system and platform files -----------------------------------------/ #include <filename.h> All files not defined within the program /---- program files -----------------------------------------------------/ #include "megabyte.h" Company standard: includes TRUE, FALSE, etc. #include "program.h" 'global' header file for program #include "filename.h" interface files for called subsystems #include "filename.h" local files for current subsystem / EXTERNAL REFEFENCES NOTE: only use if not available in header file / /---- data declarations -------------------------------------------------/ extern /---- function prototypes -----------------------------------------------/ extern / PUBLIC DECLARATIONS Defined here, used elsewhere / /---- context -----------------------------------------------------------/ #define's, typedef's, struct's, union's, enum's for use in declarations and functions /---- data declarations -------------------------------------------------/ /---- function prototypes -----------------------------------------------/ declare in same order as as definitions, below / PRIVATE DECLARATIONS Defined here, used only here / /---- context -----------------------------------------------------------/ #define's, typedef's, struct's, union's, enum's for use in declarations and functions /---- data declarations -------------------------------------------------/

static /---- function prototypes -----------------------------------------------/ declare in same order as as definitions, below static /---- macros ------------------------------------------------------------/ <page break> / PUBLIC FUNCTION DEFINITIONS / /F NAME : type FunctionName( Parm1, Parm2) DESCRIPTION : Simple description of function's function INPUTS : PARAMETERS: type VariableName Description of usage GLOBALS : type VariableName Description of usage OUTPUTS : PARAMETERS: type VariableName Description of usage GLOBALS : type VariableName Description of usage RETURN : VALUE1 Description of value VALUE2 Description of value NOTES : Other assumptions, hints, for the reader CHANGES : REF NO DATE WHO DETAIL DDMMMYY Name First version XXNNNN DDMMMYY Name Description of change F/ type FunctionName( type Parm1, type Parm2 ) / DATA / One declaration per line. type Variable1; / comment on purpose of Variable1 / type Variable2; / comment on purpose of Variable2 / / CODE / / trailing comment only where.. / /..it is necessary. Default / /..start in col.40 / /====== single line comment, major point in code ===============/

/------ single line comment, minor point in code ---------------/ /------ block comment ------------------------------------------ Comment on what has just happened and/or what is about to happen. Start in column 1 to help programmable editor to auto-insert block. ----------------------------------------------------------------/ / PRIVATE FUNCTION DEFINITIONS / /f/ PRIVATE type FunctionName( type Variable1, / Description of variable / type Variable2 / Description of variable / ); / PURPOSE : Brief description of what function does RETURN : Complete description of values of return NOTES : Other hints, etc. for the reader f/ / DATA / type Variable1; / comment on purpose of Variable1 / type Variable2; / comment on purpose of Variable2 / / CODE / <page break> /f/ Next definition of private function, etc. Constructs: if ( expression ) 'if' if ( long expression which wraps onto the next line )

if ( expression ) Always use braces if ( expression ) Even on null statement....always use braces if ( expression ) else if ( expression ) else if else if else 'if..else' 'if..else if.else' Always use default 'else' while ( expression ) 'while' while ( long expression which wraps onto the next line ) while ( TRUE ) Infinite loop

for ( expression; expression; expression ) 'for' for ( expression; expression; expression ) for ( expression which wraps onto next line; expression which wraps onto next line; expression which wraps onto next line ) do / until xyz happens / 'do' while ( expression ) switch ( expression ) case VALUE1: break; 'switch' Always use 'break' case VALUE2 / FALLTHROUGH / case VALUE2: case VALUE3: break; default: Always handle default case gracefully break; / end switch ( expression ) / Closing comment on end of all long blocks 'expression' is optional

Variable = ( expression )? expression : expression ; Minimize use of conditional expression FunctionCall( Parm1, Parm2, Parm3 ); Use when many parameters or multiple line wrap struct TAG_NAME 'struct's, 'union's and 'enum's use similar layout type VariableName1; type VariableName2; ; char pvariablename1; struct TAG_NAME VariableName2; Separate '' in pointer declarations Use comb layout to preserve vertical alignment