<?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:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Strides Interactive</title>
	<atom:link href="http://www.stridesdev.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stridesdev.org</link>
	<description></description>
	<pubDate>Tue, 02 Aug 2011 00:20:19 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Memory leaks beyond memory leaks</title>
		<link>http://www.stridesdev.org/2011/08/memory-leaks-beyond-memory-leaks/</link>
		<comments>http://www.stridesdev.org/2011/08/memory-leaks-beyond-memory-leaks/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 00:20:19 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
		
		<category><![CDATA[Weekly Tuesday Dose of goodness]]></category>

		<guid isPermaLink="false">http://www.stridesdev.org/?p=1078</guid>
		<description><![CDATA[Hi all,
Finally! I have some time to post something new here. As time goes by, the challenges I face gets tougher by the day. Remember some time ago I talked about memory leaks that occurs due to design flaws and abuse despite having a good high-level memory management pointer container?
Well, my talk has just caught [...]]]></description>
			<content:encoded><![CDATA[<p>Hi all,</p>
<p>Finally! I have some time to post something new here. As time goes by, the challenges I face gets tougher by the day. Remember some time ago I talked about memory leaks that occurs due to design flaws and abuse despite having a good high-level memory management pointer container?</p>
<p>Well, my talk has just caught up with me once more. It seems that the simple case of <strong>allocator race leak</strong> is no longer that simple. Usually, we&#8217;re always on the illusion that the memory we allocate is the same memory that is supposedly deallocated <strong>immediately</strong> (alike the allocator when sourcing out for RAM).</p>
<p>But no. This is indeed not the case.</p>
<p>Read on to find out why&#8230;<br />
<span id="more-1078"></span></p>
<h2>Introduction</h2>
<p>First of all, this post should be regarded more like a speculation rather than a fact sheet of how things work. The reason is because some of the contents requires proper understanding of the host OS which I admit that I only have limited knowledge.</p>
<p>Secondly, different OS have different means of handling memory. The allocator we&#8217;re talking about in this article is also based on the de-facto compiler. What I&#8217;m saying is, for Windows, we&#8217;ll be referring to a Visual Studio compiler and CRT. For Linux, one can assume GCC and its CRT to be the one.</p>
<h2>High-level memory management vs Low-level memory management</h2>
<p>When we talk about high level and low level, it&#8217;s usually based on the level of control you have on your memory. One can argue that he/she have absolute control over his/her memory. Granted. Only if the number of allocations are minimal and each allocation is large.</p>
<p>A low-level memory management is like managing which block of memory is to be allocated for the requested size. Managing memory to such details are usually for platform-level languages such as Java and her JVM. Have you ever wondered why the JVM takes up a <em>contiguous amount</em> of memory and this amount is usually in large amounts, such as 256MB onwards?</p>
<p>A high-level memory management is like <strong>cSmartPtr&lt;T&gt;</strong> where its management is dictated by technical design of the object. It doesn&#8217;t care how the memory is allocated or when the memory is deallocated from the OS. It only knows when to <strong>delete</strong> an object when the time comes for it to be destroyed.</p>
<p>Deleting an object <strong>doesn&#8217;t necessarily guarantee a </strong>direct refund of memory. This is especially so for <strong>debug</strong> <strong>CRTs</strong> as compared to release CRTs.</p>
<p>This article&#8217;s topic isn&#8217;t on the difference between debug and release CRTs. But one notable difference is that, debug CRTs usually take up more memory like a glutton compared to release CRTs.</p>
<h2>Memory Paging</h2>
<p>In Windows, from my limited knowledge, memory paging is used. The exact size of each page is unknown to me, but my guess is around 4k bytes per page.</p>
<p>My second guess in this article is that - the <strong>allocator cannot get a memory refund</strong> unless <strong>all the allocations on the page is deleted</strong>. Thus, if there&#8217;re 100x 4k pages and that these pages only have 1x 64 bytes of allocation, then for 64k worth of memory objects, 400K of RAM is used up instead. This is indeed memory fragmentation - fragmentation beyond what high level management can do (unless the user creates an array and manages it manually&#8230;)</p>
<p>Thus, until there&#8217;s a customized solution for game-based applications, who has high volume<em> (rate of construction/destruction per second)</em> of object construction and destruction, the amount of memory taken up will increase gradually no matter how leak-free your application might be.</p>
<h2>Possible Solutions</h2>
<p>There&#8217;re several solutions which are of a similar nature that can help reduce or eliminate the problem completely. I&#8217;ll list them in order of complexity (I hope I&#8217;m right)&#8230;</p>
<h3>1. Use limited managed arrays</h3>
<p>Although arrays are known to be evil, sometimes if properly employed, can be a powerful ally. I mentioned the use of <strong>managed</strong> - that&#8217;d mean arrays represented by a proper data structure such as <strong>std :: vector&lt;T&gt;</strong> or <strong>cArray&lt;T&gt;</strong>.</p>
<p>The crux of this solution is to design the application in such a way that certain objects have a limitation in numbers. Such as projectiles limits, particle (dust, sparks, fire, etc) limits, and other object limits. Different games require different limits, some prohibit limitations due to the way the game is meant to be played. Either way, someone has to make the decision.</p>
<p>The disadvantage of employing this solution is limitation and increases processing load. It&#8217;s difficult to calculate which of the array slots will be available during runtime and thus the algorithm will usually place the game object in the next available slot and goes back to index 0 to start searching for one when the index has come to an end.</p>
<p>The search for an available slot is timed at <strong>O ( n )</strong> though in my personal opinion, it should be somewhat lesser.</p>
<p>On top of that, the execution update loop will need to iterate <strong>all</strong> slots to see if they&#8217;re NULL or not and then process those that are not NULL.</p>
<h3>2. Use a large memory buffer and manage manually</h3>
<p>This is a significantly more complex solution which requires a high-level allocator to search, allocate, release and maintain a heap. Thus one requires a heap data structure to properly manage the user-defined memory.</p>
<p>The crux of this approach is to <strong>reduce</strong> the number of default allocation calls. This in turn will gradually reduce the amount of memory page fragmentation and thus the memory leak itself.</p>
<p>The disadvantages of this approach is that the user have to manually manage the memory buffer. The amount of effort for coding this management system; which may not be reusable or transferable easily without massive recoding, makes it highly undesirable.</p>
<h3>3. Implement your own garbage collector / memory allocator</h3>
<p>The 4 major C++ operators <strong>new, new []</strong>, <strong>delete</strong> and <strong>delete []</strong> must be overridden. This applies to <strong>all</strong> objects, thus <strong>templates</strong> shouldn&#8217;t be in the design considerations. That&#8217;s the first step.</p>
<p>Next, the internal allocation mechanism must allocate an <strong>page aligned</strong> memory buffer that is large enough by <em><strong>estimation</strong></em> to accommodate the user&#8217;s requests. On top of that, the system must also <strong>expand</strong> as necessary.</p>
<p>The way it sounds, it certainly sounds like a garbage collector isn&#8217;t it? Well, it certainly is - with the exception that your low level mechanism doesn&#8217;t include memory housekeeping. This should be done by a smart pointer container such as <strong>cSmartPtr&lt;T&gt;</strong>.</p>
<p>The crux is to ensure that memory management is done from bottom-up and top-down as well. Since the memory is pre-allocated, the number of default allocations are minimized. In fact, a well-designed application can even eliminate such memory leaks altogether.</p>
<p>This is certainly the best solution since it doesn&#8217;t require any major recoding in order for it to be reused.</p>
<p>Disadvantages? Well, the system is notoriously hard to implement and may take a long time to validate itself. <strong>Kudos to those who wrote the Java language. You guys really know your stuffs. </strong>Too bad, many of your subscribers are too stuck up to see what you&#8217;ve done for them.</p>
<h2>Conclusion</h2>
<p>This episode that I&#8217;ve encountered taught me an important lesson. That is - never to take memory allocation for granted. Understanding the OS is just but a first step to understanding how its memory allocations are done.</p>
<p>A good game developer will at least understand this and know the limitations and thus will not design something that the system cannot handle.</p>
<p>At this stage, I believe this is the end of the spectrum for memory leaks. This topic has been quite a hot topic on stridesdev.org. I hope that this article can raise the awareness of memory allocations among the community. Don&#8217;t take it for granted.</p>
<p>Do feel free to correct me should you feel that my guesses are wrong. I&#8217;ll be more than willing to acknowledge my mistakes and post them here.</p>
<p>Signing off,<br />
Jeremy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stridesdev.org/2011/08/memory-leaks-beyond-memory-leaks/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Retreat Notice</title>
		<link>http://www.stridesdev.org/2011/04/retreat-notice/</link>
		<comments>http://www.stridesdev.org/2011/04/retreat-notice/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 01:41:26 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.stridesdev.org/2011/04/retreat-notice/</guid>
		<description><![CDATA[Dear all,
I&#8217;ve gone into a retreat within the deepest recessions of creativity. Thus I will not be able to make posts for the next few months unfortunately.
However, during this period, I&#8217;ll try to eek out some time for a post if I can help it.
Something&#8217;s big is coming up.
All shall be revealed by the end [...]]]></description>
			<content:encoded><![CDATA[<p>Dear all,</p>
<p>I&#8217;ve gone into a retreat within the deepest recessions of creativity. Thus I will not be able to make posts for the next few months unfortunately.</p>
<p>However, during this period, I&#8217;ll try to eek out some time for a post if I can help it.</p>
<p>Something&#8217;s big is coming up.</p>
<p>All shall be revealed by the end of June 2011.</p>
<p>Regards,<br />
Jeremy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stridesdev.org/2011/04/retreat-notice/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Software Quality 14 - OoI and OoD</title>
		<link>http://www.stridesdev.org/2011/04/software-quality-14-ooi-and-ood/</link>
		<comments>http://www.stridesdev.org/2011/04/software-quality-14-ooi-and-ood/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 06:36:04 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
		
		<category><![CDATA[Weekly Tuesday Dose of goodness]]></category>

		<guid isPermaLink="false">http://www.stridesdev.org/?p=1071</guid>
		<description><![CDATA[This entry is part 14 of 14 in the series Software Quality SeriesThis week, we&#8217;re going to talk about OoI and OoD.
Don&#8217;t get me wrong, they&#8217;re not meant to be read literally.
OoI stands for Order of Initialization
OoD stands for Order of Destruction
The former applies to all programming language with object (or memory buffer) constructs while [...]]]></description>
			<content:encoded><![CDATA[<div class="seriesmeta">This entry is part 14 of 14 in the series <a href="http://www.stridesdev.org/series/software-quality-series/" title="series-31">Software Quality Series</a></div><p>This week, we&#8217;re going to talk about OoI and OoD.</p>
<p>Don&#8217;t get me wrong, they&#8217;re not meant to be read literally.</p>
<p><strong>OoI </strong>stands for Order of Initialization</p>
<p><strong>OoD</strong> stands for Order of Destruction</p>
<p>The former applies to all programming language with object (or memory buffer) constructs while the latter only apply to C/C++ only. Or does it?</p>
<p>So what&#8217;s so problematic about them that leads to software quality issues?</p>
<p>Read on&#8230;<span id="more-1071"></span></p>
<h2>Introduction</h2>
<p>Programmers usually have the tendency to take certain things for granted. One of the many things is the order of initialization(<em>construction</em>) and destruction.</p>
<p>Naturally some might argue that it&#8217;s not truth. Fine. However, it&#8217;s hard to say that programmers don&#8217;t take other people&#8217;s order of constructs seriously.</p>
<p>When this happens, the effects are obvious. Not only objects may not be initialized properly, it may even cause memory leaks in languages that have garbage collectors. This is especially so if one&#8217;s codes have a <strong>A-&gt;B </strong>and <strong>B-&gt;A </strong>relationship. This is possible in C++ using forward declarations and is also possible in other languages just as Java or C#.</p>
<h2>Problems</h2>
<p>How does it happen? <strong>A </strong>has <strong>B</strong> - B is the full implementation, <em><strong>child class.</strong></em> A is the class holding B. Thus it can be said that <strong>A</strong> is <em>composed of</em> <strong>B</strong> and other objects.</p>
<p>Now, let&#8217;s look at B. If B requires a parameter, ie, the parent of <strong>A</strong>. In this case, in Java or C# it can be the base object class. So if the initialization is like: <em>(In Java)</em></p>
<pre>public class A extends ParentObject {
    private B bObj;

    //constructor
    public A() {
        this.bObj = new B(this);
    }
};

public class B {
    private ParentObject pObj;
    //constructor
    public B(ParentObject obj) {
        this.pObj = obj;
    }
};</pre>
<p>At  this point, you&#8217;re technically cross-referencing both objects. I&#8217;m not 100% sure on how Java garbage collector works but if it&#8217;s just a simple reference counting pointer, then there&#8217;ll a memory leak still.</p>
<p>Why? Because the instance of <strong>A</strong> will be lost in its composed object, <strong>bObj</strong>. Even if <strong>A </strong>is set to <strong>null</strong>, it won&#8217;t be able to garbage collect since <strong>B </strong>is holding another reference to <strong>A</strong>. Remember, if <strong>A </strong>extends <strong>ParentObject</strong>, then <strong>A</strong> IS <strong>ParentObject</strong>! There&#8217;s no difference in terms of base memory address.</p>
<p>In this scenario, unless the garbage collector has additional information to decide that <strong>bObj</strong> is useless as well, otherwise it cannot, by the designs of reference counting pointer mechanisms, release the instance of A. Thus causing a memory leak.</p>
<h2>Problems In C++</h2>
<p>Order of destruction problems are compounded in C++ by the fact that you can arbitrarily delete objects at any time. Thus even with reference counting pointers, programmers cannot escape order of destruction problems.</p>
<p>The main reason is the order itself. A lot of programmers like to delegate the order of destruction to the garbage collector, in C++, it&#8217;ll be the reference counting pointer container to handle.</p>
<p>The over-reliance on this mechanism easily leads to crashes that doesn&#8217;t make a lot of sense. That&#8217;s because we&#8217;re taught to understand reference counting pointer that there&#8217;s only valid or null pointers. Nothing else.</p>
<p>Messing up the order of destruction will overturn this teaching.</p>
<p>Reason? Well, the problems generated by the order of destruction isn&#8217;t about null pointer dereferencing (assuming you&#8217;ve done your null checks properly). The biggest problem is generated by <strong>dangling pointers</strong> where certain modules are released already without waiting for the reference counting pointer.</p>
<p>How is this possible?</p>
<p>The reference counting pointer can only cover as far as it could based on allocator and deallocator.  It doesn&#8217;t cover things like when a DLL is detached, for example, how can you ensure that every reference counting container is set to NULL?</p>
<p>You can&#8217;t.</p>
<p>Thus the only way to prevent this without using the full version of Strides Interactive&#8217;s <strong>cSmartPtr&lt;T&gt;</strong>, is to ensure that the order of destruction is done properly. This require proper <span style="text-decoration: underline;">design adherence</span> and <span style="text-decoration: underline;">design declaration</span> in the early stages of the project. I&#8217;ll not go into details for these 2 terms just yet.</p>
<p>So, how is it so that Strides&#8217; smart pointer can handle what normal reference counting pointer can&#8217;t?</p>
<p>Well, it allows you to terminate an object and ensures that everybody else returns a NULL when checked against.</p>
<h2>Order of Initialization Problems</h2>
<p>So we&#8217;ve covered order of destruction problems. How about order of initialization? Surely there isn&#8217;t any wrong with allocating objects right? Does memory limitations play a part?</p>
<p>Well - there&#8217;s nothing wrong allocating objects and memory limitations do play a part but in terms of the order, it doesn&#8217;t play a major role unless the case is very specific.</p>
<p>We all know that all applications require a specific <strong>sequence</strong> of initialization. This sequence actually has an underlying order which is categorized accordingly to certain application stages.</p>
<p>Here&#8217;s an example of an order of initialization of a typical game:</p>
<p>1) Initialize Windows API to create a form<br />
2) Initialize all devices (renderer, audio, input, etc)<br />
3) Initialize game engine<br />
4) Initialize script engine<br />
5) Initialize game</p>
<p>Usually the first 4 steps are done for you if you use a 3rd party engine. Thus leaving the order of initializing the game to the programmer.</p>
<p>So what happens in a game order of initialization? Let&#8217;s look at this example:</p>
<p>1) Load the textures<br />
2) Load the meshes<br />
3) Create the particle samples<br />
4) Create the particle emitters<br />
5) Load data<br />
6) Create entities<br />
7) Create user interface to expose entity properties</p>
<p>If you notice, there&#8217;s a certain dependency in the order of initialization. If this dependency is broken, it&#8217;ll usually lead to broken implementations and functionality. Thus, it&#8217;s important to understand this order and respect the dependencies.</p>
<p>The keyword is <strong>respect.</strong> That means, to take it really seriously, consult with the technical design and ask the right questions.</p>
<h2>Self-awareness</h2>
<p>It&#8217;s also important to understand your own module and be aware of what&#8217;s required for your codes to function properly. With that, you&#8217;ll better understand where and when your codes can be initialized.</p>
<p>Sometimes, it&#8217;s not a matter with the codes, but rather, a problem with the order of scripting.</p>
<p><em>&#8220;How can a particle be created without a texture?&#8221;</em> . These type of questions should be asked. If there&#8217;s no answer, then there can only be  the following possibilities:</p>
<p>1) Your module is not applicable to the project at all<br />
2) Your module is completed way too early for any integration to be possible<br />
3) The technical design of the module isn&#8217;t clear enough<br />
4) Project management isn&#8217;t good enough to optimize your time and skills</p>
<h2>Conclusion</h2>
<p>Thus, as I sign off, I&#8217;d like to stress the importance of the Order of Initialization and the Order of Destruction. You&#8217;ll need to seriously respect these 2 nature of things in order for your project to be better off.</p>
<p>Why do I say it&#8217;s nature of things rather than nature of programming? That&#8217;s because, we&#8217;re sub-consciously doing it already. We cannot expect things to happen just like that. There&#8217;s a natural progression and a sequence of events that will lead to it.</p>
<p>Normally, we&#8217;ll even plan that sequence and hope that things will go our way. And most of the time, it doesn&#8217;t go our way, leading to exceptions.</p>
<p>So if we want to go toilet, we&#8217;ll first need to walk to it right? Sounds simple? Yes.</p>
<p>Taken for granted? Yes, unfortunately. It may not be just this case, but many other cases. For example, when you order food, do you expect it to come in a timely fashion? Yes you do. But over time, we take it for granted.</p>
<p>When it comes to programming, there&#8217;ll be no one to cover us taking for granted and such problems may not be easily traceable as it seems to be. However, once you&#8217;ve traced it - there&#8217;ll be at least 2 outcomes.</p>
<p>1) You get confused by the design of the flow and head back to the docs for an answer<br />
2) You get demoralized because this is a technically very simple (and possibly stupid) mistake.</p>
<p>Thus, please - have respect for nature and you should be just fine.</p>
<p>Signing off for now. Have a great week ahead!</p>
<p>Regards,<br />
Jeremy</p>
<h2></h2>
]]></content:encoded>
			<wfw:commentRss>http://www.stridesdev.org/2011/04/software-quality-14-ooi-and-ood/feed/</wfw:commentRss>
	
		<series:name><![CDATA[Software Quality Series]]></series:name>
	</item>
		<item>
		<title>Software Quality - Part 13 (Backtracking time?)</title>
		<link>http://www.stridesdev.org/2011/04/software-quality-part-13-backtracking-time/</link>
		<comments>http://www.stridesdev.org/2011/04/software-quality-part-13-backtracking-time/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 16:13:50 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
		
		<category><![CDATA[Weekly Tuesday Dose of goodness]]></category>

		<guid isPermaLink="false">http://www.stridesdev.org/?p=1069</guid>
		<description><![CDATA[This entry is part 13 of 14 in the series Software Quality SeriesDear all,
It has been quite some time since I&#8217;ve posted. And I must say, I&#8217;ve been thinking through about the next set of software quality parts and I do not feel very comfortable posting them here. The primary reason is due to the [...]]]></description>
			<content:encoded><![CDATA[<div class="seriesmeta">This entry is part 13 of 14 in the series <a href="http://www.stridesdev.org/series/software-quality-series/" title="series-31">Software Quality Series</a></div><p>Dear all,</p>
<p>It has been quite some time since I&#8217;ve posted. And I must say, I&#8217;ve been thinking through about the next set of software quality parts and I do not feel very comfortable posting them here. The primary reason is due to the fact that those articles are highly subjective and debatable. It&#8217;s unlike the first 12 parts where all of the components described are actually in practice.</p>
<p>Thus for the next few weeks, since my 2nd set of software quality isn&#8217;t ready for release, I&#8217;ll try my best to share my other experiences too.</p>
<p>This week, we&#8217;ll talk about a small change that can lead to a very irritating development cycle. So what&#8217;s that?</p>
<p>Read on&#8230;<span id="more-1069"></span></p>
<h2>Introduction</h2>
<p>Many a times when we&#8217;re coding using 3rd party libraries, sometimes these assholes actually have a time limit as to when you can or cannot use it.</p>
<p>Thus once this time limit is reached, your application grinds to a halt.</p>
<p>So what&#8217;s next? Well&#8230;. either you get the full version or you try to set the date back to &#8220;give&#8221; yourself more time. Well-protected shareware libraries will tend to guard against time back tracking to prevent such scenarios from happening.</p>
<p>However, others may be kind enough to allow you to do that.</p>
<p>So you&#8217;ve managed to change the time. The project builds fine and it runs alongside the 3rd party as though as it&#8217;s still under evaluation. What&#8217;s the problem then?</p>
<h2>Problem</h2>
<p>The bloody problem occurs when you back track time. In IDE (Integrated Development Environment) such as Visual Studio, it&#8217;ll be kind to check your object files as well as your executable to see if they need to be rebuild. This is also known as the &#8220;up-to-date&#8221; checker.</p>
<p>So what happens when you try to build again? The IDE will rebuild virtually, your entire project. Don&#8217;t think it&#8217;s trivial. In a standard project size, around 300,000 lines of codes, this can be a nightmare scenario.</p>
<p>You&#8217;ll start to wonder what on Earth have your IDE gone wrong and probably the first thing you&#8217;ll do is to restart your machine.</p>
<p>Guess what? It doesn&#8217;t do a thing!</p>
<p>Thus you&#8217;ll have to endure your project rebuilding every time even when you didn&#8217;t make ANY changes at all!</p>
<p>Worse of all, you may have forgotten that you&#8217;ve changed your date or worst of all, you <strong>don&#8217;t even know you changed your date</strong>.</p>
<p>This problem is very apparently in the older Windows XP where if you open the calendar in an impromptu manner, ie, a singe click on your date time and then you select a date some time before, this becomes the <strong>set date</strong>.</p>
<h2>Conclusion</h2>
<p>This is a very short article by the way. Hopefully it&#8217;ll ease the pain of having to read so much. I&#8217;ll probably do the same over the next few weeks for my articles too.</p>
<p>Anyway, the conclusion is very simple. You&#8217;ll have to manage any time changes if you ever must change your system date. If not, this should be one of the things you should look out for if your solution keeps rebuilding itself <strong>even when there isn&#8217;t any changes introduced</strong>.</p>
<p>Everybody have a great week ahead! Signing off for now!</p>
<p>Regards,<br />
Jeremy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stridesdev.org/2011/04/software-quality-part-13-backtracking-time/feed/</wfw:commentRss>
	
		<series:name><![CDATA[Software Quality Series]]></series:name>
	</item>
		<item>
		<title>Software Quality - Part 12</title>
		<link>http://www.stridesdev.org/2011/03/software-quality-part-12/</link>
		<comments>http://www.stridesdev.org/2011/03/software-quality-part-12/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 01:39:47 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
		
		<category><![CDATA[Weekly Tuesday Dose of goodness]]></category>

		<guid isPermaLink="false">http://www.stridesdev.org/?p=1054</guid>
		<description><![CDATA[This entry is part 12 of 14 in the series Software Quality SeriesHi all,
This week&#8217;s post comes a little later than usual because I&#8217;m not  satisfied with the quality of my draft. After some serious thoughts and  revamps, I&#8217;ve managed to rewrite the article to better serve this week&#8217;s  topic - Dangers [...]]]></description>
			<content:encoded><![CDATA[<div class="seriesmeta">This entry is part 12 of 14 in the series <a href="http://www.stridesdev.org/series/software-quality-series/" title="series-31">Software Quality Series</a></div><p>Hi all,</p>
<p>This week&#8217;s post comes a little later than usual because I&#8217;m not  satisfied with the quality of my draft. After some serious thoughts and  revamps, I&#8217;ve managed to rewrite the article to better serve this week&#8217;s  topic - Dangers of using stubs.</p>
<p>By the end of this post, we&#8217;d have covered most, if not all of the basics in Software Quality.</p>
<p>We&#8217;ll then move on (continuing on the series) to other parts of  software quality processes which can help you in your daily development  or higher level processes.</p>
<p>So what&#8217;s so dangerous about stubs? Let&#8217;s find out&#8230;<br />
<span id="more-1054"></span></p>
<h2>Introduction</h2>
<p>When it comes to stubs, it can come from at least 2 areas.</p>
<ol>
<li> Unit Testing</li>
<li>Runtime Testing with stubs</li>
</ol>
<p>We must also remember that stubs are temporary measure to fulfill a  prerequisite and should never be considered as a means to an end. This  being said, there&#8217;re also some not-so-obvious mechanisms hidden when <strong>instrumented stubs </strong>are used.</p>
<p>In most cases, there&#8217;ll always be a safety barrier when it comes to stubs.</p>
<h2>Safe Stubs</h2>
<p>We&#8217;ve mentioned this type of stubs in our previous article. What are  they used for? As mentioned in my previous article, safe stubs are  usually either generated automatically or pre-generated to intercept  certain potentially dangerous function calls.</p>
<p>What it does basically is to return a failure value, be it a return  code or NULL pointer, it&#8217;s main purpose for doing that is to protect the  caller from accidentally or unintentionally making those calls which  leads to disastrous results.</p>
<p>In a good stub framework, many of  these dangerous functions will be stubbed out automatically as safe  stubs. Here&#8217;s the following to name a few:</p>
<ol>
<li>f_open()</li>
<li>f_write()</li>
<li>f_prinf()</li>
</ol>
<p><em>* Arguments are omitted because I&#8217;m lazy&#8230;<br />
** The functions have a&#8217; f_&#8217; instead of &#8216;f&#8217; because of a problem. Most host applications use the &#8216;f&#8217; variant while some embedded systems uses the &#8216;f_&#8217; variant.</em></p>
<p>Just take <strong>f_open()</strong> for example, if the following code is executed in original definition (ie, without being stubbed out), what will happen?</p>
<pre>FILE* fp = f_open ("abc.txt", "w") ;</pre>
<p>This piece of code may be a file open operation, but in reality, it actually overwrites the existing file  (if any) <strong>without any warning</strong> and set it to <strong>0 bytes</strong>.</p>
<p>Imagine  100 test case, data driven, each with a different filename, some  sensitive, executing without safe stubs. True, some files may be  protected by the OS, but ask yourself, what are the chances that one of  these files isn&#8217;t?</p>
<p>It may not be an OS file. It could be something important to you or to your company.</p>
<p>Now, this is a danger that occurs <strong>only if safe stubs aren&#8217;t used</strong>. Evidently, we&#8217;ve seen safe stubs in action automatically so what&#8217;s the problem?</p>
<h2>Circumvention</h2>
<p>This  word has been mentioned several times last week and as explained, this  is a useful way to control the stub. A good stubbing framework gives the  user the ability to do at least the following below:</p>
<ol>
<li>Controlled return value</li>
<li>Bridging to original definition by conditions</li>
<li>Exception testing</li>
</ol>
<p>All  these 3 have their own dangers. However, before going deep into it, we  must first understand that, circumvention is usually done <strong>by choice</strong>. Thus this increases the risks of introducing errors into the stub itself.</p>
<p>Out of these 3, the 2nd point is the most dangerous of all. Why?</p>
<h2>Reality vs Stub Limbo vs Actual</h2>
<p>Instrumented stubs or hand-made stubs all have a common characteristic. That is, they both sit <strong>in between </strong>the reality and the actual execution.</p>
<p>Let&#8217;s first talk about reality and actual execution, what&#8217;s the difference?</p>
<p>You can understand <strong>reality</strong> as the program execution during the run while <strong>actual</strong> refers to the original definitions of other functions.</p>
<p>The stub itself serve as a limbo. Which means to say, it can alter <strong>reality</strong> by preventing the program flow from ever reaching the <strong>actual </strong>execution.</p>
<p>Since it&#8217;s in a limbo, users can control the end state of the stubs, leading to a few possible dangerous scenarios</p>
<ol>
<li>Erroneous stub returns causing incorrect or unstable program flows</li>
<li>Erroneous stub bridge, causing the wrong data to be passed to the original definitions</li>
<li>Erroneous exception triggers, causing the tests to fail incorrectly</li>
</ol>
<p>Let&#8217;s talk about them one by one.</p>
<h3>1 - Erroneous stub returns causing incorrect or unstable program flows</h3>
<p>Stubbing  requires full knowledge on the unit itself. Without sufficient or full  knowledge, the stub writer will not have a clear picture on what <strong>not to return</strong>.</p>
<p>Of course, one can defend that this is meant to test for the unit&#8217;s <strong>robustness</strong>. However, we must be careful with such tests. Even with techniques like <strong>defensive programming</strong> (which I&#8217;ll talk about soon), there&#8217;s a limit.</p>
<p>Simply put, we don&#8217;t test things that <strong>can never happen</strong>.  If you force things to work unnaturally, even if you managed to write  up a test suite, the stubs will end up triggering all the incorrect  behavior.</p>
<p>Naturally, one may put the blame on the application  design. But - that&#8217;s where the limit comes in. Going in with erroneous  stubs is a big step towards test failures.</p>
<h3>2 - Erroneous stub bridge, causing the wrong data to be passed to the original definitions</h3>
<p>This  is caused by incorrect test case identification or a lack of  communications between the test case writer and the stubs programmer.</p>
<p>The case in point is when functions like <strong>f_open()</strong> has a certain requirements to really be opened and most of the time be a <strong>safe stub</strong>.  This means, that, one, by circumvention, I go around my original safe  stub. Two, I channel my stub flow according to the test case ID assigned  by my test case writer.</p>
<p>If this flow is channeled correctly, then  things will go well. But even if it&#8217;s well-channeled, we&#8217;ll still need  to be concerned on the <strong>data </strong>that&#8217;s being passed into the stubs.  Should the data have something that the stubs cannot handle, ie, new  test cases with a new set of data, then there&#8217;s danger of file  corruption via circumvention implemented in a stub.</p>
<p>This is in fact the most damaging possibility when it comes to stubs.</p>
<h3>3 - Erroneous exception triggers, causing the tests to fail incorrectly</h3>
<p>Lastly, we have incorrect exception triggers.</p>
<p>This  is very simple. A function or method with try-catch blocks will usually  have 1 or more catch(s) to handle different types of exception. In C++,  we&#8217;re also discouraged from catching the mother of all exceptions - <strong>std::exception</strong>.</p>
<p>While  this is so, there&#8217;ll be cases where we&#8217;re forced to catch this mother  exception because we simply don&#8217;t know what will happen when we call  that particular method.</p>
<p>By right, this is bullshit. You should  know which method you&#8217;re calling and know which exception it can  possibly throw and when  and how it&#8217;ll throw.</p>
<p>Well - yes. You&#8217;ll know if you have the documentations. You&#8217;ll know if you have access to the source codes.</p>
<p>However, you won&#8217;t know if your API is using <strong>another API</strong>. If such things are not properly documented, then here it goes.</p>
<p>So  back to stubs - usually in your codes, you&#8217;ll stub out the function  that can throw exception and circumvent it accordingly. Here&#8217;s an  example:</p>
<pre>try
{
    DoThis();
}
catch( std::bad_alloc&amp; exp )
{
}
catch( someException&amp; exp )
{
}</pre>
<p>In this example, the stub programmer is only supposed to throw 2 types of exceptions:</p>
<ol>
<li>std::bad_alloc</li>
<li>someException</li>
</ol>
<p>However, if the stub throws <strong>std::exception</strong> then we have a problem. The code did not cater for such an exception to be thrown and therefore has to throw up further.</p>
<p>In  terms of unit testing, this unit has failed to handle the exception on  its own. Throwing back an exception means that the test unit has <strong>failed.</strong></p>
<p>Thus,  throwing the wrong exception can lead to incorrect results too. Though,  in all, this consequence isn&#8217;t anywhere as damaging as others.</p>
<h2>Conclusion</h2>
<p>Finally, we&#8217;ve come to an end to stubs and mock objects. I&#8217;ve mostly  covered stubs and less on mock objects due to what I&#8217;ve experienced.</p>
<p>All I can say is, stubs can make you, but it certainly can break you big time as well.</p>
<p>Stubs  are usually needed when it comes to testing static libraries, where its  default build process doesn&#8217;t include any linking. This causes an  effect known as <strong>declaration without definition</strong> to be sustained without any errors.</p>
<p>The errors that appear later will be known as <strong>linker errors - unresolved external link error.</strong> Symbol XXX is used in class YYY.</p>
<p>I  also hope that the dangers of stubs is more or less (more of less I  feel) described in this article. Hopefully it can help someone.  Remember, these points are not exhaustive. I&#8217;m in no ways representative  of any standards that tells you that this is the truth and the only  truth.</p>
<p>In my future articles, I&#8217;ll talk about other areas in  software quality. Hopefully that&#8217;ll help you in your development career  no matter where you are, which language you code in or whatever.</p>
<p>From  here on, I&#8217;ll need to take a break to settle into my new digs. I&#8217;ll  probably return in early April to continue with my Software Quality  series.</p>
<p>Thanks for reading my posts! I truly appreciate it!</p>
<p>Thus, have a great weekend ahead. Hope you&#8217;ll have a great month of March and I&#8217;ll see you again in April!</p>
<p>Signing off,<br />
Jeremy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stridesdev.org/2011/03/software-quality-part-12/feed/</wfw:commentRss>
	
		<series:name><![CDATA[Software Quality Series]]></series:name>
	</item>
		<item>
		<title>Unable to post!</title>
		<link>http://www.stridesdev.org/2011/03/unable-to-post/</link>
		<comments>http://www.stridesdev.org/2011/03/unable-to-post/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 17:01:53 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
		
		<category><![CDATA[Weekly Tuesday Dose of goodness]]></category>

		<guid isPermaLink="false">http://www.stridesdev.org/?p=1061</guid>
		<description><![CDATA[Dear all,
There has been some weird issues going on now that I can&#8217;t upload Software Quality part 12.
Please hang on while I try to resolve the issues here. Will share this article asap. Sorry for the delay.
Regards,
Jeremy
]]></description>
			<content:encoded><![CDATA[<p>Dear all,</p>
<p>There has been some weird issues going on now that I can&#8217;t upload Software Quality part 12.</p>
<p>Please hang on while I try to resolve the issues here. Will share this article asap. Sorry for the delay.</p>
<p>Regards,<br />
Jeremy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stridesdev.org/2011/03/unable-to-post/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Software Quality - Part 11</title>
		<link>http://www.stridesdev.org/2011/03/software-quality-part-11/</link>
		<comments>http://www.stridesdev.org/2011/03/software-quality-part-11/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 16:38:07 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
		
		<category><![CDATA[Weekly Tuesday Dose of goodness]]></category>

		<guid isPermaLink="false">http://www.stridesdev.org/?p=1048</guid>
		<description><![CDATA[This entry is part 11 of 14 in the series Software Quality SeriesHi all,
This week we&#8217;ll talk about stubs in greater detail. We&#8217;ve seen how stubs can be a useful helper and there&#8217;re many types of stubs as well.
We&#8217;ll also need to cover some hidden dangers of stubs and mock objects as well.
So what&#8217;s so [...]]]></description>
			<content:encoded><![CDATA[<div class="seriesmeta">This entry is part 11 of 14 in the series <a href="http://www.stridesdev.org/series/software-quality-series/" title="series-31">Software Quality Series</a></div><p>Hi all,</p>
<p>This week we&#8217;ll talk about stubs in greater detail. We&#8217;ve seen how stubs can be a useful helper and there&#8217;re many types of stubs as well.</p>
<p>We&#8217;ll also need to cover some hidden dangers of stubs and mock objects as well.</p>
<p>So what&#8217;s so dangerous about stubs and mock objects anyway? Let&#8217;s find out.<span id="more-1048"></span></p>
<h2>Introduction</h2>
<p>In this article we&#8217;ll talk about several aspects of stubs and how we can make full use of them to our full advantage.</p>
<p>First of all, as explained in my previous posts, stubs and mock objects are <strong>temporary</strong> assets which help bridge functionalities or mock state representation of a certain module.</p>
<p>We&#8217;ll talk about how much efforts should we invest in such temporary assets, what types of stubs and mock objects do we have and the dangers involved in using such assets.</p>
<h2>Efforts vs Effects</h2>
<p>Looks familiar isn&#8217;t it? This is the name of one of the post that was published a long long time ago.</p>
<p>When it comes to manual processes, stubs and mock objects stand in a single line. That&#8217;s because, if I can implement a mock object, I might as well do the stubs inside the mock object so that I can save time for others.</p>
<p>That&#8217;s a pretty standard and likely clever way of doing things. The only problem is - the code has to know of the existence of your mock objct.</p>
<p>Thus the amount of efforts come not just from coding the mock object and integrating it but also from maintaining the mock object integrations.</p>
<p>For example, you&#8217;ll have to account for how many integrations to this mock object is done so that you&#8217;ll know where to remove them when the project is finally ready for SIT or UAT. Very likely SIT (System Integrated Testing).</p>
<p>When you actually remove the mock objects from integration, you&#8217;ll begin to feel that it was such an inefficient use of time. Objects that are coded with care are now poised for removal from project, possible complete deletion once the project finishes.</p>
<p>And the effects? Well - it served it purpose more than less.</p>
<p>One more thing we&#8217;ll have to take in mind is that if we&#8217;re doing C++, then extra care must be taken when coding this object so that it doesn&#8217;t cause memory leaks or stub-triggered memory problems.</p>
<p>It&#8217;s like - nursing a pet from a young till its teen years and then slaughter it for no apparent reason or usage. That hurts.</p>
<h2>Stubbing/Mock Object Framework vs Manual Efforts</h2>
<p>Naturally, a stubbing or mock object framework can prove to be very useful in terms of helping the stub/mock object programmer to create such assets with ease. However, for such a framework to be viable, it must at least have the following qualities:</p>
<p>1) Ease of use<br />
2) Visibility of what&#8217;s stubbed, what&#8217;s not<br />
3) Ease of management<br />
4) Auto-integration<br />
5) Does not <strong>TAINT</strong> the original source codes</p>
<p>Let&#8217;s talk about all the 5 qualities.</p>
<h3>Ease of Use</h3>
<p>Naturally, this is one of the expectations when it comes to stubbing frameworks. However, the notion of &#8220;ease&#8221; can be subjective. For one, &#8220;ease&#8221; could be a code snippet generator, while for others, &#8220;ease&#8221; could be as complicated as a stub management system.</p>
<h3>Visibility</h3>
<p>Anyone who does stubs will be concerned as to which method is being stubbed out or which objects are passed in as mock objects. This is to be able to know the exact ingredients used for a particular white-box test case so that the end results will be correct still.</p>
<h3>Ease of Management</h3>
<p>Naturally, once the locations of all the stubs and mock objects are known, the next step will be the ease of accessing or removing them in a list. Thus removing the need for stub programmers to write/remove/search for declarations and definitions manually.</p>
<h3>Auto-integration and taint-free source codes</h3>
<p>This is one of the hardest things to do in stubs. Auto-integration means that quite a lot has to be done. There&#8217;re several approaches here but in my opinion, the best auto-integration will be a seamless one, ie, integration without any changes to the original source codes.</p>
<p>This is only possible with <strong>instrumentation</strong>.</p>
<h2>Stub Types</h2>
<p>Now, let&#8217;s put mock objects down and go into stubs itself. As we know, stubs by definition are nothing but methods returning a dummy value.</p>
<p>However, even so, there&#8217;re several types of stubs. Namely:</p>
<p>1) Original<br />
2) User<br />
3) Safe<br />
4) Virtual</p>
<p>So what are they?</p>
<p><strong>Original</strong> - Not stubs. It&#8217;s really just original source codes<br />
<strong>User</strong> - User-defined stubs, the ones that you&#8217;ll write<br />
<strong>Safe </strong>- Functions that are deemed to be dangerous by the stubbing framework will automatically stub them out for you<br />
<strong>Virtual</strong> - It&#8217;s to indicate that this stub is based on a virtual method call</p>
<p>A good stubbing framework will allow several types of tests to be done. They&#8217;re namely:</p>
<p>1) Backdoor testing via circumvention<br />
2) Specific return value via circumvention<br />
3) Controlling who gets the stub value, who gets the real value<br />
4) Exception testing</p>
<p>The word &#8216;circumvention&#8217; appeared twice. So what exactly is circumvention? For the benefit of those whose command of the English language may not as good as others, circumvention essentially means <strong>going around</strong>.</p>
<p>So what are we going around? Well, the question is answered by why are we doing stubs in the first place. The answer? <strong>White Box Test Cases!</strong></p>
<p>Let&#8217;s take a look at the 4 types of testing.</p>
<h3>Backdoor Testing</h3>
<p>I&#8217;ve mentioned these type of testing many times in my previous articles. The reason for this type of testing to exist is because of the inherent limitations of white box testing. As we know, white box testing primarily involves methods that allows arguments to be passed in.</p>
<p>Thus, methods that pull values using the <strong>Pull Approach</strong> will be nearly impossible to test.</p>
<p>The solution to this problem is backdoor testing and the ingredient to this solution is stubs.</p>
<h3>Specific Value via Circumvention</h3>
<p>This method is closely tied to backdoor testing with the exception that the former is done because there&#8217;s no other choice. The former is done because the original codes <strong>cannot reproduce the values required for the test</strong>.</p>
<p>Such scenarios are common among pull approaches that pull values from the hardware. A good scenario will be the commercial jetliner autopilot system. This system makes use of readings from the instruments to make certain decisions, ie, to ascend or descend.</p>
<p>Thus, if there&#8217;s a test to test such a condition, it&#8217;ll be impossible to just simply power the hardware on and hope that the plane can be simulated at 15000 feet. This is provided that the hardware even comes with a simulator or simulation device.</p>
<p>Remember here, we&#8217;re testing codes, not integration here - thus if real hardware could be avoided, they should be avoided.</p>
<p>Thus, circumvention comes in handy.</p>
<h3>Stub Return Value Control</h3>
<p>One of the most important aspects of stubs is to be able to return a <strong>different </strong>return value based on a certain predicate. In terms of white box testing, the most direct predicate will be the <strong>test case name</strong>. If registered correctly, the test case name can be used to check if the test runner is running a particular test case.</p>
<p>Once this condition is fulfilled, the stub will then be able to return the desired value.</p>
<p>It can also reconnect to the original source codes and return the original value instead if desired.</p>
<h3>Exception Testing</h3>
<p>One of the hardest aspects in unit testing is to trigger an exception. As we know, exceptions are thrown for <strong>unexpected behavior</strong>. Therefore, there&#8217;re 2 ways to generate this unexpected behavior.</p>
<p>1) Create the scenario to generate this behavior, ie, data reading from thumb drive and you pull it out to trigger an IO Exception.<br />
2) Use stubs</p>
<p>Naturally, the use of stubs is a more enticing proposition than to physically test the code via manual means. This usage is naturally evolved from specific return values and stub control.</p>
<p>Based on test case predicates, the stub can actually check and throw exceptions at certain test cases to simulate an exception scenario where the <strong>catch</strong> handling code comes into play.</p>
<p>On top of that - it allows you to see how the exception is being handled and how much coverage is derived from it.</p>
<h2>Conclusion</h2>
<p>Again, it has been a lot of information this week. I&#8217;m afraid I&#8217;d have to leave the dangers of using stubs behind till next week.</p>
<p>One thing I&#8217;d like to note. The stubs we&#8217;ve been talking so far all belong to the same stub approach known as <strong>Method-Specific Stubs.</strong></p>
<p>There&#8217;s another stub approach known as <strong>Test-Specific Stubs</strong>.</p>
<p>The differences are -<br />
1) Method specific stubs allows pipe-blockage of all the method calls to the method it stubs out<br />
2) Test specific stubs only work for the test case it&#8217;s tied to</p>
<p>We&#8217;ll however, not talk abt test specific stubs.</p>
<p>That&#8217;s it for this week. Please have a great weekend ahead!</p>
<p>Signing off,<br />
Jeremy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stridesdev.org/2011/03/software-quality-part-11/feed/</wfw:commentRss>
	
		<series:name><![CDATA[Software Quality Series]]></series:name>
	</item>
		<item>
		<title>Software Quality - Part 10</title>
		<link>http://www.stridesdev.org/2011/02/software-quality-part-10/</link>
		<comments>http://www.stridesdev.org/2011/02/software-quality-part-10/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 15:00:10 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
		
		<category><![CDATA[Weekly Tuesday Dose of goodness]]></category>

		<guid isPermaLink="false">http://www.stridesdev.org/?p=1038</guid>
		<description><![CDATA[This entry is part 10 of 14 in the series Software Quality SeriesDear all,
Time to move on to the next topic in Software Quality! Today, we&#8217;ll talk about stubs and clear up the myth between stubs and mock objects.
Stubs are commonly used a temporary gateway to tackle missing implementations. The question is how is this [...]]]></description>
			<content:encoded><![CDATA[<div class="seriesmeta">This entry is part 10 of 14 in the series <a href="http://www.stridesdev.org/series/software-quality-series/" title="series-31">Software Quality Series</a></div><p>Dear all,</p>
<p>Time to move on to the next topic in Software Quality! Today, we&#8217;ll talk about stubs and clear up the myth between stubs and mock objects.</p>
<p>Stubs are commonly used a temporary gateway to tackle <strong>missing implementations</strong>. The question is how is this gateway being implemented. Many a times, people might as well just do an entire class to mock the entire functionality. Well&#8230; so is that a mock object or stub?</p>
<p>What are their differences?</p>
<p>Read on more to find out&#8230;<span id="more-1038"></span></p>
<h2>Introduction</h2>
<p>Remember in my last article, I mentioned about Neo in the Matrix, being stuck in a train station, sort of like a limbo to nowhere. The only person that can access that place is the train master.</p>
<p>This is by right the proper way to do stubs and mock objects.</p>
<p>Most of the time, our codes have to be modified in order to accomodate the stub or the mock object that implements the stub. That code modification is considered to be undesirable since it increases the need to keep track of the changes so that it doesn&#8217;t mess up with the final build.</p>
<p>Nobody would want to have a mock object integrated into the final version of the build.</p>
<p>However, in order to achieve modification-free stubbing, you&#8217;ll need to <strong>instrument</strong> your codes. Thus there&#8217;ll be a need to use a certain tools out in the market that can help you with this. There&#8217;s certain known framework out there that can already do it flawlessly, I&#8217;m not at liberty to reveal them though; you&#8217;ll have to find out those tools yourself. All I know is that there&#8217;re existing solutions, some free and some paid.</p>
<h2>Prerequisites</h2>
<p>Even stubs and mock objects have their own prerequisites. I&#8217;ll mention them here so that you won&#8217;t need to fall into the same trap.</p>
<p>1) Knowing the difference between <strong>static libraries</strong> and <strong>executables</strong><br />
2) Understanding <strong>declarations</strong> and <strong>definitions</strong><br />
3) Knowing the difference between <strong>transient</strong> and <strong>persistent</strong><br />
4) Knowing the limitations of <strong>white box testing</strong><br />
5) <strong>Pull-approach</strong> design</p>
<h2>Static Libraries and Executable</h2>
<p><strong>Static Libraries </strong>and <strong>Executables </strong>are handled differently during their respective builds. The biggest difference is the final part where static libraries are <strong>archived</strong>, executables are <strong>linked</strong>.</p>
<p>Dynamic linked libraries and shared objects are considered <strong>executables</strong>.</p>
<p>The difference between the archive and linking process is that, the linker searches all the definitions required and reports a <strong>linker error</strong> with errors like <strong>undefined symbol</strong> used in certain codes.</p>
<p>Why are we so concerned about this? The reason is very simple - if you&#8217;re working on an <strong>Application</strong> or <strong>Shared Object/DLL</strong> project, you&#8217;ll know what are the symbols that could possibly be missing simply by building the project.</p>
<p>However, if you&#8217;re working on a <strong>Static Library </strong>project, there&#8217;s a chance that there might be some functions or classes that has <strong>declarations</strong>, but no <strong>definitions</strong>.</p>
<p>Static Libraries can avoid undefined symbol problems because the symbols search is <strong>deferred</strong> till it&#8217;s linked onto an application that uses it. Thus the linker errors won&#8217;t appear only until you link and static libraries <strong>don&#8217;t link!</strong></p>
<h2><strong>Declarations and Definitions</strong></h2>
<p>This is really going back to the basics. But I&#8217;ll make it short. When you declare a method, it&#8217;s usually in a header file and it has no body unless it&#8217;s inlined. The method body is usually in a cpp file and that body is known as the method <strong>definition</strong>.</p>
<p>This definition must always be around during linking so that the linker can resolve the missing symbols.</p>
<h2>Transient and Persistent</h2>
<p>This are really 2 IT words that ring about across the industry. But I&#8217;ll make it very simple here as well.</p>
<p><strong>Transient</strong> - No state, no variables and it comes and goes, such as a <strong>function</strong>.<br />
<strong>Persistent </strong>- Has state, has variables and have a lifespan and represents a certain behavior, such as an <strong>object</strong>.</p>
<h2>Limitations of White Box testing / Pull-approach design</h2>
<p>As mentioned in my previous posts, white box testing is poised to test methods that <strong>pass in arguments</strong>. But it doesn&#8217;t seem to cater well to methods that call other methods to get values.</p>
<p>This is known as the <strong>pull approach</strong>. Instead of working on the values passed into itself, it works more independently by drawing values from other areas. For example, reading from value from a piece of hardware.</p>
<p>Thus it&#8217;s important to know that there&#8217;ll always be some people that uses the pull-approach methodology. This will also affect the reason as to why we&#8217;re using stubs as well.</p>
<h2>Differences between Mock Objects and Stubs</h2>
<p>Let&#8217;s talk about the former - mock objects.</p>
<h3>Mock Objects and Mock Instances</h3>
<p>First of all, it&#8217;s an object that has <strong>state</strong> and some degree of <strong>behavior</strong>.</p>
<p>A cousin of mock objects is <strong>mock instance.</strong> A mock instance is based on a <strong>fully implemented</strong> object type.</p>
<p>A mock object may not necessarily be fully implemented.</p>
<h3>Stubs</h3>
<p>Stubs by itself, in definition, is simply just a method that returns a <em><strong>dummy value</strong></em>.</p>
<p>A stub may not necessarily be part of a mock object. It can also be part of a half-implemented class as a temporary measure to satisfy technical build requirements.</p>
<p>In any case, it serves more of a temporary bypass and more importantly, it doesn&#8217;t by default contain any state. That being said, it simply implies that it doesn&#8217;t represent an entire object.</p>
<p>However, we can make changes to it. Such as creating a<strong> static </strong>instance of a <strong>complete object</strong> and then return it from a stub.</p>
<p>The whole setup is still a stub-return, but it now can hold state.</p>
<p>That&#8217;s probably the reason why there&#8217;s so much confusion between a stub and a mock object.</p>
<p>Moreover, a mock object doesn&#8217;t require a stub function/method to return an instance of itself. It simply exists as a dummy implemented or half-implemented form and definitely can hold states.</p>
<h2>Conclusion</h2>
<p>In conclusion, we&#8217;ve spoken on the prerequisites of understanding stubs and mock objects, we&#8217;ve also discussed on their differences and their usage.</p>
<p>In the next article, I&#8217;ll talk about the different types of stubs and when or how each type of stubs should or should not be used.</p>
<p>One of the key concerns or consideration regarding the dummy world or stub world, is the amount of modifications on the actual source codes required for stubbing and mock object to take place.</p>
<p>We&#8217;ll also talk about the effects of stubbing on coverage as well.</p>
<p>Till then, have a great week ahead!</p>
<p>Signing off,<br />
Jeremy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stridesdev.org/2011/02/software-quality-part-10/feed/</wfw:commentRss>
	
		<series:name><![CDATA[Software Quality Series]]></series:name>
	</item>
		<item>
		<title>Software Quality - Part 9</title>
		<link>http://www.stridesdev.org/2011/02/software-quality-part-9/</link>
		<comments>http://www.stridesdev.org/2011/02/software-quality-part-9/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 01:44:36 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
		
		<category><![CDATA[Weekly Tuesday Dose of goodness]]></category>

		<guid isPermaLink="false">http://www.stridesdev.org/?p=1033</guid>
		<description><![CDATA[This entry is part 9 of 14 in the series Software Quality SeriesDear all,
As promised, today we should be able to wrap up coverage completely and call it a day. Coverage has been a big topic as we all know and has so far taken up quite a few posts for the last few weeks.
Today [...]]]></description>
			<content:encoded><![CDATA[<div class="seriesmeta">This entry is part 9 of 14 in the series <a href="http://www.stridesdev.org/series/software-quality-series/" title="series-31">Software Quality Series</a></div><p>Dear all,</p>
<p>As promised, today we should be able to wrap up coverage completely and call it a day. Coverage has been a big topic as we all know and has so far taken up quite a few posts for the last few weeks.</p>
<p>Today we&#8217;re going to discuss the last coverage metric - Modified Condition Decision Coverage (MC/DC).</p>
<p>What&#8217;s so important about this coverage? Who should pay extra attention?</p>
<p>Let&#8217;s find out!<span id="more-1033"></span></p>
<h2>Introduction</h2>
<p>Modified Condition / Decision Coverage is the hardest coverage type to understand and then to master. There&#8217;re some prerequisites in order to draw out its benefits fully.</p>
<p>One of the important prerequisites to have is <strong>truth table</strong> and <strong>understanding of binary</strong>. The next most important prerequisite to have is understanding the <strong>order of evaluation</strong>. I&#8217;ll explain in further detail as we go along.</p>
<p>MC/DC is used to test all possible permutations in a single condition block within a single method or function.</p>
<p>To get 100% coverage for a single condition, you&#8217;ll need to fulfill all of its conditions via a &#8220;truth table&#8221;. I&#8217;ll explain later on how to create this truth table and then eliminating the impossible truths via order of evaluation.</p>
<p>I&#8217;m not too sure about its history but I&#8217;ve <strong>heard</strong> that this metric came up after a serious incident occurred to a fighter jet, reportedly a F-16 variant. The cause seems to be due to the instruments blacking out for no apparent reason.</p>
<h2>Is 100% MC/DC coverage possible?</h2>
<p>By using unit testing in a single thread, it&#8217;s possible to achieve 100% MC/DC coverage. Unit testing falters when it comes across multi-threaded applications. The most common flaw it has is <strong>inconsistent</strong> results from one run to another due to the lack of deterministic execution time for threads.</p>
<p>Even so, attaining 100% MC/DC is an arduous process. On top of that - <strong>no tools can automatically generate meaningful test cases that will cover MC/DC coverage</strong>. Any coverage is almost certainly coincidental. It&#8217;ll be good enough to have tools that tell you what are the parts of the condition truth table that is being covered so that you&#8217;ll know what to test next.</p>
<p>Basically, the tool knows what&#8217;s being tested or not, but it<strong> doesn&#8217;t understand the significance of the predicates</strong>. That means, it won&#8217;t be able to understand your sub-condition and determine which value or which stub to use to trigger that predicate.</p>
<p>On top of that, the road to achieving MC/DC also means another thing; the codes must be stable enough and shouldn&#8217;t be in full development by then. The reason is simple -if a programmer changes the condition one day and patches another condition another day, the testing team will go crazy.</p>
<p>There&#8217;s a lot of work involved in getting all the permutations drawn up. Since there&#8217;re no known tools out there that can display the possible permutations via a truth table, it&#8217;s usually up to the tester to <strong>draft out a test plan</strong> for MC/DC.</p>
<p>By the time the test cases are generated, the tester would have understood the code well enough. That&#8217;s how much work it requires.</p>
<h2>MC/DC Truth Table</h2>
<p>What&#8217;s a MC/DC truth table? Let&#8217;s take a look at this simple example here:</p>
<pre>if( a &lt; 2 &amp;&amp; b &gt; 3 )
{
}</pre>
<p>First of all, you&#8217;ll need to calculate the worst case scenario for your tests. The simplest and least accurate way to get a rough worst-case scenario guide is to take 2 power of N conditions.</p>
<p>In this case, there&#8217;re 2 conditions. Therefore it&#8217;s 2 power of 2 - giving you 4 test cases minimum. So take a look at this truth table:</p>
<table style="height: 94px;" border="1" width="303">
<tbody>
<tr>
<td><strong>a &lt; 2</strong></td>
<td><strong>b &gt; 3</strong></td>
</tr>
<tr>
<td>false</td>
<td>false</td>
</tr>
<tr>
<td>false</td>
<td>true</td>
</tr>
<tr>
<td>true</td>
<td>false</td>
</tr>
<tr>
<td>true</td>
<td>true</td>
</tr>
</tbody>
</table>
<p>Looks like the case right?</p>
<p>Well - no. Here&#8217;s where the 2nd prerequisite applies - <strong>Order of Evaluation</strong>.</p>
<p>As we know, C/C++ doesn&#8217;t blindly evaluate every single sub-condition. It depends very heavily on the comparison operator used and reacts accordingly. This is done mostly for safety and bits of optimization as well.</p>
<p>Thus if we apply <strong>order of evaluation</strong> to this case, the final truth table will be :</p>
<table style="height: 94px;" border="1" width="303">
<tbody>
<tr>
<td><strong>a &lt; 2</strong></td>
<td><strong>b &gt; 3</strong></td>
</tr>
<tr>
<td>false</td>
<td><strong>X</strong></td>
</tr>
<tr>
<td>true</td>
<td>false</td>
</tr>
<tr>
<td>true</td>
<td>true</td>
</tr>
</tbody>
</table>
<p>So what does &#8220;X&#8221; mean?</p>
<p>Based on the order of evaluation, if the <strong>first condition</strong> is <strong>false </strong>and the comparison operator is <strong>&amp;&amp;</strong>, then there&#8217;s no point to continue on. Thus, the outcome of the 2nd sub-condition doesn&#8217;t matter if the first one is false.</p>
<p>Please note - this whole exercise is only for a <strong>single condition</strong> in a <strong>single method</strong> belonging to a <strong>single class</strong> or <strong>namespace</strong>.</p>
<p>If there&#8217;re 10 conditions inside a method, you&#8217;ll have to repeat it 10 times. On top of that, you&#8217;ll have to get through the conditions above in order to allow the conditions below be hit.</p>
<h2>Coverage In Conclusion</h2>
<p>Finally we&#8217;ve come to the end of coverage. For the last few posts, we&#8217;ve been talking about different coverage types all day long. Some may wonder - is there a way to combine all the coverage together and generate a single report.</p>
<p>Let me tell you - it&#8217;s not a good idea to do it that way. The reason is very simple - not all coverage types combine well. Generating a single report may be good enough for a stamp of approval but it could also at the same time, be very confusing as well.</p>
<p>In such a case, it&#8217;ll be better to generate a report based on module or even based on granularity as low as a class itself.</p>
<p>This allows every component to be tested independently before being put together for another round of testing. On top of that, the tests and test results are well-documented and is easy to maintain.</p>
<p>I hope that I&#8217;ve managed to bring out the importance of having code coverage, whether it&#8217;s recorded via unit testing or recorded via runtime testing, having a coverage report is better than having none. More importantly, this coverage report should tell you where you&#8217;ve managed to test and which parts still need more tests to penetrate.</p>
<p>In all, coverage can be seen as a statistic, a visibility matrix or an indication of a possible technical, code  design or business design flaw.</p>
<p>Thus we&#8217;ve concluded all topics for coverage. If there&#8217;s any questions, feel free to leave a comment.</p>
<h2>Conclusion</h2>
<p>In conclusion as a whole, we&#8217;ll move towards the <strong>backdoor</strong> of unit testing. Also known as stubs. However, there&#8217;s a myth surrounding stubs. Are stubs and mock objects the same?</p>
<p>Do they share state or store states?</p>
<p>Well.. I&#8217;ll answer that next week in Software Quality part 10. Meanwhile, a thought for you to ponder on. Remember in Matrix Evolution, there&#8217;s a train station where Neo was stuck in? Sort of like a limbo but it&#8217;s a clear physical area rather than a subconscious construct. Try to imagine stubs as the train station.</p>
<p>Have a great week ahead!</p>
<p>Signing off,<br />
Jeremy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stridesdev.org/2011/02/software-quality-part-9/feed/</wfw:commentRss>
	
		<series:name><![CDATA[Software Quality Series]]></series:name>
	</item>
		<item>
		<title>Software Quality - Part 8</title>
		<link>http://www.stridesdev.org/2011/02/software-quality-part-8/</link>
		<comments>http://www.stridesdev.org/2011/02/software-quality-part-8/#comments</comments>
		<pubDate>Tue, 08 Feb 2011 13:06:10 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
		
		<category><![CDATA[Weekly Tuesday Dose of goodness]]></category>

		<guid isPermaLink="false">http://www.stridesdev.org/?p=1029</guid>
		<description><![CDATA[This entry is part 8 of 14 in the series Software Quality SeriesHi all,
This week, I&#8217;ll continue to describe the last 2 types of coverage which falls under condition-based category. Just a recap, last week we&#8217;ve covered line-based and snippet-based categories of coverage.
Why didn&#8217;t I finish off the post by covering the last 2 coverage [...]]]></description>
			<content:encoded><![CDATA[<div class="seriesmeta">This entry is part 8 of 14 in the series <a href="http://www.stridesdev.org/series/software-quality-series/" title="series-31">Software Quality Series</a></div><p>Hi all,</p>
<p>This week, I&#8217;ll continue to describe the last 2 types of coverage which falls under <strong>condition-based</strong> category. Just a recap, last week we&#8217;ve covered <strong>line-based</strong> and <strong>snippet-based</strong> categories of coverage.</p>
<p>Why didn&#8217;t I finish off the post by covering the last 2 coverage types? First of all, they&#8217;re significantly more important than the rest of the coverage types. Secondly, it&#8217;s not as straightforward as any of the coverage types.</p>
<p>To be specific, today I&#8217;ll just cover <strong>Simple Condition Coverage</strong> only due to the amount of explanations and materials required for the details. I&#8217;ll cover MC/DC next week. So, please bear with me till then.</p>
<p>So why are they so special? Read on&#8230;.<span id="more-1029"></span></p>
<h2>Introduction</h2>
<p>Condition-based coverage types are the ones that can tell you the most without revealing too much of itself. We&#8217;re going to talk about the following coverage types today.</p>
<p><strong>* Simple Condition Coverage</strong></p>
<p>The more popular coverage type is probably <strong>Modified Condition / Decision Coverage (MC/DC),</strong> especially in the aerospace industry. There&#8217;re standards out there such as <strong>DO-178B</strong> that mandates 100% compulsory MC/DC coverage for all unit tests. This is to minimize, if not, eliminate all possible software problems which may lead to cases like <strong>instrument blackout</strong> which can be fatal on board a commercial jet liner.</p>
<p>Ironically, it&#8217;s also the hardest coverage type to master and understand completely.</p>
<p>We&#8217;ll discuss the technique towards understanding MCDC <em><strong>next week</strong></em>.</p>
<h2>Simple Condition Coverage</h2>
<p>Simple condition means as it is literally. However, not all conditions are as simple as it looks. For example:</p>
<pre>...
if( a &gt; 0 &amp;&amp; b &gt; 3 &amp;&amp; c &lt; 2 &amp;&amp; d == 5)
{
}
else
{
}
...</pre>
<p>It looks like a complex condition right? Well yes. On top of that, there seems to be 4 different cases right? This by right leads to a case of 2^4 which is 16 test cases right?</p>
<p>No.</p>
<p>Remember, this is <strong>simple condition</strong> coverage. A simple condition is derived from the <strong>final binary outcome</strong> of the condition block. That means for this statement:</p>
<pre>if( a &gt; 0 &amp;&amp; b &gt; 3 &amp;&amp; c &lt; 2 &amp;&amp; d == 5)</pre>
<p>Can only either have an absolute <strong>positive</strong> or <strong>negative </strong>path based on the evaluation of all the sub-conditions along with the <strong>order of evaluation</strong>. The final outcome is known as the <strong>simple condition</strong> predicate.</p>
<p>In order to have 100% Simple Condition coverage for the previous line of code, both <strong>positive</strong> and <strong>negative</strong> blocks must be executed at least <strong>once</strong>.</p>
<p>Thus the user may be required to write at least 2 test cases or more, depending on the complexity of the sub-conditions, in order to achieve 100% simple condition coverage.</p>
<h2>Speciality of Simple Condition Coverage</h2>
<p>In tools that demarcate Simple Condition Coverage, the good ones will only mark a condition with both simple condition outcomes executed as covered. It&#8217;ll also mark the condition as <strong>not covered</strong> if the test cases fail to reach the condition in any scenario.</p>
<p>There&#8217;s however, a 3rd state. This is the <strong>only</strong> coverage type that has a 3rd state color. Let&#8217;s assume that if covered is <span style="color: #99cc00;">green</span>, not covered is <span style="color: #ff0000;">red</span>. This 3rd state will either be <span style="color: #ffcc00;">yellow</span> or <span style="color: #00ffff;">teal</span>.</p>
<p>What is this 3rd state?</p>
<p>It means that after executing all the test cases, the condition will <strong>only go into a positive or negative </strong>branch only. For example, if I have 100 test cases that ran, and after the tests are done, the condition has only so far executed either the positive or the negative branch.</p>
<p>This is a warning sign to a few possible problems. I&#8217;ll list a few of them.</p>
<p><strong>1) Possible unreachable codes due to one-sided condition(s)<br />
2) Possible short-circuited logic<br />
</strong>3) Possible code design flaw<br />
4) Possible business design flaw<br />
5) Possible lack of test case robustness</p>
<p>We&#8217;ll just talk about the first 2 items listed.</p>
<h3>Possible Unreachable Codes due to one-sided condition(s)</h3>
<p>These type of codes may exists due to volatile changes made to other parts of the codes which inadvertently led to a one-sided effect.</p>
<p>The simplest example of all would be:</p>
<pre>if(true)
{
}
else
{
... //this branch will never be executed
}</pre>
<p>Now, let&#8217;s think again. Could there have been some parts of the code which, under certain circumstances, return a constant value? Take this piece of code for example:</p>
<pre>if( CheckIsSystemOn() &amp;&amp; CheckIsSystemUnlocked() &amp;&amp; CheckMemoryStatus() )
{
}
else
(
//hard to tell whether if this block will be executed right?
)</pre>
<p>It&#8217;s hard to tell yes unless we check all the 3 function calls. If <strong>all 3 function calls </strong>return a <strong>constant value</strong>, regardless of true, false, zero or non-zero, it&#8217;ll cause a branch to be blocked off always.</p>
<p>Usually, we&#8217;re advised to put an <strong>else</strong> block as a &#8220;guarantee&#8221; or condition-block or condition-sealer to avoid logic problems later on. However, when <strong>else</strong> block is used in an overly conservative manner, there might be a case as described above. It&#8217;ll really look stupid to tell people that you&#8217;ve done so much as a contingency plan only to realise that it&#8217;ll <strong>never be executed</strong>.</p>
<h3><strong>Possible short-circuited logic</strong></h3>
<p>This is only available in programming languages that allow short-circuited logic to occur. C/C++ is definitely one of them.</p>
<p>Short-circuited logic occurs when a <strong>non-comparison</strong> operator is used instead. For example:</p>
<pre>if( a <strong>&amp;</strong> b )</pre>
<p>instead of</p>
<pre>if ( a <strong>&amp;&amp;</strong> b )</pre>
<p>Short-circuited logic are common and is used for certain specific purposes in many applications despite strong advice to avoid it. Alike <strong>pointers</strong>, short-circuits can only be used when you&#8217;ve attained a certain level of mastery in the language of your choice. Otherwise, you&#8217;ll end up busting your own codes with logic outcome that you&#8217;ll never even thought will happen.</p>
<p>In C++, a very common form of short-circuited logic comes from the comparison of <strong>pointers</strong> to see if they&#8217;re null or not.</p>
<p>Instead of checking for null like:</p>
<pre>int* a = NULL; //from somewhere
if( a == NULL ) {}</pre>
<p>we&#8217;ll do something like:</p>
<pre>int* a = NULL; //from somewhere
if(!a) {}</pre>
<p>If in any case, should the code above really be true, then you&#8217;ll have a <strong>partial simple condition coverage</strong> scenario since pointer <strong><em>a</em></strong> will always be null. The opposite occurs if you <strong>didn&#8217;t assign</strong> anything to pointer <strong><em>a</em></strong>. In this case, the pointer will be a non-zero random address. Thus <strong><em>if(!a)</em></strong> will always be false.</p>
<p>That&#8217;s probably all for Simple Condition Coverage. It should be quite straightforward to understand other than the special cases.</p>
<h2>Conclusion</h2>
<p>That&#8217;s another rather heavy load of information to digest. I&#8217;ll need your mind to be clear for MC/DC next week. After that, I&#8217;ll wrap up the topic of coverage and move on to stubs as promised.</p>
<p>Simple condition coverage is actually very easy to understand. The final outcome of every condition will either yield a true or false. For a complete coverage, both cases must happen during the tests. Otherwise, gaps will appear in results, including the middle case where the condition is either always true or always false.</p>
<p>The effect of unused codes is quite minimal but is also subjected to the project size. The bigger the team, the bigger the project, the lesser the discipline, more unused codes will creep in due to all kinds of reasons and excuses.</p>
<p>When this happens, your initial heap consumption will be so high that you&#8217;ll be wondering what went wrong. Thus Simple Condition Coverage can help remove unused codes as well.</p>
<p>That&#8217;s about it for today! I&#8217;ll write up the next post earlier so that I can post it early by 10am SGT instead of 10pm SGT.</p>
<p>Signing off,<br />
Jeremy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stridesdev.org/2011/02/software-quality-part-8/feed/</wfw:commentRss>
	
		<series:name><![CDATA[Software Quality Series]]></series:name>
	</item>
	</channel>
</rss>

