<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Logic &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/logic/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Four Things to Know when Writing Comments</title>
		<link>/2010/06/27/four-things-to-know-when-writing-comments/</link>
		<comments>/2010/06/27/four-things-to-know-when-writing-comments/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 09:52:39 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Comment]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Human Interest]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[Source lines of code]]></category>
		<category><![CDATA[speaker]]></category>

		<guid isPermaLink="false">/?p=1668</guid>
		<description><![CDATA[Every developer has been learned from his teachers how important is to comment his source code. You should comment the classes, the methods, the logic, etc. However nobody explained how exactly to code with comments between the lines. Have you ever seen that i++; // we increment i with one That&#8217;s a shame! This doesn&#8217;t &#8230; <a href="/2010/06/27/four-things-to-know-when-writing-comments/" class="more-link">Continue reading <span class="screen-reader-text">Four Things to Know when Writing Comments</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2010/07/12/mvc-in-practice-designing-models/" rel="bookmark" title="MVC in Practice: Designing Models">MVC in Practice: Designing Models </a></li>
<li><a href="/2010/01/09/array-hint-in-javascript/" rel="bookmark" title="Array hint in JavaScript">Array hint in JavaScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/06/how-to-write-comments.jpg"><img src="/wp-content/uploads/2010/06/how-to-write-comments.jpg" alt="How to write comments" title="how-to-write-comments" width="250" height="241" class="aligncenter size-full wp-image-1710" /></a><br />
Every developer has been learned from his teachers how important is to comment his source code. You should comment the classes, the methods, the logic, etc. However nobody explained how exactly to code with comments between the lines. Have you ever seen that</p>
<h2>i++; // we increment i with one</h2>
<p>That&#8217;s a shame! This doesn&#8217;t tell you anything, and what will happen if sometime later somebody sits in front of that code? He&#8217;d be amazed!</p>
<h2>How To</h2>
<p>In fact the task is not to determine what are the bad parts of not commenting the code, but how to overcome this. How to write comments. Here are some basic advices.</p>
<h3>1. Think about the other developers</h3>
<p>First of all the most important thing is to write comments with other programmers in mind. They should understand the logic of your code from your comments and not from the code itself.</p>
<h3>2. Write in English</h3>
<p>Even you&#8217;re not a native English speaker, and you English is not so good, it&#8217;s a must to write in English. This is really important when you work for a open source project when always there&#8217;s a big community around the project, you cannot be sure that everybody understands your language.</p>
<p>I know that this is difficult &#8211; also for me the English is not my native language, but that&#8217;s really a must. Once you start doing it, you&#8217;ll see that it&#8217;s not so difficult.</p>
<h3>3. Comment the methods as a black box</h3>
<p>Don&#8217;t write in a method comment what technique is used to achieve the result. It&#8217;s good to describe what the method does, not how it does it.</p>
<p>Bad example:</p>
<pre lang="php">
/**
 * returns $a + $b
 */
public function sum($a, $b) 
{
    return $a + $b;
}
</pre>
<p>Better:</p>
<pre lang="php">
/**
 * Returns the sum of two numbers
 * 
 * @param number $a
 * @param number $b
 * @returns number $a + $b
 */
public function sum($a, $b)
{
    return $a + $b;
}
</pre>
<h3>4. Comment the logic, not the technical solution</h3>
<p>When there&#8217;s a loop or a statement it&#8217;s useless to describe that this is a loop or statement, isn&#8217;t it? A better approach is to describe in a comment why you needed to implement this logic in such way.</p>
<p>Wrong:</p>
<pre lang="php">
...
$arr = array();
$i = 0;

// in a while loop we
// initialize every element with 0
while ($i < 100) {
    $arr[$i++] = 0; 
}
...
</pre>
<p>Better:</p>
<pre lang="php">
...
$arr = array();
$i = 0;

// we initialize an array of 0s
// to store the sorted elements
// on the first pass... etc, etc.
while ($i < 100) {
    $arr[$i++] = 0; 
}
...
</pre>
<h2>Conclusion</h2>
<p>In fact the most important thing is to write comments with other developers in mind. Tell the future developer of that code what's in the code, and pray for mercy <img src="https://s.w.org/images/core/emoji/11/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2010/07/12/mvc-in-practice-designing-models/" rel="bookmark" title="MVC in Practice: Designing Models">MVC in Practice: Designing Models </a></li>
<li><a href="/2010/01/09/array-hint-in-javascript/" rel="bookmark" title="Array hint in JavaScript">Array hint in JavaScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/06/27/four-things-to-know-when-writing-comments/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to detect a variable existence in JavaScript?</title>
		<link>/2010/02/12/how-to-detect-a-variable-existence-in-javascript/</link>
		<comments>/2010/02/12/how-to-detect-a-variable-existence-in-javascript/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 09:01:09 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Existential quantification]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[IP]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Quantification]]></category>
		<category><![CDATA[Social Issues]]></category>
		<category><![CDATA[Stoyan Stefanov]]></category>
		<category><![CDATA[Universal quantification]]></category>

		<guid isPermaLink="false">/?p=1083</guid>
		<description><![CDATA[Preface After reading one of the latest posts from Stoyan Stefanov&#8217;s blog &#8211; phpied.com I stumbled upon an interesting construction in JavaScript describing how to check an object property existence. I&#8217;m going to cover this case using as an example the console object, which comes with the Firebug enabled, and I think it may be &#8230; <a href="/2010/02/12/how-to-detect-a-variable-existence-in-javascript/" class="more-link">Continue reading <span class="screen-reader-text">How to detect a variable existence in JavaScript?</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/02/02/profiling-javascript-with-firebug-console-profile-console-time/" rel="bookmark" title="Profiling JavaScript with Firebug. console.profile() &#038; console.time()!">Profiling JavaScript with Firebug. console.profile() &#038; console.time()! </a></li>
<li><a href="/2009/07/27/jquery-debug-plugin/" rel="bookmark" title="jQuery debug plugin">jQuery debug plugin </a></li>
<li><a href="/2010/02/03/firebugs-console-profile-vs-console-time/" rel="bookmark" title="Firebug&#8217;s console.profile vs console.time">Firebug&#8217;s console.profile vs console.time </a></li>
<li><a href="/2009/07/04/jquery-check-for-element-existence-in-the-dom/" rel="bookmark" title="jquery check for element existence in the DOM">jquery check for element existence in the DOM </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Preface</h2>
<p>After reading one of the latest posts from Stoyan Stefanov&#8217;s blog &#8211; <a title="Stoyan Stefanov's blog" href="http://phpied.com/" target="_blank">phpied.com</a> I stumbled upon an interesting construction in JavaScript describing how to check an object property existence. I&#8217;m going to cover this case using as an example the console object, which comes with the Firebug enabled, and I think it may be very useful, just because this object is widely used, but doesn&#8217;t exists over all the browsers, even under Firefox when Firebug is disabled. This construction is known as the IN statement and it looks something like that:</p>
<pre lang="javascript">property in object
</pre>
<p>which I&#8217;m going to cover later in details.</p>
<h2>How you detect a variable existence?</h2>
<p>There are three main ways to detect the existence of a property I see in my practice.</p>
<p>1. The most widely used:</p>
<pre lang="javascript">if ( console ) { ... }</pre>
<p>2. The second and more professional one:</p>
<pre lang="javascript">if ( typeof console != 'undefined' ) { ... }</pre>
<p>3. And finally the third and most unknown:</p>
<pre lang="javascript">if ( console in window ) { ... }</pre>
<p>Of course two of them are wrong! Which one should be changed?</p>
<h2>Is everything working correctly? No!</h2>
<p>Lets see how they are working. First of all I&#8217;m going to test all of them on Firefox 3.6 either with enabled and disabled Firebug, just shouting with old school&#8217;s<strong> alert().</strong></p>
<p>Now the first one using the IN statement:</p>
<pre lang="javascript">if ( console in window )
   alert('object exists!');
else
   alert('object doesn\'t exists!');
</pre>
<p>With Firebug disabled the answer is &#8230; nothing! No alert! That&#8217;s because the syntax is wrong. There&#8217;s an error within the IF statement. We should modify a bit the first row like that:</p>
<pre lang="javascript">if ( 'console' in window )
   alert('object exists!');
else
   alert('object doesn\'t exists!');
</pre>
<p>Now everything&#8217;s OK. With a disabled Firebug the answer is: &#8220;object doesn&#8217;t exists!&#8221;, and after enabling it, normally the &#8220;object exists!&#8221;.</p>
<p>Lets move on the next example:</p>
<pre lang="javascript">if ( console )
   alert('object exists!');
else
   alert('object doesn\'t exists!');
</pre>
<p>With Firebug enabled the answer is as we expect: &#8220;object exists!&#8221;, but after disabling it yet again &#8211; nothing?! Why&#8217;s that? Because the console object doesn&#8217;t exists anymore and the IF statement doesn&#8217;t return true or false, but simply crashes. How to modify the code? Simply by adding a window.console instead of console.</p>
<pre lang="javascript">if ( window.console )
   alert('object exists!');
else
   alert('object doesn\'t exists!');
</pre>
<p>Than the answer is: &#8220;the object doesn&#8217;t exists!&#8221; after what we expected!</p>
<p>The third method is the mostly used, just because it&#8217;s completely clear what&#8217;s the goal of the IF statement:</p>
<pre lang="javascript">if ( typeof console != 'undefined' )
   alert('object exists!');
else
   alert('object doesn\'t exists!');
</pre>
<p>In both disabled and enabled Firebug the answer is as expected!</p>
<p>Now the IN statement may be used if not for the console, but for checking the existence of a property of within an object.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/02/02/profiling-javascript-with-firebug-console-profile-console-time/" rel="bookmark" title="Profiling JavaScript with Firebug. console.profile() &#038; console.time()!">Profiling JavaScript with Firebug. console.profile() &#038; console.time()! </a></li>
<li><a href="/2009/07/27/jquery-debug-plugin/" rel="bookmark" title="jQuery debug plugin">jQuery debug plugin </a></li>
<li><a href="/2010/02/03/firebugs-console-profile-vs-console-time/" rel="bookmark" title="Firebug&#8217;s console.profile vs console.time">Firebug&#8217;s console.profile vs console.time </a></li>
<li><a href="/2009/07/04/jquery-check-for-element-existence-in-the-dom/" rel="bookmark" title="jquery check for element existence in the DOM">jquery check for element existence in the DOM </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/02/12/how-to-detect-a-variable-existence-in-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
