DOC PREVIEW
Princeton COS 217 - Program and Programming Style

This preview shows page 1-2-3-4-26-27-28-54-55-56-57 out of 57 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Program and Programming StyleSlide 2Goals of this LectureOverviewPart 1: Program StyleProgram StyleSelf-Documenting Code!Slide 8Structure: SpacingStructure: Indentation (cont.)Slide 11Structure: “Paragraphs”Slide 13Structure: ExpressionsStructure: Expressions (cont.)Slide 16Slide 17C IdiomsNamingCommentsComments (cont.)Slide 22Function CommentsFunction Comments (cont.)Slide 25ModularityPart 2: Programming StyleBottom-Up DesignBottom-Up Design in PgmmingTop-Down DesignTop-Down Design in PgmmingTop-Down Design in RealityExample: Text FormattingExample Input and OutputThinking About the ProblemWriting the ProgramThe Top LevelReading a WordReading a Word (cont.)Slide 40Saving a WordSaving a Word (cont.)Printing the Last LineDeciding When to PrintPrinting with JustificationPrinting with Justification (cont.)Slide 47Clearing the LineModularity: Summary of ExampleSummary: Program StyleSummary: Programming StyleAppendix: The “justify” ProgramSlide 53Slide 54Slide 55Slide 56Slide 571Program andProgramming StyleThe material for this lecture is drawn, in part, fromThe Practice of Programming (Kernighan & Pike) Chapter 12Program and Programming Style“How do we convince people that in programming simplicity and clarity — in short: what mathematicians call ‘elegance’ — are not a dispensable luxury, but a crucial matter that decides between success and failure?” — Edsger Dijkstra3Goals of this Lecture•Help you learn about:•Good program (noun) style•Good programming (verb) style•Why?•A large program that is well styled is easier to maintain (and more likely to be correct) than a large program that is poorly styled•A power programmer knows the qualities of a well styled program, and how to go about developing one4Overview•Program style: What are the qualities of a good program?•Well structured•Uses common idioms•Uses descriptive names•Contains proper comments•Modular•Programming style: How does one go about developing a good program?•Top-down design•Successive refinement•Example: left and right justifying text5Part 1: Program Style6Program Style•Who reads your code?•Compiler•Other programmers•Which of them cares about style?typedef struct{double x,y,z}vec;vec U,black,amb={.02,.02,.02};struct sphere{ vec cen,color;double rad,kd,ks,kt,kl,ir}*s,*best,sph[]={0.,6.,.5,1.,1.,1.,.9, .05,.2,.85,0.,1.7,-1.,8.,-.5,1.,.5,.2,1.,.7,.3,0.,.05,1.2,1.,8.,-.5,.1,.8,.8, 1.,.3,.7,0.,0.,1.2,3.,-6.,15.,1.,.8,1.,7.,0.,0.,0.,.6,1.5,-3.,-3.,12.,.8,1., 1.,5.,0.,0.,0.,.5,1.5,};yx;double u,b,tmin,sqrt(),tan();double vdot(A,B)vec A ,B;{return A.x*B.x+A.y*B.y+A.z*B.z;}vec vcomb(a,A,B)double a;vec A,B;{B.x+=a* A.x;B.y+=a*A.y;B.z+=a*A.z;return B;}vec vunit(A)vec A;{return vcomb(1./sqrt( vdot(A,A)),A,black);}struct sphere*intersect(P,D)vec P,D;{best=0;tmin=1e30;s= sph+5;while(s--sph)b=vdot(D,U=vcomb(-1.,P,s-cen)),u=b*b-vdot(U,U)+s-rad*s -rad,u=u0?sqrt(u):1e31,u=b-u1e-7?b-u:b+u,tmin=u=1e-7&&u<tmin?best=s,u: tmin;return best;}vec trace(level,P,D)vec P,D;{double d,eta,e;vec N,color; struct sphere*s,*l;if(!level--)return black;if(s=intersect(P,D));else return amb;color=amb;eta=s-ir;d= -vdot(D,N=vunit(vcomb(-1.,P=vcomb(tmin,D,P),s-cen )));if(d<0)N=vcomb(-1.,N,black),eta=1/eta,d= -d;l=sph+5;while(l--sph)if((e=l -kl*vdot(N,U=vunit(vcomb(-1.,P,l-cen))))0&&intersect(P,U)==l)color=vcomb(e ,l-color,color);U=s-color;color.x*=U.x;color.y*=U.y;color.z*=U.z;e=1-eta* eta*(1-d*d);return vcomb(s-kt,e0?trace(level,P,vcomb(eta,D,vcomb(eta*d-sqrt (e),N,black))):black,vcomb(s-ks,trace(level,P,vcomb(2*d,N,D)),vcomb(s-kd, color,vcomb(s-kl,U,black))));}main(){printf("%d %d\n",32,32);while(yx<32*32) U.x=yx%32-32/2,U.z=32/2-yx++/32,U.y=32/2/tan(25/114.5915590261),U=vcomb(255., trace(3,black,vunit(U)),black),printf("%.0f %.0f %.0f\n",U);}This is a working ray tracer! (courtesy of Paul Heckbert)7Self-Documenting Code!8Program Style•Why does program style matter?•Bugs often caused by programmer’s misunderstanding •What does this variable do?•How is this function called?•Good code = human readable code•How can code become easier for humans to read?•Convey program structure•Use common idioms•Choose descriptive names•Compose proper comments•Use modularity9Structure: Spacing•Use readable/consistent spacing•Example: Assign each array element a[j] to the value j.•Bad code•Good code•Can often rely on auto-indenting feature in editorfor (j=0;j<100;j++) a[j]=j;for (j=0; j<100; j++) a[j] = j;10Structure: Indentation (cont.)•Use readable/consistent indentation•Example: Checking for leap year (does Feb 29 exist?)if (month == FEB) { if (year % 4 == 0) if (day > 29) legal = FALSE; else if (day > 28) legal = FALSE;} if (month == FEB) { if (year % 4 == 0) { if (day > 29) legal = FALSE; } else { if (day > 28) legal = FALSE; }} Wrong code (else matches “if day > 29”)Right code11Structure: Indentation (cont.)•Use “else-if” for multi-way decision structures•Example: Comparison step in a binary search.•Bad code•Good codeif (x < v[mid]) high = mid – 1; else if (x > v[mid]) low = mid + 1; else return mid; if (x < v[mid]) high = mid – 1;else if (x > v[mid]) low = mid + 1; else return mid;245781017low=0high=6mid=310xv12Structure: “Paragraphs”•Use blank lines to divide the code into key parts#include <stdio.h>#include <stdlib.h>int main(void)/* Read a circle's radius from stdin, and compute and write its diameter and circumference to stdout. Return 0 if successful. */{ const double PI = 3.14159; int radius; int diam; double circum; printf("Enter the circle's radius:\n"); if (scanf("%d", &radius) != 1) { fprintf(stderr, "Error: Not a number\n"); exit(EXIT_FAILURE); /* or: return EXIT_FAILURE; */ }…13Structure: “Paragraphs”•Use blank lines to divide the code into key parts diam = 2 * radius; circum = PI * (double)diam; printf("A circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0;}14Structure: Expressions•Use natural form of expressions•Example: Check if integer n satisfies j < n < k•Bad code•Good code•Conditions should read as you’d say them aloud•Not “Conditions shouldn’t read as you’d never say them aloud”!if (!(n >= k) && !(n <= j))if ((j < n) && (n < k))15Structure: Expressions (cont.)•Parenthesize to resolve ambiguity•Example: Check


View Full Document

Princeton COS 217 - Program and Programming Style

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Program and Programming Style
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Program and Programming Style and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Program and Programming Style 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?