Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

where is the main method now...where should code be placed?  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

i realize its better to put your main method in a new class...instead of running it in the existing class like so:



should code be placed in X or in Y

a print statement worked in either...so i wasnt sure

thanks
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ideal size for a main method is one statement. Look at our FAQ.
 
Junilu Lacar
Sheriff
Posts: 11125
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:
should code be placed in X or in Y

a print statement worked in either...so i wasnt sure

A print statement will work anywhere it's syntactically legal to put it. That's not the point though nor is it the criteria you should use to decide where it goes. So how do you decide? Well, how do you decide to put your refrigerator in your kitchen rather than in your bathroom? After all, it will work exactly the same if you plug it into the wall in either room, right? What it makes the kitchen a better place?

It's about the appropriateness of location and proximity to other things that you use with the refrigerator and the activities that involve their use, right?  These are pretty much the same considerations you would have in deciding where a piece of code should go. What makes most sense? Where is it used the most? What is the most convenient, safest, sanitary? That last one might have you thinking "What's sanitary code?"  Well, to me, sanitary code is clean code and clean code has clarity of purpose/intent, proper assignment of responsibility, high cohesion and minimal to no duplication, loose coupling, and testability, among other qualities.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
junul

that was fantastically explained...thanks
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an application without a Graphical User Interface (GUI), I tend to follow Campbell Ritchie's pattern, having on;ly one line in main().
However, for programs using a Swing GUI, I find that I keep repeating the same code in the primary class, so now I tend to use a larger "template" for main() that creates the JFrame, l creates my application's main panel and adds it to the frame, then displays the frame from the event thread. So most of my main() methods look like this (for an application I will call MyAppPanel). Note that MyAppPanel has a method called close() to do whatever is desired if the user closes the window:

I tend to make this the only function in my MyApp class. That way I can use an instance of MyAppPanel as a part of some other application if desired.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still don't like that main method. Create a setUpGUI method and use that to create the display. Also the display should be separate from any business logic.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Point taken, Campbell.

I also noted that my previous post neglected to include the line

 
Dave Tolls
Rancher
Posts: 2909
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tend to use a GUIController that does all the GUI set up as well as listening in on page changes from sub (page) controllers.
It's the thing that owns the JFrame.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!