M-1 University of Washington Computer Programming I Program Style © 2000 UW CSE.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Outline Java program structure Basic program elements
CS 201 Functions Debzani Deb.
Modules, Hierarchy Charts, and Documentation
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Programming Style and Documentation Objective(s) F To become familiar with Java Style and Documentation Guidelines.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
ITP © Ron Poet Lecture 3 1 Comments. ITP © Ron Poet Lecture 3 2 Legibility  It is important that programs are easy to read.  It is easier to find bugs.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Scanf Reads in information from the keyboard Examples –scanf(“%d”, &number); –scanf(“%d%d”, &value1, &value2); WARNINGS! –Don’t forget the & (address of)
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Chapter 2 Introduction to C++ Department of Computer Science Missouri State University.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Documentation and Style. Documentation and Comments  Programs should be self-documenting.  Use meaningful variable names.  Use indentation and white.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CISC105 – General Computer Science Class 2 – 6/7/2006.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
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.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Working with Java.
Input and Output: I/O Finish reading chapters 1 and 2 of the text
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Lecture 3: Operators, Expressions and Type Conversion
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
CSE / ENGR 142 Programming I
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Basic Elements of C++ Chapter 2.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
2.1 Parts of a C++ Program.
Documentation and Style
Coding practices For IT and Computing students 2014.
Unit 3: Variables in Java
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

M-1 University of Washington Computer Programming I Program Style © 2000 UW CSE

M-2 Aspects of Quality Software Getting the syntax right This may seem hard at first, but turns out to be the easiest part of all Getting the logic right Sometimes difficult, but absolutely essential Today’s focus: Programming with good style What does this mean, and why does it matter?

M-3 Programming Style A program is a document: Some of it is read by a computer. ALL of it is read by people. Donald Knuth: “literate programming” “Style” is a catch-all term for people-oriented programming. comments, spacing, indentation, names clear, straightforward, well-organized code code quality

M-4 Style in This Course Along the way, we suggest and sometimes require particular points of style in programs that are turned in for this course. It is common for employers to have style requirements that all programmers must follow.

M-5 Style in This Course Along the way, we suggest and sometimes require particular points of style in programs that are turned in for the on campus version of this course. It is common for employers to have style requirements that all programmers must follow.

M-6 /* Comments */ /***************************************************** * Program:Mi_To_Km * Purpose:Miles to Km conversion * Author:A. Hacker, 1/18/00 Sec. AF (Turing) *****************************************************/ /* Calculate volume of cylinder and... * Inputs: radius, height,... * Output: volume,... * Assumes: radius, height nonnegative */. /* Tell user it’s negative. */ Comment block at front of program Comment block per major section Small ones throughout

M-7 Required Comments (1) 1. Heading comment at the beginning of each file Brief explanation of what’s in the file 2. Function heading comments Describe what the function does Must explain (define) all parameters and result Should never have to read function body to understand how to call it

M-8 Required Comments (2) 3. Variable declaration comments Describe information contained in the variable Not needed for trivial variables if their usage is obvious (loop indices,etc.) Should never have to read code that uses a variable to figure out what’s in it 4. Statement comments Higher-level summary of what the following group of statements does (as needed) Say what, not how Most individual statements won’t need comments

M-9 Statement Comments Say why, don’t paraphrase the code: NO:/* subtract one from sheep */ sheep = sheep - 1; YES:/* account for the sheep that the big bad wolf just ate.*/ sheep = sheep - 1;

M-10 Spaces Use blank lines to separate major sections. Vertically align like things: x= 5 ; y_prime= 7 ; z_axis= 4.3; Leave space around operators: No:y=slope*x+intercept; Yes:y = slope * x + intercept ; Use parentheses for emphasis, too Yes:y = (slope * x) + intercept ; Indentation Like an outline, indent subordinate parts.

M-11 Identifiers (Review) Identifiers name variables and other things Letters, digits, and underscores ( _ ) Can't begin with a digit Not a reserved word like double, return “Case-sensitive” VAR, Var, var, vAr are all different Using all CAPITAL letters is legal... but usually reserved for #define constants (soon to be explained)

M-12 What’s in a Name? Extremely valuable documentation. Microsoft Excel has over 65,000 variables. How long is just right? m mph miles_per_hour average_miles_per_hour_that_the_red_car_went Avoid similar names: mph vs. Mph vs. mqh

M-13 Suggestions for Names Variables and value-returning functions Noun phrase describing information in variable or value returned by function Void functions Verb phrase describing action performed when function is called

M-14 More Examples OK rectangleWidth, rectangle_Width, rectangle_width, length_10_Rectangle Illegal 10TimesLength, My Variable, int Legal, but bad style a1, I, O, xggh0sxx89s, rectangleWidth and rectanglewidth or rectangle_width

M-15 Clarity Do “obvious” things the obvious way No:x = (y = x) + 1 ; Yes:y = x ; x = x + 1; Don’t be tricky, cute, or clever without GOOD reason. If so, comment it!

M-16 (review) Named constants: #define PI #define HEIGHT 50 #define WIDTH 80 #define AREA (HEIGHT * WIDTH)... circle_area = PI * radius * radius ; volume = length * AREA; #define - Symbolic Constants Note: = and ; are not used for #define () can be used in #define

M-17 Centralize changes No "magic numbers" (unexplained constants) use good names instead Avoid typing errors Avoid accidental assignments to constants double pi ; pi = 3.14 ;vs.#define PI pi = 17.2 ;PI = 17.2 ; syntax error Why #define? (review)

M-18 /* Convert miles per hour to feet per second * Author:... * Date:... */ #include /* conversion constants. */ #define FEET_PER_MILE #define SECONDS_PER_HOUR(60.0 * 60.0) int main(void) { double miles_per_hour; /* input mph */ double feet_per_second; /* corresponding feet/sec */ double feet_per_hour;/* corresponding feet/hr */ /* prompt user for input */ printf(“Enter a number of miles per hour: ”); scanf(“%lf”, &miles_per_hour); /* convert from miles per hour to feet per second */ feet_per_hour = miles_per_hour * FEET_PER_MILE; feet_per_second = feet_per_hour / SECONDS_PER_HOUR; /* format and print results */ printf(“%f miles per hour is equal to %f feet per ” “second.\n”, miles_per_hour, feet_per_second); return 0; } Putting It All Together

M-19 Many small points; Big cumulative effect... #include int main(void){double v1,v2,v3,v4,v5; printf(“Enter a number of miles per hour:”); scanf(“%lf”,&v1);v5=v1* ; printf(“%f miles per hour is equal to “,v1); printf((“feet per second.\n”,v1,v5);return(0);}

M-20 Style Summary: Clarity is Job #1 DO Use plenty of comments - but not too many Use white space Use indentation Choose descriptive names Use named constants DON’T be terse, tricky place speed above correctness, simplicity use “magic numbers”