<?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>iPhoneDevelopmentBits &#187; shihab</title>
	<atom:link href="http://iphonedevelopmentbits.com/author/shihab/feed" rel="self" type="application/rss+xml" />
	<link>http://iphonedevelopmentbits.com</link>
	<description>iPhone Development &#124; iPhone Programming &#124; iPhone Application Development &#124; iPhone Game Development &#124; iPhone App Development &#124; iPhone Software Development</description>
	<lastBuildDate>Mon, 18 Jul 2011 06:04:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Good programming practices ( Object Oriented / C++)</title>
		<link>http://iphonedevelopmentbits.com/good-programming-practices-object-oriented-c</link>
		<comments>http://iphonedevelopmentbits.com/good-programming-practices-object-oriented-c#comments</comments>
		<pubDate>Fri, 30 Jan 2009 18:40:36 +0000</pubDate>
		<dc:creator>shihab</dc:creator>
				<category><![CDATA[iPhone Games]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[good programming]]></category>
		<category><![CDATA[object oriented]]></category>

		<guid isPermaLink="false">http://iphonedevelopmentbits.com/?p=415</guid>
		<description><![CDATA[<p> Some of the best practices given below.</p>
<div>1. Prefer inline function over macros, bcz macro willl bring unwanted errors.</div>
<div>eg: See the example of a macro to find maximum of two numbers</div>
<div> </div>
<div> #define MAX(a,b) (a &#62; b) ? a</div><p>&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p> Some of the best practices given below.</p>
<div>1. Prefer inline function over macros, bcz macro willl bring unwanted errors.</div>
<div>eg: See the example of a macro to find maximum of two numbers</div>
<div> </div>
<div> #define MAX(a,b) (a &gt; b) ? a : b</div>
<div> suppose if we used the macro as follows</div>
<div>  int x = 20, y = 19;</div>
<div>  int z =  MAX(x, ++y);</div>
<div> </div>
<div> now what is the value of z ?  is it 20 ? or  is it 19 ? .</div>
<div>  .. he he &#8230;it is 21 !</div>
<div>Let us see , how it is happened, </div>
<div><span> </span>Compiler will compile the code after macro expansion .. after macro expansion , the code will be as follows</div>
<div><span> </span>int x = 20, y = 19; </div>
<div><span> </span>int z = (x &gt; ++y) ? x: ++y;</div>
<div>y has been incremented twice .. !</div>
<div>What is the solution:-</div>
<div><span> </span>Use inline function&#8230; it will not produce unwanted errors &#8230; and high fast like macros bcz there is no function calling burdens </div>
<div><span> </span>OR </div>
<div><span> </span>Avoid complex expression when using macros&#8230; ie we can call the MAX as follows</div>
<div>
<div>
<div> <span> </span> int x = 20, y = 19;</div>
<div><span> </span>++y;</div>
<div> <span> </span> int z =  MAX(x, y);</div>
</div>
</div>
<div>2. Use call by reference other than call by value for big user defined data types.</div>
<div><span> </span>//suppose we are having a class MyClass  of size 100 bytes;</div>
<div><span> </span>void foo(MyClass m) {</div>
<div><span> </span>//some code</div>
<div><span> </span>}</div>
<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Instead use as follows &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</div>
<div><span> </span> </div>
<div><span> </span>void foo(const MyClass&amp; m) {</div>
<div><span> </span>//some code</div>
<div><span> </span>}</div>
<div>Explanation:</div>
<div><span> </span>In both case we can call the method as follows</div>
<div><span> </span></div>
<div><span> </span>MyClass mc;</div>
<div><span> </span>foo(mc);</div>
<div><span> </span>In the first case a value of MyClass object, mc is copied to another object m, of MyClass.</div>
<div>Here Object of the MyClass has been created twice ie total 200 bytes used .. </div>
<div>also it will bring extra processing to initialize a new object.</div>
<div><span> </span>In the second case , MyClass object is created only once then the alias of mc is created as m</div>
<div>there is no burden of new object initialization and processing.</div>
<div><span> </span>The &#8216;const&#8217; is used for two reason&#8230;</div>
<div><span> </span> a) To avoid the misunderstanding that it is an output argument.</div>
<div><span> </span> b) To avoid the accidental changes to the outside object from the function.</div>
<div>Note:- In C there is no call by reference but we are simulating call by reference </div>
<div>by passing address to the function argument and modifying the content of the address</div>
<div>from the function</div>
<div>3. Avoid unwanted file inclusion and prefer forward declaration.</div>
<div><span> </span>Suppose Foo is a class and its definition is in Foo.h.</div>
<div>We are using the class Foo in the File MyClass.h as follows.</div>
<div>//MyClass.h</div>
<div>#include &#8220;Foo.h&#8221;</div>
<div>class MyClass {</div>
<div><span> </span>Foo * aFoo;</div>
<div><span> </span></div>
<div><span> </span>//Some methods</div>
<div>}</div>
<div>&#8212;&#8212;&#8212;&#8212; Instead use as follows &#8212;&#8212;&#8212;&#8212;&#8211;</div>
<div>
<div>//MyClass.h</div>
<div>class Foo; // forward declaration.</div>
<div>class MyClass {</div>
<div><span> </span>Foo * aFoo;</div>
<div><span> </span></div>
<div><span> </span>//Some methods</div>
<div>}</div>
</div>
<div>Explanation: -</div>
<div><span> </span>In the first case, when including Foo.h , the entire code will be pasted in MyClass.h, it will</div>
<div>increase the size of executable and will creep in some unwanted errors.</div>
<div><span> </span>We are not instantiating, or using the functionality of  Foo, so the compiler should not bother about the size</div>
<div>of the class Foo ..it wants to know only what is Foo, we have forward declared it as &#8216;Class&#8217;.</div>
<div>Also aFoo is a pointer, the size of any pointer is the size of an integer.</div>
<div>so the compiler can allocate the size of pointer and it will not produce any error message.</div>
<div><span> </span></div>
<div>4. Use header guard to avoid multiple inclusion of a file.</div>
]]></content:encoded>
			<wfw:commentRss>http://iphonedevelopmentbits.com/good-programming-practices-object-oriented-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

