go to previous page   go to home page   go to next page



The fundamental idea, that a module should have one entry point and one exit point has been violated! This function has several exit points.

Early Returns

Often a function is more clear if it returns the result of computation as soon as it is available. This might be done at several locations in the code. This notion is called early returns and is usually regarded as acceptable in well-designed code.

Ideally, early returns are not deeply nested in the code and occur near the top of the function. Often, early returns are used when there are easily tested special cases.

But... what happened to the idea of structuring?

You can think of the flowchart on the right as an abbreviation for the flowchart on the left. The idea of structuring is still there, and should be foremost in your mind when you design code. Modularity still holds: if the pre-conditions are true then the exit conditions will be met when control leaves the function.

Max of Three Flowchart Max of Three Flowchart


Now say that you need a function that returns the maximum of its nine integer arguments. The prototype is:

int maxNine( int a, int b, int c, int d, int e, int f, int g, int h, int i );

How would you design this function?