<?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: Using short if statement in programming</title>
	<atom:link href="http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/</link>
	<description>The life experiences of Adam Douglas.</description>
	<lastBuildDate>Wed, 01 Feb 2012 13:06:34 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Adam</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23156</link>
		<dc:creator>Adam</dc:creator>
		<pubDate>Tue, 14 Oct 2008 19:11:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23156</guid>
		<description>Thanks to everyone for the great feedback! I have made changes to the ternary operator example to remove the unnecessary echo statements.</description>
		<content:encoded><![CDATA[<p>Thanks to everyone for the great feedback! I have made changes to the ternary operator example to remove the unnecessary echo statements.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mysql</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23132</link>
		<dc:creator>mysql</dc:creator>
		<pubDate>Fri, 10 Oct 2008 12:05:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23132</guid>
		<description>You also move &quot;echo&quot; function out of statment:

echo $nFoo &gt; 0 ? &quot;I&#039;m at the work.&quot; : &quot;I&#039;m at home.&quot;;

I personaly add braces when using this type of statment to make it more readable:

echo (($nFoo &gt; 0) ? &quot;I&#039;m at the work.&quot; : &quot;I&#039;m at home.&quot;);</description>
		<content:encoded><![CDATA[<p>You also move &#8220;echo&#8221; function out of statment:</p>
<p>echo $nFoo &gt; 0 ? &#8220;I&#8217;m at the work.&#8221; : &#8220;I&#8217;m at home.&#8221;;</p>
<p>I personaly add braces when using this type of statment to make it more readable:</p>
<p>echo (($nFoo &gt; 0) ? &#8220;I&#8217;m at the work.&#8221; : &#8220;I&#8217;m at home.&#8221;);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hartmut</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23129</link>
		<dc:creator>hartmut</dc:creator>
		<pubDate>Thu, 09 Oct 2008 12:14:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23129</guid>
		<description>A better example would have been

  echo ($nFoo &gt; 0) ? &quot;I&#039;m at the work.&quot; : &quot;I&#039;m at home.&quot;;

as one of the main differences between an if block and a ternary (besides the amount of keystrokes needed) is that the ternary is an expression and so has a result value.

IMHO it is also good style to put ternary parts on different lines unless they are *really* short like &quot;$leap_year ? 29 : 28&quot;:

  echo ($nFoo &gt; 0) 
     ? &quot;I&#039;m at the work.&quot;
     : &quot;I&#039;m at home.&quot;;</description>
		<content:encoded><![CDATA[<p>A better example would have been</p>
<p>  echo ($nFoo &gt; 0) ? &#8220;I&#8217;m at the work.&#8221; : &#8220;I&#8217;m at home.&#8221;;</p>
<p>as one of the main differences between an if block and a ternary (besides the amount of keystrokes needed) is that the ternary is an expression and so has a result value.</p>
<p>IMHO it is also good style to put ternary parts on different lines unless they are *really* short like &#8220;$leap_year ? 29 : 28&#8243;:</p>
<p>  echo ($nFoo &gt; 0)<br />
     ? &#8220;I&#8217;m at the work.&#8221;<br />
     : &#8220;I&#8217;m at home.&#8221;;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: LGB</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23126</link>
		<dc:creator>LGB</dc:creator>
		<pubDate>Thu, 09 Oct 2008 09:54:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23126</guid>
		<description>$nFoo &gt; 0 ? echo &quot;I&#039;m at the work.&quot; : echo &quot;I&#039;m at home.&quot;;

This is the overcomplicated version, still. You can use this:

echo $nFoo &gt; 0 ? &quot;I&#039;m at the work.&quot; : &quot;I&#039;m at home.&quot;;

Or even:

echo &quot;I&#039;m at &quot;.($nFoo &gt; 0 ? &quot;the work&quot;:&quot;home&quot;);</description>
		<content:encoded><![CDATA[<p>$nFoo &gt; 0 ? echo &#8220;I&#8217;m at the work.&#8221; : echo &#8220;I&#8217;m at home.&#8221;;</p>
<p>This is the overcomplicated version, still. You can use this:</p>
<p>echo $nFoo &gt; 0 ? &#8220;I&#8217;m at the work.&#8221; : &#8220;I&#8217;m at home.&#8221;;</p>
<p>Or even:</p>
<p>echo &#8220;I&#8217;m at &#8220;.($nFoo &gt; 0 ? &#8220;the work&#8221;:&#8221;home&#8221;);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas CORBIERE</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23125</link>
		<dc:creator>Thomas CORBIERE</dc:creator>
		<pubDate>Thu, 09 Oct 2008 06:53:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23125</guid>
		<description>This example doesn&#039;t work as echo is not an expression and the ternary operator expects his arguments to be expressions. The correct example should be :

echo $nFoo &gt; 0 ? &quot;I&#039;m at the work.&quot; : &quot;I&#039;m at home.&quot; ;</description>
		<content:encoded><![CDATA[<p>This example doesn&#8217;t work as echo is not an expression and the ternary operator expects his arguments to be expressions. The correct example should be :</p>
<p>echo $nFoo &gt; 0 ? &#8220;I&#8217;m at the work.&#8221; : &#8220;I&#8217;m at home.&#8221; ;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy Peterson</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23123</link>
		<dc:creator>Jeremy Peterson</dc:creator>
		<pubDate>Thu, 09 Oct 2008 01:35:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23123</guid>
		<description>I used to hate the ternary operator, because it puts two possible values for a variable on one line and I&#039;d like to keep my style for if/else statements consistent.  

It is much easier to put these lines of code on one line than taking up six lines of code.  Not only is it a matter of space, but  for simple variable definitions it makes it easy to have a default value.

Ex:
$page_num = ($_REQUEST[&#039;page&#039;]) ? $_REQUEST[&#039;page&#039;] : 1;// Default 1st page.

Jeremy</description>
		<content:encoded><![CDATA[<p>I used to hate the ternary operator, because it puts two possible values for a variable on one line and I&#8217;d like to keep my style for if/else statements consistent.  </p>
<p>It is much easier to put these lines of code on one line than taking up six lines of code.  Not only is it a matter of space, but  for simple variable definitions it makes it easy to have a default value.</p>
<p>Ex:<br />
$page_num = ($_REQUEST['page']) ? $_REQUEST['page'] : 1;// Default 1st page.</p>
<p>Jeremy</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob Wultsch</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23122</link>
		<dc:creator>Rob Wultsch</dc:creator>
		<pubDate>Thu, 09 Oct 2008 01:29:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23122</guid>
		<description>I have seen some horrific uses of the ternary operator. I would rather it not be used than abused.

Something I wrote on the topic:
http://wultsch.com/blog/2008/08/17/why-php-sucks/</description>
		<content:encoded><![CDATA[<p>I have seen some horrific uses of the ternary operator. I would rather it not be used than abused.</p>
<p>Something I wrote on the topic:<br />
<a href="http://wultsch.com/blog/2008/08/17/why-php-sucks/" rel="nofollow">http://wultsch.com/blog/2008/08/17/why-php-sucks/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: K Jordan</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23121</link>
		<dc:creator>K Jordan</dc:creator>
		<pubDate>Thu, 09 Oct 2008 01:21:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23121</guid>
		<description>Especially since PHP does it backwards with regards to that if you have nested ternary operators.



You could also eliminate it down to one echo and just have it evaluate which string to echo out.</description>
		<content:encoded><![CDATA[<p>Especially since PHP does it backwards with regards to that if you have nested ternary operators.</p>
<p>You could also eliminate it down to one echo and just have it evaluate which string to echo out.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johannes</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23120</link>
		<dc:creator>Johannes</dc:creator>
		<pubDate>Wed, 08 Oct 2008 23:47:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23120</guid>
		<description>Your second example won&#039;t work, echo can&#039;t be used inside an expression, youd have to use print (that&#039;s the only difference between the two btw., print always returns true and vcan be used in expressions, echo can&#039;t) or you have to write the echo outside the ternary, which i consider nicer.

echo $condition ? &quot;yes&quot; : &quot;no&quot;;

But don&#039;t abuse ternaries, I saw people widely nesting them - unreadable that is :-)</description>
		<content:encoded><![CDATA[<p>Your second example won&#8217;t work, echo can&#8217;t be used inside an expression, youd have to use print (that&#8217;s the only difference between the two btw., print always returns true and vcan be used in expressions, echo can&#8217;t) or you have to write the echo outside the ternary, which i consider nicer.</p>
<p>echo $condition ? &#8220;yes&#8221; : &#8220;no&#8221;;</p>
<p>But don&#8217;t abuse ternaries, I saw people widely nesting them &#8211; unreadable that is <img src='http://www.adamsdesk.com/be/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JulesBravo</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23118</link>
		<dc:creator>JulesBravo</dc:creator>
		<pubDate>Wed, 08 Oct 2008 22:58:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23118</guid>
		<description>echo ( $nFoo &gt; 0 ? &quot;I&#039;m at the work.&quot; : &quot;I&#039;m at home.&quot; );

I think that&#039;s better syntax.</description>
		<content:encoded><![CDATA[<p>echo ( $nFoo &gt; 0 ? &#8220;I&#8217;m at the work.&#8221; : &#8220;I&#8217;m at home.&#8221; );</p>
<p>I think that&#8217;s better syntax.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Karwin</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23117</link>
		<dc:creator>Bill Karwin</dc:creator>
		<pubDate>Wed, 08 Oct 2008 22:32:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23117</guid>
		<description>One drawback to using the ternary operator is that you now have a logic branch in a single &quot;line&quot; of code.  This makes it hard to measure code coverage from tests, for example.  It also makes diffs less clear when you commit code changes to source control.

I never use the ternary operator when each predicate is a full statement of code.  I use the ternary operator only when I need each predicate to return a value because the whole expression returns a value.

As soon as you need either predicate to be two statements, you must rewrite the expression into a full if/then/else construct anyway.

It should not be your goal to write code that has fewer lines.  It should be your goal to write code that is easier to test, easier to change, easier to document, easier to maintain.</description>
		<content:encoded><![CDATA[<p>One drawback to using the ternary operator is that you now have a logic branch in a single &#8220;line&#8221; of code.  This makes it hard to measure code coverage from tests, for example.  It also makes diffs less clear when you commit code changes to source control.</p>
<p>I never use the ternary operator when each predicate is a full statement of code.  I use the ternary operator only when I need each predicate to return a value because the whole expression returns a value.</p>
<p>As soon as you need either predicate to be two statements, you must rewrite the expression into a full if/then/else construct anyway.</p>
<p>It should not be your goal to write code that has fewer lines.  It should be your goal to write code that is easier to test, easier to change, easier to document, easier to maintain.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Izenman</title>
		<link>http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/comment-page-1/#comment-23116</link>
		<dc:creator>Joe Izenman</dc:creator>
		<pubDate>Wed, 08 Oct 2008 22:27:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/#comment-23116</guid>
		<description>In PHP you can also pull those echo statements out and have the ternary statement just return a string to a single echo, for incrementally cleaner code:

echo $nFoo &gt; 0 ? &quot;I&#039;m at the work.&quot; : &quot;I&#039;m at home.&quot;;</description>
		<content:encoded><![CDATA[<p>In PHP you can also pull those echo statements out and have the ternary statement just return a string to a single echo, for incrementally cleaner code:</p>
<p>echo $nFoo &gt; 0 ? &#8220;I&#8217;m at the work.&#8221; : &#8220;I&#8217;m at home.&#8221;;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- This Quick Cache file was built for (  www.adamsdesk.com/be/archives/2008/10/08/using-short-if-statement-in-programming/feed/ ) in 1.81074 seconds, on Feb 11th, 2012 at 9:26 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 11th, 2012 at 10:26 am UTC -->
