CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1

Slides:



Advertisements
Similar presentations
Reviewing your Program CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4 © Mitchell Wand, This work is licensed under a Creative Commons.
Advertisements

Basics of Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Language Issues for Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.3 © Mitchell Wand, This work is licensed under a Creative.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Patterns of Communication Between Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.1 © Mitchell Wand, This work is licensed under.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.4 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Examining Two Pieces of Data CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Testing Mutable Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.5 © Mitchell Wand, This work is licensed under a Creative Commons.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Design Strategies 1: Combine Simpler Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed.
The Point of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.1 © Mitchell Wand, This work is licensed under a Creative Commons.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Testing Mutable Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Organization of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.2 © Mitchell Wand, This work is licensed under a Creative Commons.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
How to use an Observer Template
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4
How to Learn in This Course
Contracts, Purpose Statements, Examples and Tests
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.2
The Point of This Course
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.4
Introduction to Invariants
Reviewing your Program
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.7
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
Halting Functions for Tree-Like Structures
Generalizing this Design
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1
From Templates to Folds
Callbacks and Interacting Objects
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
Case Study: Undefined Variables
Patterns of Interaction 2: Publish-Subscribe
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Solving Your Problem by Generalization
A Case Study: Space Invaders
Testing Mutable Objects
Contracts, Purpose Statements, Examples and Tests
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
The Iterative Design Recipe
Introduction to Universe Programs
Examining Two Pieces of Data
When do I need an invariant?
Design Strategies 3: Divide into cases
Presentation transcript:

CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, 2012-2017 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

The Point 1. It’s not calculus. Getting the right answer is not enough. 2. The goal is to write beautiful programs. 3. A beautiful program is one that is readable, understandable, and modifiable by people.

Your programs should look like this: source

Not like this source

Your programs should look like this source

Not like this source

And never, ever like this source

Key Practices for writing beautiful programs 1. Write programs that people can read, understand, and modify. 2. Represent information as data; interpret data as information. 3. Use contracts and purpose statements to specify the intended behavior of your functions and methods. 4. Use invariants to limit your functions’ responsibility. 5. Use functions and methods that produce and consume values. 6. Use state only to share information between distant parts of the program. 7. Use interfaces to limit dependencies between different parts of your program.

1. Write programs that people can read, understand, and modify You write programs so others can read them Bosses, customers, maintainers, etc. This means an older version of you, too You work with others as you develop programs The earlier you articulate your thinking, the earlier you can catch flaws The earlier you catch flaws, the easier/cheaper they are to fix

2. Represent Information as Data; Interpret Data as Information representation interpretation Information Data If you don’t know what your data means, you can’t possibly manipulate it correctly!

3. Use contracts and purpose statements to specify the intended behavior of your functions and methods Writing out the contract and purpose statement before you code will help you plan. The person who calls your function should never have to read your implementation to figure out what your function returns. If you use good function names, the reader will have a good head start. If you don’t know what your function is supposed to do, how can you possibly write it? If the reader doesn’t know what your function is supposed to do, how can they possibly understand your code?

4. Use Invariants to Limit Your Function's Responsibility Your function may need to rely on information that is not under its control and not represented in its contract. Record this assumption as an invariant (WHERE clause). The function is only responsible for giving the right answer for inputs that satisfy the invariant. The caller is responsible for making sure that the invariant is satisfied.

5. Use functions and methods that produce and consume values Functional model makes it easy to create examples and test data Easier to understand Easier to test You can test your functions independently, without worrying about the order of the tests. Your function shouldn’t have side-effects unless that’s its purpose.

6. Use state only to share information between distant parts of the program. You need to use state in exactly two situations: you need an object with stable identity to send messages to or ask questions of (e.g. the players) you need to construct cyclic structures Sometimes you need state, but less often than you might think Java, C++, etc. lead you to use state more often than you should.

7. Use interfaces to limit dependencies between different parts of your program. Always manipulate your data through a set of functions. That way, if you change the representation of your data, you won’t have to change other parts of your program.

Other important things to remember...

The Function Design Recipe 1. Data Design 2. Contract and Purpose Statement 3. Examples and Tests 4. Design Strategy 5. Function Definition 6. Program Review Remember, this is a process, not a set of deliverables. Here is the function design recipe.

The Shape of the Program Follows the Shape of the Data is-component-of BinTree leaf data calls tree-fn leaf data function Data Hierarchy (a BinTree is either leaf data or has two components which are BinTrees Call Tree (tree-fn either calls a function on the leaf data, or it calls itself twice.)

Design one function/method per task Small is good. Period. Big is bad. Period. If you have complicated junk in your function, you must have put it there for a reason. Turn it into a separate function so you can test it. If you can’t think of a good name for your help function, then you are probably doing it wrong.

Don't Repeat Yourself Introduce a generalization whenever you start to duplicate code. Any time you copy & paste, look for a pattern. One is an exception; two is a coincidence; three is a pattern. But don't generalize until you know what the pattern is. It's OK to copy & paste for a while until you see the pattern. But be sure to replace them all with good generalizations. Your testers and maintainers will thank you.

Don't Reinvent the Wheel Use other people’s code, libraries, etc. whenever possible (and legal). You aren’t (or shouldn’t be) paid by the line!

Summary: You need never be afraid of this: You need never be afraid of a blank page.

You know the questions to ask What is the relevant information from the world? How should it be represented as data? What is the purpose of this system/function/method? How should I go from purpose to code?

And you know how to write down the answers Data Definitions and Interpretations Contracts and Purpose Statements Examples and Tests Design Strategies Code

And good luck! Stay in touch. --Prof. Wand Go get ‘em!! And good luck! Stay in touch. --Prof. Wand