Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Functions

 
Mark Petruszak
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it fair to say that when using functions, if you don't have to return a value, then don't return one?
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Petruszak
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic