HIGH-Quality Routines (1)

xiaoxiao2021-03-06  55

What is a "routine"? A routine is an Individual Method or procedure invocable for a single purpose.

What is a high-quality routine It's a difficult question because it may include many features such as:?.... 1 has a good name2 has good document.3 have good layout.4 does not read or write the global variables. 5. HAS A Single Purpose.6. Defends itself against bad data.7. Doesn't Use magic number.8. Has Appropriate parameters.

Reason to Create a Routine:.. 1 Reduce complexity.2 Make a section of code readable.Putting a section of code into a well-named routine is one of the best ways to document its purpose.3 Avoid duplicate code.4.. hide sequencesIt's a good idea to hide the order in which events happen to be processed. WHY ??? 5. Improve protability.6. Simplify complicated boolean tests.Understanding complicated boolean tests in detail is rarely necessary for understanding program flow. Putting such a Test INTO A Function Makes The Code More Readable.7. Improve Performance.

One of the strongest mental blocks to creating effective routines is a reluctance to create a simple routine for a simple purpose. Constucting a whole routine to contain two or three lines of code might seem like overkill. But experience shows how helpful a good small routine can BE. First, IT Improves The Readable. Second, The Small Operation TEND to Turn Into Larger Operations.

Good Routine Names1. Describe everything the routine does.2. Avoid meaningless or wishy-washy verbs.Routine names like HandleCalculation (), PerformServices (), ProcessInput () do not tell you what the routines do.3. Make names of routines AS Long As Necessary.research Shows That The Optimum Average Length For a Variable Name IS 9 To Name A Function, Use A Description of the Return Valuesuch As (), getColor (), isReady () 5. To name a procedure, use a strong verb followed by an object.6. use opposite precisely.such as add / remove, begin / end, first / last, get / put.Type of Cohesion1. Functional cohesionIs the strongest ad best kind of cohesion, occurring when a routine performs one and only one operation.2. Sequential cohesionExists when a routine contains operations that must be performed in a specific order, that share data from step to step, and that do not make up a complete function when Done together.3. Communicational CohesionOccurs when Operation In A Routine Make Use Of The Same Data And Aren't Related In Any Other Way.4. Temporal CohesionOccurs When Operations Are Combined Into A Routine Because Time. Time. Time. Time. Time. type

The remaining kinds of cohesion are generally unacceptable.5. Procedural cohesionOccurs when operations in a routine are done in a specified order6. Logical cohesionOccurs when serveral operations are stuffed into the same rouitne and one of the operations is selected by a control flag that's passed in .7. COINCIDENTAL CO winsionocurs WHEN The Operations in a Routine Have No Discernible Relationship to each other.

转载请注明原文地址:https://www.9cbs.com/read-114162.html

New Post(0)