aspose file tools*
The moose likes Beginning Java and the fly likes Main method too many codings?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Main method too many codings??" Watch "Main method too many codings??" New topic
Author

Main method too many codings??

Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
is it advisable not to have too much coding in main method??

I actually have a menu system in the main method using switch case



as u can c, my main method will become very long if I have many options.
what should i do to make my program neater? if I put static void option1() in my case 1, is it still considered OOP??

Thakns a lot
Patrick van Zandbeek
Ranch Hand

Joined: Aug 17, 2005
Posts: 37
Hi Timothy,

personally I would always try to keep main rather clean. Depending on the complexity of your program and the likelyhood of extending upon it of course, a couple of lines of code just to learn something never hurts.

From the info you give I would probably create a menu processing object or something like that and store the methods for displaying and the menu choices and processing them in there. That allows for a lot of flexibility in your desing and keeps main easy to understand (if you choose meaningful object and method names of course).

Hope I have given you some ideas to work with.

If the use of certain language constructs that get the job done is still considered OOP is something I like to leave to the academics, it's usually easier to debate politics or religion then OOP software design with some people.

But I'm sure someone has some thoughts on it.


u is a letter, not a word.
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
you should probably do a different class altogether for your menu.


I'm a Hood Ornament
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Timothy had a good question about making another static method for each choice in the menu. I try to get out of static mode right away by creating a real object instance:

This makes someMethod() more like most of the non-static methods on most objects.

Now you can create a method per case. In "real life" I'd start to get concerned after about 5 choices, but for practice or school assignments I wouldn't worry too much if there are more. There are there are a couple rather advanced ways to replace a long case statement with other structures. Something to look forward to later.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
so I should do this??


Should I put option1() & option2() as static function???
if i do it in the above way, will it still be object oriented??
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yes, that puts the code for each option cleanly into its own method. This isn't gospel or anything, but instance methods are somewhat "better object stuff" than static methods. One exception might be if we moved the methods to an object (instead of the class) and then only ever made one instance of the object, which is probably just what you'd do, so it's a bit of a toss-up. I'd probably go to something like this:

Now I can write and test getUserChoice with no other methods. I can write and test doSomeAction without the menu. That's not much change from what you had, but tries to make each method say exactly what it does in the fewest possible words. Sound like fun?
[ August 28, 2005: Message edited by: Stan James ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main method too many codings??