lecture 4: communication

28
LECTURE 4: COMMUNICATION CSC 313 – Advanced Programming Topics

Upload: jela

Post on 24-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

CSC 313 – Advanced Programming Topics. Lecture 4: Communication. Why Does This Matter?. } catch (IOException ioe) { if ( n == 0 ) { spec.harness.Context.out.println("Empty database"); return; } - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 4: Communication

LECTURE 4:COMMUNICATION

CSC 313 – Advanced Programming Topics

Page 2: Lecture 4: Communication

Why Does This Matter?

Page 3: Lecture 4: Communication

Spot the Bugpublic void read_db(String filename) { Entry entry; int i; int n = 0, act = 0, e, s; boolean OK; byte buffer[] = null; spec.harness.Context.out.print("Reading database "+dbname+" ... "); spec.harness.Context.out.flush(); try { spec.io.FileInputStream sif = new spec.io.FileInputStream(filename); n = sif.getContentLength(); buffer = new byte[n]; int bytes_read; while ( (bytes_read = sif.read(buffer, act , (n - act))) > 0){ act = act + bytes_read; } sif.close(); sif = null; // 03/11/98 rrh if ( act != n ){ spec.harness.Context.out.println("ERROR reading input file"); //System.exit(1); return; }

} catch (IOException ioe) { if ( n == 0 ) { spec.harness.Context.out.println("Empty database"); return; } spec.harness.Context.out.println("ERROR opening/reading input file \""+filename+"\"");// System.exit(1); }; entry = new Entry(); spec.harness.Context.out.print("OK\nBuilding database ..."); spec.harness.Context.out.flush(); n = buffer.length; s = e = 0; while ( (e < n) && (s < n) ) { // Check for CR also - rrh 2/18/98 while ( (e < n) && (buffer[e] != '\n') && (buffer[e] != '\r') ) e++; if ( e < n ) { if ( buffer[s] == '#' ) { add(entry); entry = new Entry(); } else entry.items.addElement(new String(buffer, 0, s, e-s)); // Discard CR & LF - rrh 2/18/98…

Page 4: Lecture 4: Communication

Brevity is the soul of witComplexity kills. It sucks the

life out of developers, it makes products difficult to

plan, build and test, it introduces security challenges

and it causes end-user and administrator frustration.

Ray Ozzie

Page 5: Lecture 4: Communication

Spot the Bug

public boolean isPrime(int n) {for (int i = 1; i < n; i++) { if ((n % i) == 0) { return false; }}return true;

}

Page 6: Lecture 4: Communication

Spot the Bug

public boolean isPrime(int n) {for (int i = 2; i < n; i++) { if ((n % i) == 0) { return false; }}return true;

}

Page 7: Lecture 4: Communication

Which Line Is Yours?

Page 8: Lecture 4: Communication

Shared Vocabulary Necessary Hints:

WiseCreativeHumorousAttractiveImpatientLazyFull of hubrisPerson assigning grades

Page 9: Lecture 4: Communication

Shared Vocabulary Necessary Hints:

ToolUselessLack of talentWaste of TimeAI HostDr. Hertz’s odd fascination

Page 10: Lecture 4: Communication

Shared Vocabulary Necessary

Page 11: Lecture 4: Communication

Shared Vocabulary Necessary

Page 12: Lecture 4: Communication

public interface Greeter {public void greet(String name);

}public class Formal implements Greeter {

public void greet(String name) { System.out.println(“Hello ” + name);}

}public class Informal implements Greeter {

public void greet(String name) { System.out.println(“Hiya ” + name);}

}public class French implements Greeter {

public void greet(String name) { System.out.println(“Bonjour ” + name);}

}

Page 13: Lecture 4: Communication

public interface Greeter {public void greet(String name);

}public abstract class Interpersonal {

protected Greeter g;}public class Worker extends Interpersonal {

private String person;public void greet() { g.greet(person); }

}public class DrHertz extends Interpersonal {

public void greet() { g.greet(“Bob”); }}public class Flanders extends Interpersonal {

private String person; public void greet() { g.greet(person + “doodly”);}

}

Page 14: Lecture 4: Communication

Strategy Pattern

Define interface for family of algorithms Method signature created for entire

family Coded up so each Strategy

interchangeable Each algorithm encapsulated in class of

its own Separates the Behavior from

Context Implies client uses 1 strategy at a time Also implies can change Strategy

dynamically

Page 15: Lecture 4: Communication

Shared Vocab. Not Sufficient

Page 16: Lecture 4: Communication

Brevity is the soul of witThe competent programmer is

fully aware of the strictly limited size of his own skull; therefore he approaches the

programming task in full humility, and among other

things he avoids clever tricks like the plague.

Edsger Dijkstra

Page 17: Lecture 4: Communication

Not a Free Gift

Design patterns are useful… Provide solutions at design level Enable shared vocabulary

… but are not panacea Do not prevent complex designs Programs continue having box

Page 18: Lecture 4: Communication

Not a Free Gift

Design patterns are useful… Provide solutions at design level Enable shared vocabulary

… but are not panacea Do not prevent complex designs Programs continue having box Are not “gift” in the box

Page 19: Lecture 4: Communication

Elevator Talk

VIP in elevator asks, “What are you working on?” You have elevator ride to explain it all Need to impress, but will lose them with BS

Boss comes by and asks, “How is it going?” Does not care/have time to get into details Have 90 seconds to get out important issues

Page 20: Lecture 4: Communication

This Matters

Need to market your ideas & designs Requires explaining to lazy, impatient

coders Well before your time is up, they are

bored If cannot explain design in elevator

talk Code created from design will have bugs Will require major effort to get to work

Page 21: Lecture 4: Communication

This Matters

Need to market your ideas & designs Requires explaining to lazy, impatient

coders Well before your time is up, they are bored

If cannot explain design in elevator talk Code created from design will have bugs Will require major effort to get to work Coders are lazy and HATE effort and work

Page 22: Lecture 4: Communication

This Matters

Need to market your ideas & designs Requires explaining to lazy, impatient

coders Well before your time is up, they are

bored If cannot explain design in elevator

talk Code created from design will have bugs Will require major effort to get to work Coders are lazy and HATE effort and

work

IT IS TOO COMPLEX

Page 23: Lecture 4: Communication

More Marketing

Explanations must be brief Coders should not see only necessary details Detail needed to code or not an important

details

Before software can be reusable it first has to be usable.

-- Ralph Johnson

An API that isn't comprehensible isn't usable.

-- James Gosling

Page 24: Lecture 4: Communication

Why Comments are Useful

If the code and the comments disagree,

then both are probably wrong.

Norm Schryer

Page 25: Lecture 4: Communication

Why Comments are Useful

As you're about to add a comment, ask yourself, 'How can I improve the

code so that this comment isn't needed?' Improve the code and

then document it to make it even clearer.

Steve McConnell

Page 26: Lecture 4: Communication

Your Turn

Give elevator speech on your current project

Page 27: Lecture 4: Communication

…and finally

For every complex problem there is an answer that is clear, simple, and

wrong.-- H L Mencken

Page 28: Lecture 4: Communication

For Next Lecture

Two (short) readings available on web Will start looking into how code is

optimized Begin by investigating how programs

actually run Also look at some of the simplest

optimizations Important to understand to improve

code written