Bravo! Nice example of good program maintainability.
The key is logical separation of a project into functions - if some piece of code has a logical conclusion - there should be a function with the name describing that logic (no matter if the code repeated even once - in the future - the same functionality can be required in other part(s) of the code, so if you already have the function - program becomes easily readable/maintainable).
:
This message was edited by dennisparker at 2004-11-10 13:6:18
: : Please forgive me if my lack of formal programming education is the real trouble, but I see problems being asked on the board now and then where part of the problem states to call a function to do something that I can do as part of one line of code.
: :
: : If one line of code can be used within main() to determine some result, would it still be "better" or perhaps more "proper" to send out to a function?
: :
: : As an example, the recent post "coin flip in C++" states "the program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads."
: :
: : If I write this program with a function called flip, I have to add several lines. If I omit the need for the function flip, I can calculate the answer in one line plus the printf/cout line.
: :
: : Is the need for a separate function just a part of this class structure at this point in its schedule or is it more proper to perform everything in separate functions? Would I be a problem student in this class?

: :
: : Comments please. Thanks.
: :
: : Take Care,
: : Ed
: :
: :
:
: I have found it important to seperate into functions those parts of a program that perform an identifiable job.
:
: For instance, the line
:
:
: SetDigital( INDEX, ON );
:
:
: performed the same task as
:
:
: Extend_Index();
:
:
: Which was simply a two line function:
:
:
: int Extend_Index( void ){
: SetDigital( INDEX, ON );
: return TRUE;
: }
:
:
:
: in a recent project. The reason it was useful to put that "job" into a function became apparent when unforseen modifications to the machine changed how the job was performed.
:
: The new code:
:
:
: int Extend_Index( void ){
: int return_value;
: SetDigital( INDEX, ON );
: MilliDelay( 2000 );
: if( GetDigital(PROX, &bValue) )
: return_value = TRUE;
: else
: return_value = FALSE;
: }
:
:
: took care of the mods. I only had to change the code within Extend_Index() rather than dozens of other places in main(). This is the reason that I prefer to put most of my code, even one liners, into functions.
:
:
:
:
:
:
: