<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: My main() Method Is Better Than Yours</title>
	<atom:link href="http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=my-main-method-is-better-than-yours</link>
	<description>Testability Explorer</description>
	<lastBuildDate>Thu, 19 Jan 2012 16:42:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: David Leppik</title>
		<link>http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/comment-page-1/#comment-6641</link>
		<dc:creator>David Leppik</dc:creator>
		<pubDate>Fri, 18 Feb 2011 17:27:42 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=195#comment-6641</guid>
		<description>Actually, the main method is the easiest method at all to test-- so easy that you often don&#039;t even need to test it!

Assuming you&#039;ve tested all the code going into main, when you run the program, it&#039;s either going to run properly or it&#039;s going to fail spectacularly, and often with an obvious cause. Typically there aren&#039;t that many permutations to the command line arguments, and each one should be easy to test.

The reason I say you often don&#039;t need to test it is that many main methods (for the sorts of programs I write at least) do exactly one thing, and I&#039;m going to be running it before I hand it off to anyone else.  And if I&#039;m bothering to write a main method at all, somebody&#039;s going to be using it right away.  Which means that even if I were silly enough not to run it before handing it off, the bug would be discovered and I would get my well-deserved public shame long before a customer gets the code.</description>
		<content:encoded><![CDATA[<p>Actually, the main method is the easiest method at all to test&#8211; so easy that you often don&#8217;t even need to test it!</p>
<p>Assuming you&#8217;ve tested all the code going into main, when you run the program, it&#8217;s either going to run properly or it&#8217;s going to fail spectacularly, and often with an obvious cause. Typically there aren&#8217;t that many permutations to the command line arguments, and each one should be easy to test.</p>
<p>The reason I say you often don&#8217;t need to test it is that many main methods (for the sorts of programs I write at least) do exactly one thing, and I&#8217;m going to be running it before I hand it off to anyone else.  And if I&#8217;m bothering to write a main method at all, somebody&#8217;s going to be using it right away.  Which means that even if I were silly enough not to run it before handing it off, the bug would be discovered and I would get my well-deserved public shame long before a customer gets the code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/comment-page-1/#comment-1846</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Thu, 10 Sep 2009 03:53:22 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=195#comment-1846</guid>
		<description>@peter,

Your reasoning is correct, if the main method is responsible for instantiating and wiring all of the pieces together, how does one tests the wiring without actually running the application. The solution is to move the instantiation/wiring into a separate class (which we call factory)</description>
		<content:encoded><![CDATA[<p>@peter,</p>
<p>Your reasoning is correct, if the main method is responsible for instantiating and wiring all of the pieces together, how does one tests the wiring without actually running the application. The solution is to move the instantiation/wiring into a separate class (which we call factory)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/comment-page-1/#comment-1836</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Mon, 07 Sep 2009 00:30:42 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=195#comment-1836</guid>
		<description>Hey!
Maybe I&#039;m missing  the point here, but why not wrap the startup task inside a command (pattern)eg. ApplicationStartupCommand that takes a Server and args as the constructor arguments ..
eg. 

public class ApplicationStartupCommand extends Command{
 
  Server server;
  ApplicationStartupCommand ( Server server){
	this.server = server
 }

  public void Run(){
        server.Start(); }
....
 }


}
Then the ApplicationStartupCommand becomes testable (because we&#039;ve seperated it from the application and we can mock the server) and the main method (applying MY simple mind to this) becomes.
// for the Manual injection
 static void main(String[] args){
   Command startup  =new StartupCommand ( new ServerFactory(args)
.createServer() ) ;
   startup.Run();
}

I&#039;m not a Java guy so forgive me for not know what the server factory etc does.

It would be cool if you could shed some light on the above. 
Once again, Thank you</description>
		<content:encoded><![CDATA[<p>Hey!<br />
Maybe I&#8217;m missing  the point here, but why not wrap the startup task inside a command (pattern)eg. ApplicationStartupCommand that takes a Server and args as the constructor arguments ..<br />
eg. </p>
<p>public class ApplicationStartupCommand extends Command{</p>
<p>  Server server;<br />
  ApplicationStartupCommand ( Server server){<br />
	this.server = server<br />
 }</p>
<p>  public void Run(){<br />
        server.Start(); }<br />
&#8230;.<br />
 }</p>
<p>}<br />
Then the ApplicationStartupCommand becomes testable (because we&#8217;ve seperated it from the application and we can mock the server) and the main method (applying MY simple mind to this) becomes.<br />
// for the Manual injection<br />
 static void main(String[] args){<br />
   Command startup  =new StartupCommand ( new ServerFactory(args)<br />
.createServer() ) ;<br />
   startup.Run();<br />
}</p>
<p>I&#8217;m not a Java guy so forgive me for not know what the server factory etc does.</p>
<p>It would be cool if you could shed some light on the above.<br />
Once again, Thank you</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/comment-page-1/#comment-590</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Sun, 18 Jan 2009 16:39:55 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=195#comment-590</guid>
		<description>Hi Iwamot,

The GUICE module should be one responsible for parsing the args using your favorite CLI parser. The module should than set up different bindings based on the arguments. Once GUICE builds the server and hands it back to you the server is already wired with all of the configuration information set. You just need to start it.</description>
		<content:encoded><![CDATA[<p>Hi Iwamot,</p>
<p>The GUICE module should be one responsible for parsing the args using your favorite CLI parser. The module should than set up different bindings based on the arguments. Once GUICE builds the server and hands it back to you the server is already wired with all of the configuration information set. You just need to start it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: iwamot</title>
		<link>http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/comment-page-1/#comment-589</link>
		<dc:creator>iwamot</dc:creator>
		<pubDate>Sun, 18 Jan 2009 08:30:47 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=195#comment-589</guid>
		<description>Hi Misko, I&#039;m a manual DIer (and am a Japanese).First, for the second example, I think the &#039;args&#039; should be passed to the createServer() method. The constructor doesn&#039;t seem to use it. Any reasons?Second, I think there should be a &#039;Parsing Phase&#039; before the &#039;Creation Phase&#039;. The &#039;args&#039; often needs to be parsed by a parser(ex. Commons CLI). What&#039;s your opinion?</description>
		<content:encoded><![CDATA[<p>Hi Misko, I&#8217;m a manual DIer (and am a Japanese).First, for the second example, I think the &#8216;args&#8217; should be passed to the createServer() method. The constructor doesn&#8217;t seem to use it. Any reasons?Second, I think there should be a &#8216;Parsing Phase&#8217; before the &#8216;Creation Phase&#8217;. The &#8216;args&#8217; often needs to be parsed by a parser(ex. Commons CLI). What&#8217;s your opinion?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Where Have all the &#8220;new&#8221; Operators Gone &#124; Miško Hevery</title>
		<link>http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/comment-page-1/#comment-150</link>
		<dc:creator>Where Have all the &#8220;new&#8221; Operators Gone &#124; Miško Hevery</dc:creator>
		<pubDate>Wed, 10 Sep 2008 17:23:56 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=195#comment-150</guid>
		<description>[...] My main() Method Is Better Than Yours we looked into what a main() method should look like. There we introduced a clear separation [...]</description>
		<content:encoded><![CDATA[<p>[...] My main() Method Is Better Than Yours we looked into what a main() method should look like. There we introduced a clear separation [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhirama</title>
		<link>http://misko.hevery.com/2008/08/29/my-main-method-is-better-than-yours/comment-page-1/#comment-144</link>
		<dc:creator>Abhirama</dc:creator>
		<pubDate>Sun, 07 Sep 2008 06:37:08 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=195#comment-144</guid>
		<description>I have a doubt regarding the separation of object graph creation and application running. Say I am writing a person class and the constructor takes weight as an argument. Now I want to make sure that the weight assigned is within some bounds, say it should not be negative or something like that. Do you advocate doing this validation in the constructor itself, before assigning to the weight instance variable?

Btw thanks for the wonderful blog.</description>
		<content:encoded><![CDATA[<p>I have a doubt regarding the separation of object graph creation and application running. Say I am writing a person class and the constructor takes weight as an argument. Now I want to make sure that the weight assigned is within some bounds, say it should not be negative or something like that. Do you advocate doing this validation in the constructor itself, before assigning to the weight instance variable?</p>
<p>Btw thanks for the wonderful blog.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

