<?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>Mahbubur Rahman - jQuery, PHP, JavaScript, Codeigniter, CSS &#187; meaningful variable</title>
	<atom:link href="http://www.mahbubblog.com/tag/meaningful-variable/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mahbubblog.com</link>
	<description>PHP, JQUERY, JAVASCRIPT fun</description>
	<lastBuildDate>Thu, 03 Jun 2010 07:23:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Always use meaningful variable and function names in PHP</title>
		<link>http://www.mahbubblog.com/php/always-use-meaningful-variable-and-function-names-in-php/</link>
		<comments>http://www.mahbubblog.com/php/always-use-meaningful-variable-and-function-names-in-php/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 09:00:28 +0000</pubDate>
		<dc:creator>Mahbub</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[convention]]></category>
		<category><![CDATA[meaningful variable]]></category>

		<guid isPermaLink="false">http://www.mahbubblog.com/?p=34</guid>
		<description><![CDATA[Think About the following code $a=&#8221;I Am John Doe&#8221;; $b=explode(&#8220; &#8220;,$a); echo &#8220;Total Number of words in $a is &#8220;.count($b); Now how about this, $name=&#8221;I Am John Doe&#8221;; $num_of_words=explode(&#8220; &#8220;,$a); echo &#8220;Total Number of words in $name is &#8220;.count($num_of_words); Well, the example was too simple. But things can go terribly wrong when not using meaningful [...]]]></description>
			<content:encoded><![CDATA[<p>Think About the following code  <span style="font-family: Courier New;">$a=&rdquo;I Am John Doe&rdquo;;</span><br style="font-family: Courier New;" /><br />
<span style="font-family: Courier New;">$b=explode(&ldquo; &ldquo;,$a);</span><br style="font-family: Courier New;" /><br />
<span style="font-family: Courier New;">echo &ldquo;Total Number of words in $a is &ldquo;.count($b);</span><br style="font-family: Courier New;" /><br />
Now how about this,  <span style="font-family: Courier New;">$name=&rdquo;I Am John Doe&rdquo;;</span><br style="font-family: Courier New;" /><br />
<span style="font-family: Courier New;">$num_of_words=explode(&ldquo; &ldquo;,$a);</span><br style="font-family: Courier New;" /><br />
<span style="font-family: Courier New;">echo &ldquo;Total Number of words in $name is &ldquo;.count($num_of_words);</span>  Well, the example was too simple. But things can go terribly wrong when not using meaningful variable names in PHP or any other language&#8217;s codes. Even you&#8217;ll forget at some point of time what was the variable $a for and $b. And it really pisses other developers when using some one&#8217;s code which didn&#8217;t follow good naming convention. And when i see codes in those messy fashion it makes me angry like the Goku below.<br />
<a href="http://www.mahbubblog.com/wp-content/uploads/2009/03/angry_goku_by_psycho_slayer.jpg"><img height="311" width="300" src="http://www.mahbubblog.com/wp-content/uploads/2009/03/angry_goku_by_psycho_slayer.jpg" alt="Angry Goku" title="angry_goku_by_psycho_slayer" class="size-full wp-image-66" /></a></p>
<p>And the end result is inefficiency and waste of valuable time.  There are various naming conventions practiced for coding. PHP has it&#8217;s own style where underscore is massively used. And it&#8217;s quite comfortable to use. $last_name is more readable than $lastname. So variables/functions can be made quite meaningful when they are written. To cut the story short, here&#8217;s some suggestions from me:&nbsp;</p>
<p><span id="more-34"></span></p>
<ul>
<li><span style="font-weight: bold;">Always use lower case in functions and variables unless specifically asked by Frameworks. Like</span> <span style="font-family: Courier New;">function Login() </span><br style="font-family: Courier New;" /><br />
    <span style="font-family: Courier New;"> function login()</span></li>
</ul>
<ul>
<li><span style="font-weight: bold;">Functions/Variable separators should be with underscores. e.g </span> <span style="font-family: Courier New;">function get_users()..</span><br style="font-family: Courier New;" /><br />
    <span style="font-family: Courier New;">function getUsers() // It&rsquo;s better to avoid Camelize </span></li>
</ul>
<ul>
<li><span style="font-weight: bold;">Function names should be grammatically sensible. For example if we want a function that returns all the countries of the world, we should name it like</span> <span style="font-family: Courier New;">function get_all_countries().</span><br style="font-family: Courier New;" /><br />
    <span style="font-family: Courier New;">function get_all_country()..&nbsp;// wrong plural </span><br style="font-family: Courier New;" /><br />
    <span style="font-family: Courier New;">function get_country_all()..&nbsp;// Hard to make sense what this means</span></li>
<li style="font-weight: bold;">Be consistent with one practice throughout the whole project so that other people can confortably assume the convention.</li>
<li style="font-weight: bold;">If possible keep signature of datatypes or return types in variable function name. Like if we want to store users as an array in a variable it should be $user_array instead of $users</li>
<li><span style="font-weight: bold;">Using imperatives. Meaning put some verbs in functions to know what the function is doing. Instead of function notes(), try to write function get_notes(); which is kind of self explaining.</span></li>
<li style="font-weight: bold;">Avoid homophones: e.g., foo, fu, phoo, etc.</li>
<li style="font-weight: bold;">Avoid generic names such as tmp, buf, reg.</li>
<li style="font-weight: bold;">Avoid intentionally misspelled words such as lo or lite.</li>
</ul>
<p>Well, there can be many more suggestions for better coding, because &quot;coding is poetry&quot; for programmers.</p>
<div class="zemanta-pixie"><img alt="" src="http://img.zemanta.com/pixy.gif?x-id=34c7b094-453a-4086-a72d-3e2c6d45ad60" class="zemanta-pixie-img" /></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mahbubblog.com/php/always-use-meaningful-variable-and-function-names-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
