Software Engineering Routine design. High quality routines Routine: individual method or procedure invocable for a single purpose.

Slides:



Advertisements
Similar presentations
Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation For C# Developers.
Advertisements

Chapter 1: Computer Systems
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
By Justin Hendrix. Design Challenges Design is the activity that links requirements to coding and debugging. Good design is useful on small project and.
1 Java Programming Basics SE-1011 Dr. Mark L. Hornick.
Software Engineering Variables. The data literacy test Count 1.0 if you know what the concept means. Count 0.5 if you believe you know what the concept.
Software Engineering Class design. Class design – ADT Abstract Data Types:  Why use ADT? Hidden implementation details Changes do not affect whole program.
Inheritance Inheritance Reserved word protected Reserved word super
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
CS 536 Spring Run-time organization Lecture 19.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
CSE1301 Computer Programming: Lecture 13 Documentation.
Fall 2007CS 2251 Software Engineering Intro. Fall 2007CS 2252 Topics Software challenge Life-cycle models Design Issues Documentation Abstraction.
CS 201 Functions Debzani Deb.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
General Issues in Using Variables
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Routines and Defensive Programming Chapter 7-8 McConnell.
Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
SOFTWARE DESIGN (SWD) Instructor: Dr. Hany H. Ammar
Chapter 7: High Quality Routines By Raj Ramsaroop.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Software Implementation (Writing Quality Code). void HandleStuff( CORP_DATA & inputRec, int crntQtr, EMP_DATA empRec, float & estimRevenue, float ytdRevenue,
1 Software Design Overview Reference: Software Engineering, by Ian Sommerville, Ch. 12 & 13.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CS242.  Reduce Complexity  Introduce an intermediate, understandable abstraction  Avoid code duplication  Support subclassing  Hide sequences  Hide.
Documentation Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.
Lecture 13 Law of Demeter. Cohesion Cohesion: the “glue” that holds a module together. Don’t do things that do not support a common goal Cohesion: the.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Jump to first page (C) 1998, Arun Lakhotia 1 Design Quality Metrics Arun Lakhotia University of Southwestern Louisiana Po Box Lafayette, LA 70504,
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
High-Quality Routines Chapter 5-6. Review Class Quality Checklist 2.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Key Principles of Software Architecture and Design (II) adapted from Dave Penny’s.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Using const in C++ Classes In the presence of pointers we must take steps to ensure the integrity of the object Can use const method definitions The.
CHAPTER 8 Scope, Lifetime, and More on Functions.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Methods CSC 171 FALL 2001 LECTURE 3. History The abacus.
Coupling and Cohesion Rajni Bhalla.
High-Quality Routines
Introduction to the C Language
Chapter 4 Procedural Methods.
High-Quality Routines
Introduction to the C Language
Improving the Design “Can the design be better?”
CS 190 Lecture Notes: Wrapup
Chapter 1: Computer Systems
Chapter 09 – Part I Creating High-Quality Code
Chapter 7 Procedural Methods.
CS 190 Lecture Notes: Wrapup
CS 240 – Advanced Programming Concepts
Presentation transcript:

Software Engineering Routine design

High quality routines Routine: individual method or procedure invocable for a single purpose

High quality routines Low quality routine: (Code Complete, 2 nd ed., chapter 7 – page 162) void HandleStuff( CORP_DATA & inputRec, int crntQtr, EPM_DATA empRec, double & estimRevenue, double ytdRevenue, int screenX, int screenY, COLOR_TYPE & newColor, COLOR_TYPE & prevColor, StatusType & status, int expenseType ) { int i; for ( i = 0; i < 100; i++ ) { inputRec.revenue[i] = 0; inputRec.expense[i] = corpExpense[ crntQtr ][ i ]; } updateCorpDatabase( empRec ); estimRevenue = ytdRevenue * 4.0 / (double) crntQtr; newColor = prevColor; status = SUCCESS; if ( expenseType == 1 ) { for ( i = 0; i < 12; i++ ) profit[i] = revenue[i] – expense.type1[i]; } else if ( expenseType == 2 ) { profit[i] = revenue[i] – expense.type2[i]; } else if ( expenseType == 3 ) profit[i] = revenue[i] – expense.type3[i]; }

High quality routines Low quality routine:  Bad name: HandleStuff() does not inform what the routine does  Routine is not documented  Routine has a bad layout (compare e.g. the styles where expenseType == 2 and expenseType == 3)  Routine reads and writes global variables (corpExpense and profit) – bad way to communicate with other routines

High quality routines Low quality routine:  Routine´s input variable, inputRec, is changed. An input variable should not be modified, and a modifiable variable should not be named inputRec  Routine does not have a single, clearly identifiable purpose

High quality routines Low quality routine:  Routine is not protected against bad data (if crntQtd is zero, ytdRevenue * 4.0 / (double) crntQtr causes an error)  Routine uses “magic numbers”: 100, 4.0, 12, 2, 3  Some parameters are not used: screenX, screenY

High quality routines Low quality routine:  Routine has too many parameters (we should avoid more than a maximum 7 parameters)  Parameters are badly named  Parameters layout is poor  Parameters are poorly ordered  Parameters are not documented

High quality routines Valid reasons to create a routine:  Reduce complexity  Introduce an intermediate, understandable abstraction  Avoid duplicate code  Hide sequences  Hide pointer operations  Improve portability

High quality routines Valid reasons to create a routine:  Simplify complicated boolean tests  Improve performance (actually, give room to performance tuning) ... plus the previously seen good reasons to create a class

High quality routines Do not be ashamed to create very small and very simple routines. The abstraction may well be worth it.

Routine design Major keyword: COHESION A routine should do ONE thing well and not do anything else e.g., Cosine() is probably better than CosineAndTan()

Naming routines Describe everything the routine does Avoid meaningless, vague verbs Do not differentiate routine names solely by number Make names of routines as long as necessary Typically, good variable names are 9 to 15 characters long. Typically, routine names are longer

Naming routines To name a function, use a description of the return value To name a procedure, use a strong verb and an object Use opposites precisely (e.g. add/remove, begin/end, create/destroy) Establish conventions for common operations (e.g. avoid student.id.Get() and lecturer.GetId() in the same system)

Routine parameters Put parameters in a consistent order grouped by input, modify and output, e.g. input-modify-output Consider creading IN and OUT keywords – e.g. in C++: # define IN # define OUT In C++, consider using const for IN parameters

Routine parameters If several routines use similar parameters, put similar parameters in a consistent order Use all the parameters Put status or error variables last Do not use routine parameters as local variables Use local variables instead

Routine parameters Document interface assumptions about parameters Avoid too many parameters – seven is a good maximum Consider a naming convention to identify input, modify and output variables