DRY. 11: DRY—Don’t Repeat Yourself Every piece of knowledge should have a single, unambiguous, authoritative representation within a system Duplication.

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

Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
Documentation 1 Comprehending the present – Investing in the future.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Software Engineering and Design Principles Chapter 1.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
A Code Reuse Approach to Inheritance. Reusing Code – Lowest Level Copy/paste parts/all into your program –Maintenance problem Need to correct code in.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
CS 201 Functions Debzani Deb.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
From Module Breakdown to Interface Specifications Completing the architectural design of Map Schematizer.
Efficiently Sharing Common Data HTCondor Week 2015 Zach Miller Center for High Throughput Computing Department of Computer Sciences.
Computer Science 240 Principles of Software Design.
Test Driven Development Derived from Dr. Fawcett’s notes Phil Pratt-Szeliga Fall 2009.
Java Swing Joon Ho Cho. What is Java Swing? Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Abstract classes and Interfaces. Abstract classes.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Modular Programming. Modular Programming (1/6) Modular programming  Goes hand-in-hand with stepwise refinement and incremental development  Makes the.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Computer Science 240 © Ken Rodham 2006 Principles of Software Design.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Marie desJardins University of Maryland, Baltimore County.
1 Documenting with Javadoc. 2 Motivation  Why document programs? To make it easy to understand, e.g., for reuse and maintenance  What to document? Interface:
CSE 219 Computer Science III Program Design Principles.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CSE 143 Lecture 6 Linked Lists slides created by Ethan Apter
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
Intermediate 2 Software Development Process. Software You should already know that any computer system is made up of hardware and software. The term hardware.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
Software Engineering Design & UML.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
CIS 234: Project 1 Issues Dr. Ralph D. Westfall April, 2010.
Designing Classes. Software changes Software is not like a novel that is written once and then remains unchanged. Software is extended, corrected, maintained,
Working With Objects Tonga Institute of Higher Education.
Object-Oriented Programming Chapter Chapter
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Phil Tayco Slide version 1.0 Created Sep 18, 2017
The hashCode method.
Lecture 2 Introduction to Programming
Objects First with Java
Topic 28 classes and objects, part 2
Object-Oriented Programming Using C++ Second Edition
Topic 29 classes and objects, part 3
Stacks.
Building Java Programs
CS2013 Lecture 7 John Hurley Cal State LA.
Dr. R Z Khan Handout-3 Classes
Topic 29 classes and objects, part 3
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

DRY

11: DRY—Don’t Repeat Yourself Every piece of knowledge should have a single, unambiguous, authoritative representation within a system Duplication is evil! Why? –Things change –If you have the same thing in several places, when you change one, you have to change all the others –And you can’t always find them all!

What not to duplicate Data –If you represent the same data in two places (or in two ways), you have just taken on the job of keeping the two representations in agreement Code –If you code the same task in two places (or in two ways), you have just taken on the job of keeping the two code segments in agreement

How duplication arises Same information must be represented in many ways, or in several places Required by documentation standards Required by the language As a result of bad design Taking shortcuts Poor communication among team members

Multiple copies of data Sometimes the same information must be stored in two places (say, a client and a server) Sometimes you need the same information in two different programming languages Best solution: Have one authoritative repository, and generate all the needed copies from it –Remember, programs can create code

Example Suppose you must have a table of numbers in your program, and also in a document –Solution #1: Add code to your program to write out the table in a form you can use in the document –Solution #2: Same as #1, but hot link to the table from your document (if possible) –Solution #3: Write a program that takes the table from the document and produces code you can insert into your program –Solution #4: Keep the master table in a separate place, and generate both the program and the document

Documentation standards Comments should not repeat code –They should add higher-level information—what the code is doing, and why, but not how it does it If code requires explanation, it’s bad code—fix it! Your documentation should agree with your code –Untrustworthy comments are worse than none –This is much easier if your comments are high level –This is why javadoc is so wonderful—it’s always correct!

Language requirements Some languages, such as Ada, require you to duplicate information –Ada has: An interface part, where you provide all the function headers An implementation part, where you provide all the functions (with the same headers) –The advantage is that it forces you to hide the implementation from the users –The disadvantage is that you need the identical information in two places There’s not much you can do about this

Bad design A Truck has a capacity, a license number, and a driver A DeliveryRoute has a route, a Truck, and a driver One of our drivers calls in sick and we need to change drivers –Where do we change this information? –Solution: Refactor (redesign) the program!

Copy-paste programming Suppose, as you are coding, you find you need to do the same thing as you did somewhere else –So you take this shortcut: you copy and paste code Later you find you need to do almost the same thing again –So you copy the code, paste it in, and adjust it Later still, you find you need to change that code –Which copies are the same, and which almost the same? Never take a shortcut when you’re in a hurry!

How to avoid copy-paste Often the best solution is to write a method –Call the method whenever you want to perform the computation –There is nothing wrong with short methods If you want to do almost the same thing, parameterize (add parameters to) the method

Duplication for efficiency Suppose you have this class: class Point { double x, y;... } And you discover that some of your users want to have polar coordinates (ρ and θ) You can’t discard x and y, because the class is already in use So you end up with double x, y, rho, theta; Now what if a user changes, say, x ?

Meyer’s Uniform Access Principle Bertrand Meyer: “All services offered by a module should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation.” Translation into Java: 1.Make your variables private 2.Use getters and setters

Redoing the Point example, I Suppose you had started with: class Point { private double x, y; void setX(double x) { this.x = x; } void setY(double y) { this.y = y; } double getX() { return x; } double getY() { return y; } } Does this help solve the problem?

Redoing the Point example, II To add ρ and θ: –Do not add fields rho and theta ; instead, do double getRho() { return Math.sqrt(x*x + y*y); } double getTheta() { return Math.acos(x / rho); } void setRho(double rho) { x = rho * Math.cos(theta); y = rho * Math.sin(theta); } void setTheta(double theta) { x = rho * Math.cos(theta); y = rho * Math.sin(theta); } –Is there anything else that needs to be done? –Can you think of another way to do this?

Poor communication Different team members might implement the same functionality How do we avoid this? –Good communication! Exchange ideas with co-workers Read your co-workers code Let others read your code Have code walkthroughs –Have a clear design –Have a strong technical project leader –Have a clearly understood division of responsibilities

12: Make it easy to reuse Your co-workers won’t use your code unless: –They know about it –It’s easy to access –It’s easier to use your code than to write their own version of the same thing You won’t even reuse it yourself unless: –It’s easy to use –It’s general enough to support reuse

Summary DRY—Don’t repeat yourself! –This principle is so important that I’ve given almost an entire set of slides to it Make it easy to reuse

The End