• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use main?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all, I have been moving along with my Java studies but I still have a couple of reoccuring issues/questions. My first question is there some standard for how much code should be in main? Is it a waste of time to do something like this

Java Main Class(Main program Name) creates an instance of some other class which has a start() function and that class is what creates most of the other objects and calls most other functions etc... Or should i just use main for that? Seems like I am just using another class for the sake of not having much code in main. Unless that is good practice. For some reason i have in my head that main should not contain very much code at all. I defer to the experts on what they have to say on this matter.

Thanks.

Random Question: I have the book Code Complete, at what point should someone begin reading that book? What exactly does that book teach in terms of my development learning Java? I just am trying to figure out where it fits in as far the other technical books I have been reading. I bought it a while back because it was so well reviewed by everyone on Amazon.

Thanks.
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ideally we use main to launch an application and rest can be taken care by different methods or functionality as and when required. If you are just practicing java, don't worry about the beautification.
You can think of it as, if you have a method say,

it should only do summation and return result as is evident by its signature and nothing else.
So in context of main, it should give you a start for your application.

I don't know about code complete
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ish Del wrote:Java Main Class(Main program Name) creates an instance of some other class which has a start() function and that class is what creates most of the other objects and calls most other functions etc... Or should i just use main for that?


That's very difficult to answer, but what I will say that that a main() method should be easily understood by someone other than yourself; and that usually means keeping code to a minimum, but whether that minimum is 2 lines or 20 is up to you. I'd say that you certainly don't want it to be much more than 20 though.

Seems like I am just using another class for the sake of not having much code in main.


No, you use a class because it's the right thing to do. Sure, you could probably write an entire accounting application in a single main() method; but would you want to come back to 5,000 lines of monolithic code in 6 months time when you've forgotten what you were thinking? Let alone have to debug it if there are problems?

Classes and methods help to divide problems up into manageable chunks and, in general terms, smaller methods (any methods) are better; but there's no "magic number" of lines that tells you whether one is good or bad. My basic rule of thumb is "no more than a screenful", but I've broken it many times.

Think of it like a poem: Do you need to write a new Beowulf to say what you want, or will a few pithy lines do instead?

Winston
 
Ish Del
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Than You for your responses. So let me make a 1000ft level example. lets say i was making a program to store my movies list.

Classes:
Main(name of program) is MyMovies
Ouput to screen class
Input from user class
movie class

I know this is a small program so it doesnt matter but would you have MAIN create the ARRAY that holds movie objects and Main makes an instance of input and output? Or should i create a 2nd class called MovieProgram which does all that. And all main does is instantiate the MovieProgram and run the start method.

I know this is an overly simplified example but it better explains what i was trying to ask.

That might be good advice to forget the beautification but how does one learn good practices or how to better do things? Is it just experience and work or is this something that could be gotten from an object oriented design book or design patterns book?

Thanks again.
 
Marshal
Posts: 79152
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That might be a small program, but if you want to store movies and show them on screen with a GUI, you will be lucky to get away with only using 100 classes. A lot of those will be ready made for the display, fortunately.
You are going about it the wrong way by trying to work out how many classes you need. You need as many as you need. A bit like when us men cook. We need as many pots and pans as we need (as any woman will tell you!)
What you ought to do is work out how to store movies, and then later on you can think about a GUI to go on top. Remember the amount of effort to store 2000 movies is only slightly more than that required to store 2 movies (≡time to type in the other 1998 movies:wink:‍).

Start by writing a description of how you would store your movie details. Only when you have worked that out can you even think of writing any code.
 
Campbell Ritchie
Marshal
Posts: 79152
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ish Del wrote: . . .
I know this is a small program so it doesnt matter but would you have MAIN create the ARRAY that holds movie objects and Main makes an instance of input and output?

Nonononononononono

What array? Why are you thinking you want to put your movies into an array? At this stage you should forget there are such things as arrays, and consider the general functionality you want. Similarly what input and output instances?

Or should i create a 2nd class . . . And all main does is instantiate the MovieProgram and run the start method.

yesyesyesyesyes And a 3rd class, a 4th class, a 5th class, a 6th class …
 
Campbell Ritchie
Marshal
Posts: 79152
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . a main() method should be easily understood by someone other than yourself; and that usually means keeping code to a minimum, but whether that minimum is 2 lines or 20 is up to you. . . .

I take a more extreme view. One statement to start the application and that’s it. I know I am at the extreme end of the spectrum; there are probably very few people who advocate main methods with fewer statements in than I do
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:there are probably very few people who advocate main methods with fewer statements in than I do


Considering your statement, that would be tough.

Winston
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, we could always put code in a static initialization block instead.

I tend to make the main method responsible for parsing the arguments, if any (perhaps by calling arg4j) and then using that to instantiate something and call a method. And maybe have an outermost try/catch so I can control where any escaping exceptions get logged to. But all other logic gets pushed to some other method, so it's easier to test, or mock out.
 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Winston Gutkowski
pre Java 7:

would do the trick. That example is/was really bad. So don't do that!

@Mike Simmons
I would argue that you should write an argument parsing class for that.
Simply creating an instance of a class and maybe calling one method on it is usually enough.
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manuel Petermann wrote:@Mike Simmons
I would argue that you should write an argument parsing class for that.


Sure, as I alluded to with the args4j comment. But do you call that class from main, as I do, or from the next method down the chain? To me, command line arguments are part of the basic responsibilities of the main method; might as well call off the parsing from there, rather than defer one more level. Example from the args4j docs:

This is within my limit of complexity of what one method should do, and allows the next methods down the chain (parseArguments and run) do concentrate on something useful, rather than merely replicating the signature of main() from a non-static context.

But, to each their own. I don't see anything wrong with doing it as you and Campbell describe. But I have limited patience for being told I should do it that way, without some sort of reasoning.
 
Ish Del
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That might be a small program, but if you want to store movies and show them on screen with a GUI, you will be lucky to get away with only using 100 classes. A lot of those will be ready made for the display, fortunately.
You are going about it the wrong way by trying to work out how many classes you need. You need as many as you need. A bit like when us men cook. We need as many pots and pans as we need (as any woman will tell you!)
What you ought to do is work out how to store movies, and then later on you can think about a GUI to go on top. Remember the amount of effort to store 2000 movies is only slightly more than that required to store 2 movies (≡time to type in the other 1998 movies:wink:‍).

Start by writing a description of how you would store your movie details. Only when you have worked that out can you even think of writing any code.



Ha i have to say that is hysterical. I had no idea how universal that sentiment was. I always get yelled at for how many pots i use lol. Darn it woman! I am making a masterpiece here...of mac and cheese

I guess i may have been going about it wrong. I in no way am writing a full featured program that was just a simple example I could think of. So having main just start the program seems like a standard procedure used by many so I will stick to that. Forget Swing I could write another post with tons of questions on how to pass information to a gui or present information in real time. For now I am sticking to console input and output and throwing in some saving and reading from a file. I am trying to really get a nice grasp of object oriented design while still writing enough programs to learn the java syntax and java specific language elements (for example how java passes information around between variables and things like that).

Thanks for your answers everyone.



 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manuel Petermann wrote:@Winston Gutkowski: pre Java 7:
<tortuous example>
would do the trick...


Ooof. A fellow of the Douglas Adams Academy of Programming, no doubt.

However, I stand corrected and grovel at Campbell's feet.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic