Communicating in Code: Layout and Style Programming Studio Spring 2009 Note: several examples in this lecture taken from The Practice of Programming by.

Slides:



Advertisements
Similar presentations
Communicating in Code: Layout and Style Programming Studio Spring 2012 Note: several examples in this lecture taken from The Practice of Programming by.
Advertisements

Software Engineering Table-driven methods General control issues.
Introduction to C Programming
Coding Standards A Presentation by Jordan Belone.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
1 Software Craftsmanship Steve Chenoweth CSSE 375, Rose-Hulman Based on Don Bagert’s 2006 Lecture Right – Making sundials. From website
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 2: Your First Program.
15-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
26-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Chapter 2: Introduction to C++.
Introduction to C Programming
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Programming 101 EPGY Middle School Programming Adam Leeper.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Code Conventions Tonga Institute of Higher Education.
Flow of Control and Program Style n Nested if statements n C++ struct n Program Style n Lab Exercise.
// FileName.cpp // Written by Max Fomitchev (mif10) // This is a sample program // #include using namespace std; void main() { } Sample Program.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
1 CS161 Introduction to Computer Science Topic #8.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CPS120: Introduction to Computer Science Decision Making in Programs.
計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects.
ATS Programming Short Course I INTRODUCTORY CONCEPTS Tuesday, Feb 10th, 2009 Introduction to Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Communicating in Code: Commenting Programming Studio Spring 2009 Note: several examples in this lecture taken from The Practice of Programming by Kernighan.
The for Statement A most versatile loop
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
GC211Data Structure Lecture2 Sara Alhajjam.
Communicating in Code: Layout and Style
Communicating in Code: Layout and Style
Communicating in Code: Layout and Style
Phil Tayco Slide version 1.0 Created Oct 2, 2017
2.1 Parts of a C++ Program.
The Elements of Programming Style
Communicating in Code: Commenting
Computing Fundamentals
Topics Introduction to Functions Defining and Calling a Function
An Overview of C.
Controlling Program Flow
Presentation transcript:

Communicating in Code: Layout and Style Programming Studio Spring 2009 Note: several examples in this lecture taken from The Practice of Programming by Kernighan and Pike

Layout and Style Like naming, the goal is to communicate Again like naming, sometimes conventions are in place –Adhering to the convention in place will usually lead to more readable code than using your own “better” convention Goal of layout and style is to increase clarity.

Fundamental Theorem of Formatting Good visual layout shows the logical structure of the program. Studies show that organization is as important to understanding as the “details”

White Space Used to indicate logical grouping –Spacing between characters –Indentation –Blank lines

Indentation Can clarify structure, especially in odd cases. Studies show that 2-4 space indentation works best. –More indentation might “appear” better, but is not. Now, usually editors provide automatically. –But, variations for some statements: switch/case if/elseif Brace conventions differ, but be consistent.

Example Brace Conventions while (something) { blahblahblah } while (something) { blahblahblah } while (something) { blahblahblah }

Parentheses Parentheses can resolve ambiguity –Particularly important since order of operations can be problematic Better to use more parentheses than you think you need Coupled with white space, can more quickly highlight the grouping/ordering of operations leap_year = y % 4 == 0 && y % 100 != 0 || y % 400 == 0;

Parentheses Parentheses can resolve ambiguity –Particularly important since order of operations can be problematic Better to use more parentheses than you think you need Coupled with white space, can more quickly highlight the grouping/ordering of operations leap_year = y % 4 == 0 && y % 100 != 0 || y % 400 == 0; leap_year = ((y%4 == 0) && (y%100 != 0)) || (y%400 == 0);

Braces Like parentheses, use more braces than you need. One-statement operation often becomes more, later. if (a > b) max = a;

Braces Like parentheses, use more braces than you need. One-statement operation often becomes more, later. if (a > b) max = a; cout << “Set a new maximum.” << endl;

Braces Like parentheses, use more braces than you need. One-statement operation often becomes more, later. if (a > b) { max = a; }

Braces Like parentheses, use more braces than you need. One-statement operation often becomes more, later. if (a > b) { max = a; cout << “Set a new maximum.” << endl; }

