Coding Convention for ECON 41701

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

EE 382 Style Guide. March 2, 2018

Enhance Your Productivity and Software Quality with Techniques from Silicon Valley

C Formatting Guidelines

CSCI 171 Chapter Outlines

C++ Programming Style Guide

C Language, Token, Keywords, Constant, variable

Syntax and Variables

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

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

A Fast Review of C Essentials Part I

EL6483: Brief Overview of C Programming Language

INTRODUCTION 1 AND REVIEW

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

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

C++ Tutorial AM 225. Dan Fortunato

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

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++

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

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

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

C: How to Program. Week /Mar/05

Coding conventions and C++-style

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

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

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

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

Introduction to Programming using C++

Creating a C++ Program

Java Programming Style Guide

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CMAT Language - Language Reference Manual COMS 4115

Chapter 2 Basic Elements of C++

CSCI 2101 Java Style Guide

QUIZ. What are 3 differences between C and C++ const variables?

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

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

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

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.


BLM2031 Structured Programming. Zeyneb KURT

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

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

Chapter 2: Introduction to C++

Variables. Data Types.

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

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

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

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

Programming Style Guide v1.1

UEE1302 (1102) F10: Introduction to Computers and Programming

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

Coding Standards for C

Full file at

Starting to Program in C++ (Basics & I/O)

Chapter 2 - Introduction to C Programming

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

Typescript on LLVM Language Reference Manual

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

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

Introduction to C# Applications

Chapter 2 Author Notes

A brief introduction to C programming for Java programmers

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Tokens, Expressions and Control Structures

What does this program print?

Objectives. In this chapter, you will:

Pace University. Fundamental Concepts of CS121 1

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

Coding Standards for C

CPSC 427: Object-Oriented Programming

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CSE 142/143 Unofficial Style Guide

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

4. The is a diagram that graphically depicts the steps that take place in a program. a. Program b. Flowchart c. Algorithm d. Code e.

Basic Types, Variables, Literals, Constants

6.096 Introduction to C++ January (IAP) 2009

Introduction to C Programming

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

We do not teach programming

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

PPP Style Guide Stroustrup 1/29/2010. PPP Style Guide. Bjarne Stroustrup

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Topic 6: A Quick Intro To C

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Review of the C Programming Language for Principles of Operating Systems

LECTURE 02 INTRODUCTION TO C++

C and C++ 2. Functions Preprocessor. Alan Mycroft

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

CS11 Java. Fall Lecture 1

CS102: Variables and Expressions

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Transcription:

