|
-
Jan 18th, 2019, 01:32 PM
#7
Re: I... Err... Wait... What?!
Why do you create any function?
1. Allow self documentation of the code.
2. Reduce repetitious logic scattered throughout the code.
3. Allow modifying the logic in one place, rather than having to track down all the places one could have implemented the logic.
4. Testable code.
The user could have specified a literal 365 multiple places in the code where one might call the function with the IsAnnual flag is set, or set it some other literal or calculated value where the duration would be passed and the IsAnnual flag not be set, but what the value represents at each place may not be immediately apparent.
This is also assuming the function is being called with literal values where used. The values may be coming from another source, data driven, message driven, transaction queue, etc.
I've written code where I first write out the logic as a series of statements of what needs to be done. I then put underscores in place of the spaces, and turn them into sub or function calls. Now the process should be easy to follow as the series of sub and function calls explaining in their name what they are doing.
Then, it is just a matter of filling in the content of each sub or function to do what its name says it is doing. Some of those functions could end up being one liners, so technically would be more efficient to just put the line in the code rather than use a sub or function for it, but the "aliasing" of the operation with a descriptive method name in its place can be worth it for maintainability.
Snippet of code from a card game program as an example.
Code:
'The cards must all be sequential and same suit to be dragged here.
'Only kings can be moved to an empty normal stack
'Card dragged must follow suit of card dropped on, i.e. 2 of hearts on 3 of hearts
If To_Stack <> From_Stack Then 'don't need to process if dropped on the same stack
If Dragged_Cards_are_Sequential_and_the_Same_Suit() Then
If The_ToStack_is_Empty_and_The_Dragged_Card_is_a_King() Or _
The_Last_Card_of_the_ToStack_is_One_Greater_than_the_Dragged_Card() Then
Update_History() 'save move for Undo
link_Card() 'Link the Card(s) to the Normal Stack dropped on
End If
End If
End If
RePosition_Cards() 'Draw cards in their new location
End If
Send_any_Free_Cards_Home() 'See if any cards are now free to go home
Last edited by passel; Jan 18th, 2019 at 03:15 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|