Avoiding Complex Expressions Goal is not to write most concise and clever code. Break up expressions to make them clearer The “?” operator can be especially problematic *x += (*xp=(2*k < (n-m) ? c[k+1] : d[k--]));

Avoiding Complex Expressions Goal is not to write most concise and clever code. Break up expressions to make them clearer The “?” operator can be especially problematic *x += (*xp=(2*k < (n-m) ? c[k+1] : d[k--])); if (2*k < n-m) *xp = c[k+1]; else *xp = d[k--]; *x += *xp;

Use “Natural Form” for Expressions State conditional tests positively if (!(z>=0) && !(z<a))

Use “Natural Form” for Expressions State conditional tests positively if (!(z>=0) && !(z<a)) if ((z =a)) This can vary if the way it’s expressed better matches the underlying algorithm

Use “idomatic” forms There are “common” ways of expressing certain things. –e.g. Use a for loop appropriately – try to keep all loop control in the for statement, and keep other operations outside of the for statement for (i=0;i<n;i++) a[i] = 0.0;

Use “idomatic” forms There are “common” ways of expressing certain things. –e.g. Use a for loop appropriately – try to keep all loop control in the for statement, and keep other operations outside of the for statement for (i=0;i<n;i++) a[i] = 0.0; for (i=0;i<n;a[i++]=0.0);

Use “idomatic” forms There are “common” ways of expressing certain things. –e.g. Use a for loop appropriately – try to keep all loop control in the for statement, and keep other operations outside of the for statement for (i=0;i<n;i++) a[i] = 0.0; for (i=0;i<n;a[i++]=0.0); for (i=0;i<n;) { a[i] = 0.0; i++ }

Idiomatic forms e.g. use if elseif else form if (cond1) { dothis1(); } else { if (cond2) { dothis2(); } else { if (cond3) { dothis3(); } else { dothis4(); }

Idiomatic forms Use if elseif else form if (cond1) { dothis1(); } else if (cond2) { dothis2(); } else if (cond3) { dothis3(); } else { dothis4(); }

If statements Read so that you look for the “true” case rather than a “stack” of else cases if (a > 3) { if (b < 12) { while (!EOF(f)) { dothis(); } } else { cerr << “Error 2” << endl; } } else { cerr << “Error 1” << endl; }

If statements Read so that you look for the “true” case rather than a “stack” of else cases if (a <= 3) { cerr << “Error 1” << endl; } else if (b >= 12) { cerr << “Error 2” << endl; } else { while (!EOF(f)) { dothis(); }

Avoid Magic Numbers Rule of thumb: any number other than 0 or 1 is probably a “magic number” Can lead to tremendous debugging problems when these numbers are changed Instead, define constants to give names to those numbers.

Layout for Control Structures Put control in one line when possible Single indentation level for what it affects xxxxxx xxxxx Group each part of a complicated condition on its own line

Layout of Individual Statements White space can improve readability –Spaces after commas EvaluateEmployee(Name.First,EmployeeID,Date.Start,Date.End); –Spaces between parts of conditions if (((a d))&&((a+b) 2)) if (((a d)) && ((a+b) < (c-d)) && ((c-d) > 2))

Layout of Individual Statements Line up related definitions or assignments StudentName = ProcessInputName(); StudentID = ProcessInputID(); StudentHometown = ProcessInputName(); Don’t use more than one statement per line. –Likewise, define only one variable per line. Avoid side-effects (such as including the ++ operator when doing something else).

When a Line is Too Long Make it clear that the previous line is not ending (e.g. end with an operator) Keep related parts of the line together (don’t break single thought across line) Use indentation to highlight that there’s a continuation Make it easy to find the end of the continued line.

Layout of Routines Use standard indentation approach for arguments. Use blank lines to separate parts of routines or blocks of common actions Use comments (will return to) to identify major breaks in conceptual flow

Layout of Files Clearly separate (multiple line breaks) different routines in the same file –Don’t want to accidentally “merge” or “break” individual routines –Sequence files in a logical manner In order of header file definition In alphabetical order Constructor, accessor, destructor, other