Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function question 2

Status
Not open for further replies.

zooxmusic

Programmer
Nov 24, 2004
96
US
Hi all,
Quick question about how to write functions. I have read it is better to write show() and hide() rather than setVisible(boolean) so going on that logic is it better to have a seperate function to wrap up some process like doAJob(), doBJob(), doCJob() rather than doJob(typeOfJob)? And I need to understand the reasoning behind WHY this is better.

Spend like you don't need the money,
love like you've never been hurt and dance like no one's watching!
 
Interesting you say that, if you say:
doJob(typeOfJob) that implies a strategy pattern. Example,

DoSave(new HtmlSaver())
or
DoSave(new RTFSaver())


"is it better to have a seperate function to wrap up some process like doAJob(), doBJob(), doCJob()"

Well, I learned once from my mentor that programming internal functions with seperate method workers is better that coding everything in one single methiod. Take for example, this is how I would code the HtmlSaver() strategy above.

Pesudo code:

public class HtmlSaver {

public void Save(string aCharString) {
//Preform algorithm steps in order
runHtmlValidation(aCharString);
removeExtraChars(aCharString);
encode(aCharString);
saveToDisk();
}
}


See how easily readable that was? I can look at that HtmlSaver and know exactly whats going on, and in what order. I call that worker methods and the Save() being the manager. The manager coordinates a task by using a group of internal workers.

Pretty neat huh?
 
zooxmusic

Have a read of "Refactoring", by Martin Fowler et al. This is exactly what EdwardsJS is talking about. He doesn't need to comment his code, because you can see exactly what it's doing because the function names are so descriptive. If any one of the functions needs to be debugged or amended, it's small enough to get your head around easily, even months later. If it isn't, then break it up into smaller chunks.
 
Exactly!
You won't believe how many times I get comments from other co-engineers saying things like "Why havn't you commented your code" or "You need commenting in there to make it more readable"


No you don't. Any good engineer that limits their use of commenting does so by employing good seperation of worker methods and high cohesion.

Code is more comprehendable and without the need for all those //'s

- EdwardJS
 
Both of you are awesome! And stevexff I have only read a little exerpt of "Refactoring" so I will pick that up. I am about to go pick up Design Patterns and I am already trying to read UML in 24 hours and Design Patterns in Java Vol 1 so I am feeling overwhelmed. But thanks to all of you guys here, my job is becoming easier. I am frustrated because I have a project with a deadline that will come due before I can get this stuff assimilated. So I will be an annoying little bug on this forum because you guys are at least letting me create this project so the refactoring later once I get this stuff won't be as bas as it could be.

Spend like you don't need the money,
love like you've never been hurt and dance like no one's watching!
 
zooxmusic

If you are going to refactor later, then you need to start using JUnit (or NUnit for .NET) NOW. Then when you start refactoring later, you can easily retest your code to make sure it still behaves correctly...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top