aspose file tools*
The moose likes Cattle Drive and the fly likes Functions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Functions" Watch "Functions" New topic
Author

Functions

Mark Petruszak
Greenhorn

Joined: Jan 30, 2007
Posts: 9
Is it fair to say that when using functions, if you don't have to return a value, then don't return one?


-Mark
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Technically Java has methods, not functions.

I think you should "use the simplest thing that could possibly work". This is also known as the KISS (Keep It Simple, Silly) principle which is very popular around here.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mark Petruszak
Greenhorn

Joined: Jan 30, 2007
Posts: 9
So it's okay then if I have a program that takes me from one method to the next and so on, and I end up 32 methods away from 'main' when the program terminates?
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1364
    
  17
As always: I am no expert, so I'll just give you my take on the issue...

The short answer is "yeah, kind of". There is no clear cut rule, and that if it makes sense to pass information from method to method 32 methods away from main(), then that is what you will end up doing.

Usually, though, I find that main() is a starting point, and if I am working with objects, main() is a good place to instantiate the object that is in charge of directing traffic in my program. Who does what next seems to be more of a question of who can take responsibility for what. When I make a class, I am always asking myself "what does this object know, and what can this object do?".

In a school administration program, students know who they are, what classes they are taking, and they can apply for classes and take exams. They can check information about classes they are enrolled in (who is the main lecturer? When is the midterm?). A student should not be able to write their own diploma, though - the writeDiploma() method belongs in a different object... not Student, not Professor, probably some form of Administration object.

In the beginning of the cattle drive, there is no need for objects, and main() often holds the control structure of whatever you are doing, but you can make methods to get specific things done, and sometimes you will make a method that does a rather large specific thing, and want to have small methods that can take care of something that you need to do in the larger method.

Hm... it seems like you can sum up all of the above to say: It depends.

Try stuff out, play around with it. The more you try different ways of doing things, the more the nitpickers will be able to guide you in the right direction.

- my two cents
Mark Petruszak
Greenhorn

Joined: Jan 30, 2007
Posts: 9
Thank you Katrina and Marilyn for your responses. I don't program for a living so little things like this sometimes puzzle me. I did take a class in VB.Net at a local college and was taught to start the program from main and terminate the program from main. Any pre-application initialization and post application administration can be put in main as well.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Java kind of does that for you automatically behind the scenes, Mark. Even if you don't return anything, the method returns up the chain of methods and ends up back in main. But that's not obvious because you don't see anything happening. To see this, put a System.out.println call just before the closing curly brace in main and watch when it prints -- just before the program exits -- regardless of whatever else prints while the program is running. At least that's true in the little applications we're writing in the CattleDrive.

But ... it's not something you have to do. It may appear to you that the program ended somewhere else, and that's fine too.
[ February 25, 2007: Message edited by: Marilyn de Queiroz ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Functions