spring 2020 software engineering software design and best ...rjust/courses/2020spring/cse403… ·...

48
CSE 403 Software Engineering Spring 2020 Software design and best practices April 22, 2020

Upload: others

Post on 20-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

CSE 403Software Engineering

Spring 2020

Software design and best practices

April 22, 2020

Page 2: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Logistics

In-class exercise 1: Friday 04/24● Set up your machine

○ Git, Java 8+ JDK, and Apache Ant

● Test your set-up○ https://bitbucket.org/rjust/basic-stats○ Clone, build, and test (testMedian is supposed to fail)

● (Optional) pair up by Thursday (group In-class-1 on Canvas).

Page 3: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Logistics

In-class exercise 1: Friday 04/24● Set up your machine

○ Git, Java 8+ JDK, and Apache Ant

● Test your set-up○ https://bitbucket.org/rjust/basic-stats○ Clone, build, and test (testMedian is supposed to fail)

● (Optional) pair up by Thursday (group In-class-1 on Canvas).

Progress report and meeting agenda● Every Wednesday by 10pm● Markdown file in a publicly or UW-NetID accessible git repo● https://homes.cs.washington.edu/~rjust/courses/2020Spring/CSE403/project.html

Page 4: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Recap: software architecture vs. design

Architecture and design● Components and interfaces: understand, communicate, reuse● Manage complexity: modularity and separation of concerns● Process: allow effort estimation and progress monitoring

View Controller

Model

Client uses

manipulatesupdates

seesPresentation layer

Business logic layer

Data access layer DB

Client

Page 5: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Today

● Software design● How to avoid bugs in your program● Best practices

Page 6: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

SW Design: Purposes, Concepts, and Misfits

Page 7: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Concept and motivating purpose

“A concept is something you needto understand in order to use anapplication (and also something adeveloper needs to understand towork effectively with its code) andis invented to solve a particularproblem, which is called the motivating purpose.”

Use case are a good starting pointfor defining concepts for motivating purposes.

Page 8: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Operational principle and misfit

“A concept is defined by an operationalprinciple, which is a scenario that illustrateshow the concept fulfills its motivating purpose.”

Page 9: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Operational principle and misfit

“A concept is defined by an operationalprinciple, which is a scenario that illustrateshow the concept fulfills its motivating purpose.”

“A concept may not be entirely fit for purpose.In that case, one or more operational misfitsare used to explain why. The operational misfitusually does not contradict the operationalprinciple, but presents a different scenario inwhich the prescribed behavior does not meet a desired goal.”

Page 10: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Properties of a good software design

MotivationEach concept should be motivated by at least one purpose.

CoherenceEach concept should be motivated by at most one purpose.

FulfillmentEach purpose should motivate at least one concept.

Non-divisionEach purpose should motivate at most one concept.

DecouplingConcepts should not interfere with one another’s fulfillment of purpose.

Page 11: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Properties of a good software design

MotivationEach concept should be motivated by at least one purpose.

CoherenceEach concept should be motivated by at most one purpose.

FulfillmentEach purpose should motivate at least one concept.

Non-divisionEach purpose should motivate at most one concept.

DecouplingConcepts should not interfere with one another’s fulfillment of purpose.

Page 12: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Properties of a good software design

MotivationEach concept should be motivated by at least one purpose.

CoherenceEach concept should be motivated by at most one purpose.

FulfillmentEach purpose should motivate at least one concept.

Non-divisionEach purpose should motivate at most one concept.

DecouplingConcepts should not interfere with one another’s fulfillment of purpose.

Page 13: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Properties of a good software design

MotivationEach concept should be motivated by at least one purpose.

CoherenceEach concept should be motivated by at most one purpose.

FulfillmentEach purpose should motivate at least one concept.

Non-divisionEach purpose should motivate at most one concept.

DecouplingConcepts should not interfere with one another’s fulfillment of purpose.

Page 14: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

How to avoid bugs in your code?

Page 15: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

How to avoid bugs in your code?

It’s super simple...don’t introduce them during coding!