Coding Convention for ECON 41701 Benjamin S Skrainka The Harris School of Public Policy University of Chicago skrainka@uchicagoedu 1 Introduction Writing software with a cogent programming style produces code which is easier to maintain, extend, debug, and understand You actually save time by taking the time to follow a sensible coding convention You should attempt to follow this style guide when working on your homework Where something is unspecified, exercise your best professional judgement For further discussion of programming style, see Kernighan & Pike You may also find the Google coding convention helpful (http://google-styleguidegooglecode com/svn/trunk/cppguidexml) 2 Names Construct names using CamelCase (eg, somevariablename) and not with underscores separating words in names (eg, some_variable_name) With Camel- Case, you should capitalize the first letter of every word except the first Choose names which describe what the name refers to 21 Variable Names If the name refers to a variable, you should choose the first letter or two as: n the variable name is an integer count of something, like nhh for the number of households v the variable is a vector, eg, vmarketshares m the variable is a matrix, eg, mproductchar f the variable is a file or file stream object, eg, finput 1

2Names 2 dw the variable contains double precision floating point data, eg, dwprice Now, I am somewhat sloppy and use this for any floating point quantity, but if you want to be more precise, then use dw for double word (8-bytes, eg a double in C++), w for single word (4-bytes, eg a float in C++), ldw for long double (16-bytes, eg long double in C++) sz for a string either char or a std::string Historically, this meant a null terminated string The larger the scope of the variable, the better the name needs to be For example, in a short function or loop, p may represent price without confusion Over larger scales, it is a horrible name Is it price, a pointer, or??? Err on the side of being overly clear Remember, you may have to put your code down for aquarterbecauseofotherobligations,suchasteaching,andyouwanttomake it as easy as possible to resume work on your code at a later date Define or declare only one variable per line Follow the declaration with a short comment explaining the variable Eg, double dwobjfuncopt ; // Value of objective function at optimum 211 Loop Indexes Many people use i, j, k, l, etc for loop indexes These names are hard to search on Consequently, I like ix, jx, and kx which make it clear that they are an index and stand out in your code 212 Pointers Variables which are pointers should begin with p and have one p for each level of indirection Eg, char pname ; char ppname ; BLP_CONFIG pblpconfig ; Using this convention will help you avoid errors when dereferencing pointers 213 Constants Traditionally, constants are given names which are all in capital letters This is particularly true in older variants of C, where const doesn t exist and #define is used to create constants: #define N_HH_TYPES (10) However, it is better to use const because the compiler can perform type checking:

2Names 3 const int N_HH_TYPES = 10 ; Make sure constants whether numeric or strings such as filenames are defined in one place only For languages which lack constants, such as MATLAB and Stata, the best you can do is store these values in a variable 214 User Defined Types When defining your own types, simple types should have a lowercase name which ends in _t whereas classes should be in upper CamelCase with a capital first letter and a terminal _T, eg: typedef unsigned long size_t ; class BLPSnoptProblem_T : public snoptproblem ; These suffixes make it clear that the types are user-defined or part of a system library in the case of size_t 22 Library Names, Function Names and Arguments Libraries (modules) should be named with a short word or two followed by Lib The short word or two should make the purpose of the library clear The names for all functions in that library should begin with the same word(s) which begin the library s name This makes it clear to which library a function belongs so that you can quickly find which file defines a function After the library name which starts the function, you should choose a verb which describes what a function does For functions which return TRUE/FALSE, the verb should be Is, eg, loadisvalid to check that the data which was loaded is valid For example, a library for computing quantities used in the BLP model of differentiated products is named blplib It includes functions blpcalcshares, blpcalccondshares, and blpcalcmeanutiljac blplib has both a header file blplibhpp and an implementation file blplibcpp Afunction sargumentsshouldfirstlisttheinputargumentsandthenthe output arguments List arguments from most general to more specific Each argument s name should end with an underscore to make it clear that it is an argument Try to write functions which do one thing and do it well Such functions are easier to debug and compose with other functions to perform more complex tasks If you function is longer than a screen or two, you probably need to break it up so that it is easier to understand In general, a function should return void or status information More complex results should be returned either as a reference or a pointer (Google prefers the latter)

3 Module Layout 4 3 Module Layout First, C++ supports a range of suffixes for C++ header and implementation files In this course, we will use hpp for header files and cpp for implementation files You may also wish to put your code in its own namespace Eg, my BLP code is in namespace blp so that it will not clash with any other libraries which ImightuseincaseIchoosethesamenameassomeotherlibrary 31 File Layout Every file must begin with a one-line comment on the first line of the file describing what the file does If you cannot summarize what you are doing succinctly, you are not ready to start writing code Follow the one line comment with a larger comment describing the purpose of the file, special considerations, etc Cite papers where relevant You may find it helpful to include a modification history which explains why you modified the file and the date Explain why you did something not what you did This information can also be stored in the version control system, so a modification history is less important now, but may save you time, especially if you distribute your code Here is an example from blplibcpp: / blplibcpp - implementation of functions needed to estimate the BLP model with MPEC / blplibcpp implements the functions used to estimate the BLP model of differentiated products (Berry, Levinsohn, & Pakes, 1995) using MPEC (Su & Judd, 2012) Because we use MPEC, most of the action is in the constraints In particular, blplibcpp computes values for the objective function, constraints, gradient of the objective function, Jacobian of the constraints, and Hessian needed to compute standard errors Compile with -DBLP_DEBUG to enable diagnostic error messages about share values which are zero, Inf, or NaN Primarily, these messages apply to the calculation of the constraints and their Jacobian modification history -------------------- 27mar2011 bss re-enabled #if BLP_DEBUG handling of Infs to improve performance of Release build 11nov2010 bss support for portable, BLP floating point type 09nov2010 bss better handling of NaNs 30oct2010 bss implementation of many blp_ functions 29oct2010 bss written

3 Module Layout 5 / Note how the the modification history (or mod hist ) consists of the date, the initials of who made the change, and a description These descriptions could be better See how the simple comment about compiling with -DBLP_DEBUG could save you time when building the file 32 Header Files Header files should start with the same one line comment at the cpp file and be composed of logically separated sections for includes, declarations, etc In addition, you should use include guards to prevent including your header file excessively or in an infinite loop (will be discussed later in the course) Lay out the module as: / mylibhpp - trenchant one line comment / / More lengthy comments go here! modification history -------------------- 25mar2012 you written / #ifndef INCyourLib #define INCyourLib //-------------------------------------------------------------------- // Includes #include somelibyouneedhpp #include anotherlibyouneedhpp //-------------------------------------------------------------------- // Constants // define any constants you need //-------------------------------------------------------------------- // Types // declare any special types or classes you need here //-------------------------------------------------------------------- // Declarations

4 White Space, Comments, and Braces 6 // function declarations go here #endif // #ifdef INCyourLib 4 White Space, Comments, and Braces Use white space, comments, and braces to indicate which chunks of code go together 41 White Space 411 Horizontal White Space Put white space inside of parenthesis and around operators 412 Indentation Use two spaces when indenting The text recommends three which is insane Also, setup your editor to convert tabs to whitespace because tabs are evil To make this easier, I set the following in my ~/vimrc: :syntax on :set ts=2 :set ai :set nu :set et If you are using MacVim, you can put :set guifont=menlo\ Regular:h16 in your vimrc to set the default font size 413 Vertical White Space Use one or two newlines (vertical white spaces) to show that some lines are logically related This is like breaking a document up into sentences, paragraphs, subsections, sections, and chapters 414 Comments Comment anything which is not obvious When you are not sure of the correct implementation or it may need to change, mark it with a comment // XXX This makes it easy to search for

4 White Space, Comments, and Braces 7 You may find it easier to write the comments first to work out the general layout of your code before writing the code itself! Use comments with 20, 40, or 80 repetitions of =, -,, etc to show breaks or logical groupings in code 42 Braces Line up braces vertically without indentation Eg, int myfunc( int narg_, double dwarg_ ) int nretval ; // Return value if( dwarg_ > 0 ) nretval = 1 ; else nretval = 0 ; for( int ix = 0 ; ix < narg_ ; ++ix ) nretval += ix ; return nretval ; With this convention, it is easy to see that you have not forgotten a brace The alternative is 1TBS (the One True Brace Style) and is to be avoided: int myfunc( int narg_, double dwarg_ ) int nretval if( dwarg_ > 0 ) nretval = 1 ; else nretval = 0 ; for( int ix = 0 ; ix < narg_ ; ++ix ) nretval += ix ;

5Miscellaneous 8 return nretval ; 5 Miscellaneous Some other advice: Use const wherever possible It will help the compiler catch errors Use C++ casts, not C-style casts, eg static_cast<double> ( nhh ) instead of (double) nhh Compile with as much warning information as possible, eg g++ -Wall -pedantic When applying a prefix/postfix operator in a way without side effects, prefer the prefix version of the operator because it is faster on complex objects, like iterators Eg, ++ix is better than ix++ Use parenthesis around any mathematical expressions other than the +, -,, and / so that you do not have to worry about order of operations Avoid parameterized macros where possible If you must, then make sure you put parentheses around the arguments: #define max( a, b ) ( (a) > (b)? (a) : (b) ) Because it is easy to forget an equals sign when testing for equality, specify constants first so that the compiler will generate a warning if you make this mistake: if( 0 == nfound ) handleerror() ; instead of if( nfound == 0 ) handleerror() ; In MATLAB, use cell arrays to help set off sections and facilitate rerunning subsets of your code To create a cell array, just start the line with %% R seems to work better if you use 1TBS Python uses indentation to indicate blocks It is probably best to follow the official Python style guide

5Miscellaneous 9 With Stata, do the best you can and exercise good judgement Built March 25, 2012