Download presentation
Presentation is loading. Please wait.
1
High-Quality Routines
Chapter 7
3
What is a “routine?” A routine is an individual method or procedure achieves a single purpose. Examples include a function in C++, a method in Java, a function or sub procedure in Visual Basic. The routine makes programs easier to read and easier to understand. It is also used for saving space and improving performance
4
Valid Reasons to Create a Routine
Reduce complexity Make a section of code readable Avoid duplicate code Hide pointer operations Improve portability Improve performance
5
Cohesion have each routine do one thing well and not do anything else. (High Cohesion) Examples of highly cohesive routines include sin(), GetCustomerName(), EraseFile(), CalculateLoanPayment(), and AgeFromBirthday(). Example of low cohesive routine is TanAndCosine() One study of 450 routines found that 50 percent of the highly cohesive routines were fault free. To increase cohesion, split the operations into individual routines.
6
Good Routine Names .. Guidlines
Describe everything the routine does Avoid meaningless or wishy-washy verbs: Routine names like HandleCalculation(), PerformServices(), ProcessInput(), and DealWithOutput() don’t tell you what the routines do. HandleOutput() could be replaced with FormatAndPrintOutput() Make names of routines as long as necessary To name a function, use a description of the return value If a function returns a value, the function should be named for the value it returns. For example, cos(), customerId.Next(), printer.IsReady(), and pen.CurrentColor() are all good function names that indicate precisely what the functions return
7
Good Routine Names .. Guidlines
Use opposites precisely Examples add/remove begin/end create/destroy first/last get/put get/set increment/decrement insert/delete lock/unlock min/max next/previous
8
How Long Can a Routine Be?
IBM once limited routines to 50 lines TRW limited them to two pages In worst case, number of lines in one routine should not exceed 200 lines
9
How to Use Routine Parameters
Interfaces between routines are some of the most error-prone areas of a program. Put parameters in input-modify-output order If several routines use similar parameters, put the similar parameters in a consistent order Use all the parameters
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.