Everybody makes mistakes, except for me…

But then, there is just one of me.

Page 16: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

How to avoid bugs in your code?

A more realistic approach: 3 steps

1. Make certain bugs impossible by design2. Correctness: get it right the first time3. Bug visibility

Page 17: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

How to avoid bugs in your code?

A more realistic approach: 3 steps

1. Make certain bugs impossible by designa. Programming language

i. Ever had a use-after free bug in a Java program?ii. Ever had an assignment bug (String to Integer) in a statically typed language?

b. Libraries and protocolsi. TCP vs. UDPii. No overflows in BigInteger

Page 18: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

How to avoid bugs in your code?

A more realistic approach: 3 steps

1. Make certain bugs impossible by designa. Programming language

Page 19: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

How to avoid bugs in your code?

A more realistic approach: 3 steps

1. Make certain bugs impossible by designa. Programming language

Pick the right tool for the job...

Page 20: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

How to avoid bugs in your code?

A more realistic approach: 3 steps

1. Make certain bugs impossible by designa. Programming language

Pick the right tool for the job...if possible!

Page 21: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

How to avoid bugs in your code?

A more realistic approach: 3 steps

1. Make certain bugs impossible by designa. Programming languageb. Libraries and protocols

2. Correctness: get it right the first timea. A program without a spec is bug freeb. Keep it simple, modular, and testablec. Defensive programming and conventions (discipline)

Page 22: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

How to avoid bugs in your code?

A more realistic approach: 3 steps

1. Make certain bugs impossible by designa. Programming languageb. Libraries and protocols

2. Correctness: get it right the first timea. A program without a spec is bug freeb. Keep it simple, modular, and testablec. Defensive programming and conventions (discipline)

3. Bug visibilitya. Assertions (pre/post conditions)b. (Regression) testingc. Fail fast

Page 23: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April
Page 24: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Quiz: setup and goals● Each project group is a team● 6 code snippets● 2 rounds

○ First round■ For each code snippet, decide whether it represents good or bad practice.■ Goal: discuss and reach consensus on good or bad practice.

○ Second round (known “solutions”)■ For each code snippet, try to understand why it is good or bad practice.■ Goal: come up with an explanation or a counter argument.

Page 25: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Round 1: good or bad?

Page 26: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 1: good or bad?

Page 27: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 2: good or bad?

Page 28: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 3: good or bad?

Page 29: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 4: good or bad?

Page 30: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 5: good or bad?

Page 31: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 6: good or bad?

Page 32: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Round 2: why is it good or bad?

Page 33: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

My take on this

● Snippet 1: bad

● Snippet 2: bad

● Snippet 3: good

● Snippet 4: bad

● Snippet 5: bad

● Snippet 6: good

Page 34: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 1: this is bad! why?

Page 35: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 1: this is bad! why?

Null references...the billion dollar mistake.

Page 36: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 1: this is bad! why?

… Don’t return null; return an empty array instead.

Page 37: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 1: this is bad! why?

No diagnostic information.

Page 38: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 2: short but bad! why?

Page 39: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 2: short but bad! why?

Defensive programming: add an assertion (or write the literal first).Use constants and enums to avoid literal duplication.

Page 40: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 3: this is good, but why?

Page 41: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 3: this is good, but why?

Type safety using an enum; throws an exception for unexpected cases (e.g., future extensions of PaymentType).

Page 42: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 4: also bad! huh?

Page 43: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 4: also bad! huh?

Method parameters should be final;use local variables to sanitize inputs.

Page 44: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 5: Java API, but still bad! why?

Page 45: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 5: Java API, but still bad! why?

What does the last call return?

Page 46: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 5: Java API, but still bad! why?

Avoid method overloading, which is statically resolved. Autoboxing/unboxing adds additional confusion.

Page 47: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 6: this is good, but why?

Page 48: Spring 2020 Software Engineering Software design and best ...rjust/courses/2020Spring/CSE403… · CSE 403 Software Engineering Spring 2020 Software design and best practices April

Snippet 6: this is good, but why?

Good encapsulation; immutable object.