Page 1 of 1

## Functional Decomposition - Part 1 Analysis of code snippet spelt out for new programmers Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=19657&amp;s=969b8cd86d92b6aba46a018b5ecb2f1e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gregoryH

Reputation: 60
• Posts: 656
• Joined: 04-October 06

Posted 13 October 2006 - 11:00 PM

Introduction
Many new programmers think Functional Decomposition is only for a few cases and simply pile almost all of their code into the main function. This leads to code that is hard to read and even harder to fix.

By the end of this tutorial, the aims of Functional Decomposition, proper use of functions and a practical example of it use will be uncovered.

Along the way, I hope to instill in the reader a sense of purpose to achieve readabile and maintainable code. This includes the use of whitespace to show structure and improve readbility.

This tutorial explains the process involved to create the code snippet located here.

The Problem
I would like to get input from the user as in integer, that is ranged checked between an upper and lower value.

The Analysis (1st Pass)
We begin by extracting the nouns and verbs from the text:
• input from user
• integer
• range check
• maximum
• minimum
Many programs require us to get input from a user many times. It makes good reason to believe that such code would benefit from its own block that we can call. What sort of things should this block do?
• display a message to the user
• range check
• return validated input
Lets take the functionality one byte at a time:
Display Message What do I want my users to be told?
Can I tell them something to make their job easier (User Friendly?)
How do i get the message into the program? via the paramters? hard code?
I will send it via parameters

Receive Input Anything that might upset the input mechanism? eg type a letter and not a number?
how would that be handled?

Range Check How do I detect "out of range"?
How do I get the range values into the function? Hard Code? Parameter?
I will send it via parameters
How would I say that? hmmm
1.if the value is between min and max?
2.if the value is not less than min and not greater than max?

The latter sounds more code like, we'll try that

ReturnWhich way to return to calling program. May be affected by language.

"Psuedo" Code
A trial "paper based" code attempt. We'll start with return value as the Return method
```int getUserInput ( char * msg , int min , int max )
{
int input; // input from user
do{
cout << msg;
cin >> input;
}while ( input >= min && input <= max );
return input;
}

```

That looks simple enough. How do I know if my code is right?

Testing
The best way is to create a small main that calls this program up. Over a period of years, I have met people who shun testing as a "waste of their time". In reality its the response of a person who feels threatened by the prospect that their code should be subjected to any kind of testing.

The test run was not what I expected. Any entry within the range repeated and outside the range exited the code... not what we wanted at all.

Fault Finding
The problem lies in the way we speak and the nearest coded version. C/C++ does have a !< so the nearest code changed that to >= and we said and >= max... clearly the way I thought and coded are wrong.

So how can I word that to trap for out of range correctly?
1. if input is less than min OR greater than max repeat

mmm that does sound better.

Maintenance
Now I have two things to fix - the message and the range detection. My code now looks like this:
```int getUserInput ( char * msg , int min , int max )
{
int input; // input from user
do{
cout << msg << " (" << min << "," << max <" ) : ";
cin >> input;
}while ( input < min || input > max );
return input;
}

```

Re-Test you sinner
Now that I have done the maintenance thing, I should retest to confirm that the code is working as expected.

Once the retest confirms my code, I can now integrate it into a larger project, confident that the code will be much closer to correct than untested code.

Coding conventions
You may notice that my code is spread out a lot, there are "indents" and spaces used a lot.

In the programming world, your code today may have to be maintained by other people. Try to think about how you lay out the code and use Functional Decomp to make your code more readable and as shown in this tutorial, easier to maintain by anyone that has to undertake that task in the future.

Its also good for the people trying to review your code on here. The better the breakdown, the more chance there is of finding a fault in the smaller code blocks.

This post has been edited by gregoryH: 14 October 2006 - 02:55 PM

Is This A Good Question/Topic? 0

## Replies To: Functional Decomposition - Part 1

### #2 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 184
• Posts: 4,673
• Joined: 26-November 04

Posted 14 October 2006 - 02:00 AM

Looking great.
I've corrected a few typos before approving your tutorial.