<?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>Good Practices &#8211; Games Architecture</title>
	<atom:link href="http://gamesarchitecture.com/category/goodpractices/feed/" rel="self" type="application/rss+xml" />
	<link>http://gamesarchitecture.com</link>
	<description>For indie developers who already know that making games is not easy</description>
	<lastBuildDate>Sat, 30 Dec 2017 20:29:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.8</generator>
	<item>
		<title>Short #2 Leave these parameters alone</title>
		<link>http://gamesarchitecture.com/short-2-leave-parameters-alone/</link>
		<comments>http://gamesarchitecture.com/short-2-leave-parameters-alone/#respond</comments>
		<pubDate>Sat, 30 Dec 2017 20:29:19 +0000</pubDate>
		<dc:creator><![CDATA[Bartosz]]></dc:creator>
				<category><![CDATA[Good Practices]]></category>
		<category><![CDATA[Shorts]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[parameters]]></category>
		<category><![CDATA[short]]></category>

		<guid isPermaLink="false">http://gamesarchitecture.com/?p=505</guid>
		<description><![CDATA[<p>You&#8217;re updating an old method by adding only two simple lines of code at the end, but something is really wrong here. You checked all the method calls and parameters that you&#8217;re passing, but they are correct. So maybe their values are incorrect? You run a debugger and check. Nope, they are perfectly fine. The &#8230; </p>
<p class="link-more"><a href="http://gamesarchitecture.com/short-2-leave-parameters-alone/" class="more-link">Continue reading<span class="screen-reader-text"> "Short #2 Leave these parameters alone"</span></a></p>
<p>The post <a rel="nofollow" href="http://gamesarchitecture.com/short-2-leave-parameters-alone/">Short #2 Leave these parameters alone</a> appeared first on <a rel="nofollow" href="http://gamesarchitecture.com">Games Architecture</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="aligncenter wp-image-508 size-full" src="http://gamesarchitecture.com/wp-content/uploads/2017/12/1514665184729.png" alt="lemmings_portal_method_parameters" width="1534" height="510" srcset="http://gamesarchitecture.com/wp-content/uploads/2017/12/1514665184729.png 1534w, http://gamesarchitecture.com/wp-content/uploads/2017/12/1514665184729-300x100.png 300w, http://gamesarchitecture.com/wp-content/uploads/2017/12/1514665184729-768x255.png 768w, http://gamesarchitecture.com/wp-content/uploads/2017/12/1514665184729-1024x340.png 1024w" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" /><br />
You&#8217;re updating an old method by adding only two simple lines of code at the end, but something is really wrong here. You checked all the method calls and parameters that you&#8217;re passing, but they are correct. So maybe their values are incorrect? You run a debugger and check. Nope, they are perfectly fine. The problem might be in the function itself, but it worked like a charm until now, so you&#8217;re rechecking your two lines and you still cannot see any issues.<span id="more-505"></span></p>
<p style="text-align: justify;">The problem might be the old code in the method. Maybe it doesn&#8217;t introduce any bugs by itself, but if it modifies procedure&#8217;s parameters at some place, it can cause annoying issues later.</p>
<p style="text-align: justify;">It is like basing the decision about renting an apartment on internet advertisement. The description seems great, the photos are splendid, and the price is low. But when you come to see the flat, it&#8217;s dirty, smells bad, and it lacks a fridge. The ad didn&#8217;t mention that previous residents damaged it and left it like that. You feel cheated. It would be great if you could just trust an ad, right?</p>
<p style="text-align: justify;">It seems impossible for an advertisement to be honest, but it is doable for the method code. Don&#8217;t change the function&#8217;s parameters in the body so that you can trust its signature. Of course, if the method is meant to modify its parameters, its name should denote it, or you should use the right keyword (i.e. out in C#) so that the user can be aware of it.</p>
<p>The post <a rel="nofollow" href="http://gamesarchitecture.com/short-2-leave-parameters-alone/">Short #2 Leave these parameters alone</a> appeared first on <a rel="nofollow" href="http://gamesarchitecture.com">Games Architecture</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamesarchitecture.com/short-2-leave-parameters-alone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Short #1 Don&#8217;t stick to &#8216;no negations in conditions&#8217; rule</title>
		<link>http://gamesarchitecture.com/short-1-dont-stick-to-no-negations-in-conditions-rule/</link>
		<comments>http://gamesarchitecture.com/short-1-dont-stick-to-no-negations-in-conditions-rule/#comments</comments>
		<pubDate>Tue, 12 Sep 2017 14:49:14 +0000</pubDate>
		<dc:creator><![CDATA[Bartosz]]></dc:creator>
				<category><![CDATA[Good Practices]]></category>
		<category><![CDATA[Shorts]]></category>

		<guid isPermaLink="false">http://gamesarchitecture.com/?p=422</guid>
		<description><![CDATA[<p>I mean, what&#8217;s the point? Let the code flow naturally after the &#8216;if&#8217; and leave more unlikely situations for &#8216;else&#8217; (displaying error for instance). In the &#8216;if&#8217; block, you can put cases that are more natural for your game. A player wants to deal damage to living enemies. It is more common, as dead bodies &#8230; </p>
<p class="link-more"><a href="http://gamesarchitecture.com/short-1-dont-stick-to-no-negations-in-conditions-rule/" class="more-link">Continue reading<span class="screen-reader-text"> "Short #1 Don&#8217;t stick to &#8216;no negations in conditions&#8217; rule"</span></a></p>
<p>The post <a rel="nofollow" href="http://gamesarchitecture.com/short-1-dont-stick-to-no-negations-in-conditions-rule/">Short #1 Don&#8217;t stick to &#8216;no negations in conditions&#8217; rule</a> appeared first on <a rel="nofollow" href="http://gamesarchitecture.com">Games Architecture</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;"><img class="aligncenter size-full wp-image-426" src="http://gamesarchitecture.com/wp-content/uploads/2017/09/Zrzut-ekranu-2017-09-11-o-19.46.55.png" alt="" width="1400" height="398" srcset="http://gamesarchitecture.com/wp-content/uploads/2017/09/Zrzut-ekranu-2017-09-11-o-19.46.55.png 1400w, http://gamesarchitecture.com/wp-content/uploads/2017/09/Zrzut-ekranu-2017-09-11-o-19.46.55-300x85.png 300w, http://gamesarchitecture.com/wp-content/uploads/2017/09/Zrzut-ekranu-2017-09-11-o-19.46.55-768x218.png 768w, http://gamesarchitecture.com/wp-content/uploads/2017/09/Zrzut-ekranu-2017-09-11-o-19.46.55-1024x291.png 1024w" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" />I mean, what&#8217;s the point? Let the code flow naturally after the &#8216;if&#8217; and leave more unlikely situations for &#8216;else&#8217; (displaying error for instance). In the &#8216;if&#8217; block, you can put cases that are more natural for your game. A player wants to deal damage to living enemies. It is more common, as dead bodies are usually waiting for being cleaned up and are less attractive for the player. So:<span id="more-422"></span></p>
<pre class="brush: csharp; collapse: false; title: ; notranslate">If (!enemy.IsDead())
{
    enemy.TakeDamage();
}
else
{
    enemy.AddForce(FORWARD); // Profane the corpse
}</pre>
<p style="text-align: justify;">It increases readability and stresses the expected game flow. If an attacker tells Mr. Wayne &#8220;Give me that wallet, or else…,&#8221; Mr. Wayne should give the wallet back, move on and doesn&#8217;t care about &#8220;else.&#8221; It&#8217;s the standard course of the situation and that solution should be considered first.</p>
<p>The post <a rel="nofollow" href="http://gamesarchitecture.com/short-1-dont-stick-to-no-negations-in-conditions-rule/">Short #1 Don&#8217;t stick to &#8216;no negations in conditions&#8217; rule</a> appeared first on <a rel="nofollow" href="http://gamesarchitecture.com">Games Architecture</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamesarchitecture.com/short-1-dont-stick-to-no-negations-in-conditions-rule/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Easy method to fix bugs in your game &#8211; Debugging (Part II)</title>
		<link>http://gamesarchitecture.com/easy-method-to-fix-bugs-in-your-game/</link>
		<comments>http://gamesarchitecture.com/easy-method-to-fix-bugs-in-your-game/#respond</comments>
		<pubDate>Wed, 09 Aug 2017 19:23:49 +0000</pubDate>
		<dc:creator><![CDATA[Bartosz]]></dc:creator>
				<category><![CDATA[Generic]]></category>
		<category><![CDATA[Good Practices]]></category>

		<guid isPermaLink="false">http://gamesarchitecture.com/?p=357</guid>
		<description><![CDATA[<p>In an ideal world, we write bug-free code. In the same universe, we don&#8217;t have to struggle with marketing our indie games, and we are skilled in design, programming, graphics and audio. Today I&#8217;ll try to make fixing bugs easier and faster for you, so you&#8217;ll have more time for more enjoyable tasks. Welcome to &#8230; </p>
<p class="link-more"><a href="http://gamesarchitecture.com/easy-method-to-fix-bugs-in-your-game/" class="more-link">Continue reading<span class="screen-reader-text"> "Easy method to fix bugs in your game &#8211; Debugging (Part II)"</span></a></p>
<p>The post <a rel="nofollow" href="http://gamesarchitecture.com/easy-method-to-fix-bugs-in-your-game/">Easy method to fix bugs in your game &#8211; Debugging (Part II)</a> appeared first on <a rel="nofollow" href="http://gamesarchitecture.com">Games Architecture</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-380" src="http://gamesarchitecture.com/wp-content/uploads/2017/08/Zrzut-ekranu-2017-08-09-o-20.48.33.png" alt="" width="1380" height="424" srcset="http://gamesarchitecture.com/wp-content/uploads/2017/08/Zrzut-ekranu-2017-08-09-o-20.48.33.png 1380w, http://gamesarchitecture.com/wp-content/uploads/2017/08/Zrzut-ekranu-2017-08-09-o-20.48.33-300x92.png 300w, http://gamesarchitecture.com/wp-content/uploads/2017/08/Zrzut-ekranu-2017-08-09-o-20.48.33-768x236.png 768w, http://gamesarchitecture.com/wp-content/uploads/2017/08/Zrzut-ekranu-2017-08-09-o-20.48.33-1024x315.png 1024w" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" /></p>
<p style="text-align: justify;">In an ideal world, we write bug-free code. In the same universe, we don&#8217;t have to struggle with marketing our indie games, and we are skilled in design, programming, graphics and audio. Today I&#8217;ll try to make fixing bugs easier and faster for you, so you&#8217;ll have more time for more enjoyable tasks. Welcome to the second part of the series about making debugging an (almost) pleasant experience for you.</p>
<p style="text-align: justify;">Fixing bugs is difficult enough. You don&#8217;t want to add &#8216;What the fuck is this shit&#8217; on top of that. In the previous post &#8211; <a href="http://gamesarchitecture.com/debugging-part-i-7-good-code-practices-that-will-help-you-in-debugging/" target="_blank" rel="noopener">7 good code practices that will help you in debugging</a> &#8211; we tried to get rid of those unpleasant thoughts, so in this one, we&#8217;ll be dealing with a pure puzzle solving. That is because bug happens. So let&#8217;s get into it.</p>
<p><span id="more-357"></span></p>
<h1>&#8216;Debugging&#8217; series</h1>
<p><a href="http://gamesarchitecture.com/debugging-part-i-7-good-code-practices-that-will-help-you-in-debugging/" target="_blank" rel="noopener">Part I &#8211; 7 good code practices that will help you in debugging</a><br />
Part II &#8211; Easy method to fix bugs in your game (this)</p>
<h1>Content</h1>
<p>6 steps of debugging<br />
Easy to use tools and real-life example</p>
<h1>6 steps of debugging</h1>
<p style="text-align: justify;">Every issue is different, so it is not possible to write exact workflow for every one of them, so we&#8217;ll generalize it a bit. One more thing. I use this workflow. It worked for me when I had to fix bugs in my code, as well as bugs in huge codebases that I didn&#8217;t contribute in writing at all. I worked it out, and I want to share it with you. It doesn&#8217;t have to be as fabulous for you as it is for me, but if you don&#8217;t have your workflow, I believe this one is a great start. And after that, I&#8217;ll show you a real-life example.</p>
<h3>A. Find a scenario that in 100% of cases triggers the bug</h3>
<p style="text-align: justify;">Play a game until you know exactly how to bring out the issue. It is even more important in foreign code or game part you don&#8217;t remember very well. It helps you understand the code better if you already know its in-game behavior and all circumstances for the bug.</p>
<h3>B. Use tools to find a place in the code that is responsible for it</h3>
<p style="text-align: justify;">By tools I mean the IDE features you can use to see what the code does and what are the values of variables. We&#8217;ll talk about them later. I assure you that if you find a region where the bug is located, you have more than a half of the work behind you. By region, I mean a method or methods, class or a module. All the code that is involved in the examined feature. That&#8217;s where an advice from the <a href="http://gamesarchitecture.com/debugging-part-i-7-good-code-practices-that-will-help-you-in-debugging/" target="_blank" rel="noopener">first part of the series</a>, as well as the ones from the &#8220;<a href="http://gamesarchitecture.com/read-it-like-a-book-centralized-control-part-i/" target="_blank" rel="noopener">Read it like a book &#8211; Centralized Control</a>&#8221; article make a difference.</p>
<h3>C. Use tools to find out what is going on there</h3>
<p style="text-align: justify;">After this one you&#8217;ll know if the place you have found in &#8216;B&#8217; is the one that causes the bug. If not, repeat &#8216;B&#8217; and &#8216;C&#8217; until you find the bug. I&#8217;ll show you the example of &#8216;C&#8217; in a moment.</p>
<h3>D. Fix it</h3>
<p style="text-align: justify;">After a couple (of dozens) of &#8216;B&#8217;-&#8216;C&#8217; iterations, you&#8217;ll know your enemy. Fix it. It may be a simple change from &#8216;&lt;&#8216; to &#8216;&lt;=&#8217; or it may require some refactoring. You&#8217;ll probably know.</p>
<h3>E. Use tools to find out if it looks okay now</h3>
<p style="text-align: justify;">Basically, you have to repeat the &#8216;C&#8217; to see if the code looks correct now. A pretty common mistake is avoiding this point and going straight to the &#8216;F.&#8217; However, missing the &#8216;E&#8217; leads to the trial and error loop that may last as well five minutes as five hours or even days. If you already made the &#8216;C,&#8217; &#8216;E&#8217; will be easy peasy. I&#8217;m guilty of that mistake sometimes, but until now I&#8217;ve worked out a Samuel L. Jackson-like signal in my head that says &#8220;Stop. Right. There, you <a href="https://youtu.be/c0LBi1MHoaU?t=27" target="_blank" rel="noopener">motherfucka&#8217; motherfucka&#8217;</a>&#8221; Yeah, I&#8217;ve got that alarm, I&#8217;m not joking. It backs me on the right path.</p>
<h3>F. Test it in the game</h3>
<p style="text-align: justify;">Play the scenario from the &#8216;A&#8217; couple of times, and check if related features didn&#8217;t break. If the codebase is large, even if it is wonderfully written, the change can entail some unwanted flaws. It is worth to check it now when you&#8217;re &#8220;in the code.&#8221; It will be cheaper to fix it now than later.</p>
<h1>Easy to use tools and real-life example</h1>
<p style="text-align: justify;">I&#8217;m using Visual Studio 2017 Community here. The functions I&#8217;ll show below look much the same in previous versions, and they are present in most (if not all) nowadays&#8217; IDEs.</p>
<p style="text-align: justify;">I&#8217;ll show you a real bug from my project and the workflow of fixing it.</p>
<p style="text-align: justify;">First, I&#8217;ve observed such a behavior of an enemy.</p>
<p><img class="aligncenter size-full wp-image-358" src="http://gamesarchitecture.com/wp-content/uploads/2017/08/1.gif" alt="" width="896" height="520" /></p>
<p style="text-align: justify;">I&#8217;ve found that this &#8220;dancing&#8221; happens when the enemy sees the player and then the player runs away (step &#8216;A&#8217;). Since the enemy behavior utilizes <a href="http://gameprogrammingpatterns.com/state.html" target="_blank" rel="noopener">Finite-State Machine</a> pattern, the issue may be related to the one of its states &#8211; <code>ChasingState</code>. I put some breakpoints here and there (step &#8216;B&#8217;).</p>
<p><img class="aligncenter size-full wp-image-360" src="http://gamesarchitecture.com/wp-content/uploads/2017/08/2.gif" alt="" width="960" height="444" /></p>
<p style="text-align: justify;">Breakpoints mark the lines of code where the game should pause, so we will be able to check what are the variables&#8217; values at that point. From these breakpoints we can start going through the code step by step, and see the flow of the application. When we marked the suspicious places already, let&#8217;s launch a debugger.</p>
<p><img class="aligncenter size-full wp-image-361" src="http://gamesarchitecture.com/wp-content/uploads/2017/08/3.png" alt="" width="622" height="125" srcset="http://gamesarchitecture.com/wp-content/uploads/2017/08/3.png 622w, http://gamesarchitecture.com/wp-content/uploads/2017/08/3-300x60.png 300w" sizes="(max-width: 622px) 100vw, 622px" /></p>
<p style="text-align: justify;">Yes, it&#8217;s as simple as that. In other IDE&#8217;s it may look a bit different, and maybe it is called &#8220;Start Debugging&#8221; or so, but you&#8217;ve got the idea.</p>
<p style="text-align: justify;">With a debugger running, we can start our game in the editor, and we repeat the scenario we found during step &#8216;A.&#8217; Then, the game pauses and Visual Studio shows up.</p>
<p><img class="aligncenter size-full wp-image-362" src="http://gamesarchitecture.com/wp-content/uploads/2017/08/4.gif" alt="" width="960" height="444" /></p>
<p style="text-align: justify;">Yellow background highlights the line that will execute. after we move to the very next line. It means that the highlighted one is waiting for execution, yet. In the meantime we can check, what the variables look like.</p>
<p style="text-align: justify;">Using the navigation buttons (or shortcuts) we can move as the code execution moves.</p>
<p><img class="aligncenter size-full wp-image-363" src="http://gamesarchitecture.com/wp-content/uploads/2017/08/5.gif" alt="" width="1026" height="200" /><br />
These buttons are:<br />
<em>Continue (F5)</em> &#8211; resumes the game and stops it on the next breakpoint.<br />
<em>Step Over (F10)</em> &#8211; Executes highlighted line and goes to the next one.<br />
<em>Step Into (F11)</em> &#8211; Same as F10, but when the highlighted line is a method call, it moves onto it.</p>
<p style="text-align: justify;">The next highlighted line will be the first line of this method. I&#8217;ve used these navigation shortcuts to check, what is going on there (step C).</p>
<p style="text-align: justify;"><em>There are two iterations &#8211; during one GIF play, we&#8217;re going into <code>IsCheckpointReached()</code> twice.</em></p>
<p><img class="aligncenter size-full wp-image-364" src="http://gamesarchitecture.com/wp-content/uploads/2017/08/6.gif" alt="" width="960" height="444" /></p>
<p style="text-align: justify;">I stepped into <code>IsCheckpointReached()</code> method by pushing F11 (using F10 would move me to line 119 or 121 depending on if statement&#8217;s result). During the first pass, the first &#8216;if&#8217; statement in <code>IsCheckpointReached()</code> was false. The execution moved to the line 122. I pushed F5 (Continue). After this, the next highlighted line was 117. It was the <code>IsCheckpointReached()</code> again (as in that time I had only one breakpoint set), but this time during the next frame. During the second pass, I&#8217;ve noticed that enemy reached X and Z coordinates (taking the error into account), but not Y. Since this enemy cannot jump or climb, checking Y is just wrong. Knowing that I could move to the step &#8216;D&#8217; &#8211; fixing. I&#8217;ve introduced a new <code>IsCheckpointReached()</code> method (with suffix &#8216;2D&#8217;), for the enemies that cannot jump and I&#8217;ve replaced the current function occurrences (found by clicking on the method and pushing Shift+F12 &#8211; Find All References) with this new version.</p>
<pre class="brush: csharp; collapse: true; light: false; title: IsCheckpointReached2D (click to unfold); toolbar: true; notranslate">    /// 
&amp;lt;summary&amp;gt;
    /// Indicates if the checkpoint has been reached or not. Takes distance error into account.
    /// 2D version of the method. Y axis is not checked.
    /// &amp;lt;/summary&amp;gt;

    /// &amp;lt;param name=&quot;enemy&quot;&amp;gt; Enemy that is checked. &amp;lt;/param&amp;gt;
    /// &amp;lt;param name=&quot;position&quot;&amp;gt; Position of the checkpoint. &amp;lt;/param&amp;gt;
    /// &amp;lt;param name=&quot;errorDistance&quot;&amp;gt; Error distance that can be made for X and Z axis. &amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;True - checkpoint reached, false - otherwise. &amp;lt;/returns&amp;gt;
    protected bool IsCheckpointReached2D(TEnemy enemy, Vector3 position, float errorDistance = 0.0f)
    {
        if (Math.Abs(position.x - enemy.transform.position.x) &amp;gt; errorDistance) return false;
        if (Math.Abs(position.z - enemy.transform.position.z) &amp;gt; errorDistance) return false;

        return true;
    }</pre>
<p style="text-align: justify;">I&#8217;ve checked the code (&#8216;E&#8217;), as I could forget about something (&#8216;something&#8217; is a good word here &#8211; you usually cannot name things you forgot about) and then I&#8217;ve checked the enemies behavior in the game. It worked, character stood still after losing the player out of his sight. Maybe it is not the smartest AI behavior, but now it needs improvements, not fixes.</p>
<p style="text-align: justify;">There is one more tool I wanted to show you. A wonderful one. It&#8217;s called Call Stack and thanks to it, you can see how the game flow looked like until currently highlighted line. It will show you the list of method calls that led to this point.</p>
<p><img class="aligncenter size-full wp-image-366" src="http://gamesarchitecture.com/wp-content/uploads/2017/08/9.gif" alt="" width="946" height="676" /></p>
<p style="text-align: justify;">It is especially useful when you need to know WHY something happened. Why is this method being called?</p>
<p style="text-align: justify;">I&#8217;ve used different code because the above one only had two positions on the list. Also, the new Visual Studio seems to have an issue, so it shows Call Stack twice. Maybe someone should debug it?</p>
<p style="text-align: justify;"><em>There you have it! I hope you like it. I got a lot of amazing feedback recently. I wrote it down as bullet point list, and I read it during writing to improve the text. I appreciate it. Thanks to your suggestions I can do better and better. Join our community, that believes or feels that better coding is crucial for making great games. It is easier this way, once you prove it to yourself. So prove it. I&#8217;ll do my best to help you with that, but I need your voice. Follow Games Architecture however you like and let’s make some great games together.</em></p>
<h1>If you want more:</h1>
<ul>
<li style="text-align: justify;"><a href="https://www.codeproject.com/Articles/359801/plus-powerful-debugging-tricks-with-Visual-Studi" target="_blank" rel="noopener">10+ powerful debugging tricks with Visual Studio</a></li>
<li style="text-align: justify;"><a href="http://gameprogrammingpatterns.com/state.html" target="_blank" rel="noopener">Finite-State Machine</a></li>
</ul>
<div class="grammarly-disable-indicator"></div>
<p>The post <a rel="nofollow" href="http://gamesarchitecture.com/easy-method-to-fix-bugs-in-your-game/">Easy method to fix bugs in your game &#8211; Debugging (Part II)</a> appeared first on <a rel="nofollow" href="http://gamesarchitecture.com">Games Architecture</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamesarchitecture.com/easy-method-to-fix-bugs-in-your-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging (Part I) &#8211; 7 good code practices that will help you in debugging</title>
		<link>http://gamesarchitecture.com/debugging-part-i-7-good-code-practices-that-will-help-you-in-debugging/</link>
		<comments>http://gamesarchitecture.com/debugging-part-i-7-good-code-practices-that-will-help-you-in-debugging/#respond</comments>
		<pubDate>Sun, 02 Jul 2017 14:35:37 +0000</pubDate>
		<dc:creator><![CDATA[Bartosz]]></dc:creator>
				<category><![CDATA[Generic]]></category>
		<category><![CDATA[Good Practices]]></category>

		<guid isPermaLink="false">http://gamesarchitecture.com/?p=249</guid>
		<description><![CDATA[<p>Hey guys! Welcome in the first post in a series about debugging process. This post will be treating about good practices which will help you in the debugging (and/or decrease amount of situations where you&#8217;ll need to do that). The second one will be about debugging process workflow, tools and how to use them in &#8230; </p>
<p class="link-more"><a href="http://gamesarchitecture.com/debugging-part-i-7-good-code-practices-that-will-help-you-in-debugging/" class="more-link">Continue reading<span class="screen-reader-text"> "Debugging (Part I) &#8211; 7 good code practices that will help you in debugging"</span></a></p>
<p>The post <a rel="nofollow" href="http://gamesarchitecture.com/debugging-part-i-7-good-code-practices-that-will-help-you-in-debugging/">Debugging (Part I) &#8211; 7 good code practices that will help you in debugging</a> appeared first on <a rel="nofollow" href="http://gamesarchitecture.com">Games Architecture</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-294" src="http://gamesarchitecture.com/wp-content/uploads/2017/07/2.png" alt="" width="1876" height="440" srcset="http://gamesarchitecture.com/wp-content/uploads/2017/07/2.png 1876w, http://gamesarchitecture.com/wp-content/uploads/2017/07/2-300x70.png 300w, http://gamesarchitecture.com/wp-content/uploads/2017/07/2-768x180.png 768w, http://gamesarchitecture.com/wp-content/uploads/2017/07/2-1024x240.png 1024w" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" /></p>
<p style="text-align: justify;">Hey guys! Welcome in the first post in a series about debugging process. This post will be treating about good practices which will help you in the debugging (and/or decrease amount of situations where you&#8217;ll need to do that). The second one will be about debugging process workflow, tools and how to use them in Visual Studio. I will focus on the most useful ones in my opinion. After these two posts, you should be equipped with a set of tools that, I think, are enough in most cases. This article collects code practices that are good in general, so even if you think you don&#8217;t need debugging (yet), you will make use of them. I decided to write this short series for two reasons. The good practices are good not because the big individuals said so. They are good for a reason. They make our development lives easier. I want to show you their usefulness during the debugging process because debugging is a reasoning about the code. It requires a lot of focus from us. This is a situation where code quality makes a huge difference. These practices make that reasoning easier. The second reason is that I see too many &#8216;easy&#8217; questions on the Stack Overflow, Quora, and Facebook groups. By &#8216;easy&#8217; I mean the problems that are fairly simple to solve during the debugging process. It seems like many of us just don&#8217;t debug at all. Maybe because they just don&#8217;t know how or it seems too hard or time-consuming. They prefer to delegate the problem to the Internet, what is usually more time consuming than debugging it by themselves. And the truth is, after fair amount of practice, you can find it <em>almost</em> easy and <em>almost</em> mechanical. No matter how good your code is, the bugs will happen sooner or later. You will have to get to the source of the problem. This is where the debugging begins and you better be prepared for that. Let&#8217;s get into it!</p>
<p><span id="more-249"></span></p>
<h1>&#8216;Debugging&#8217; series</h1>
<p>Part I &#8211; 7 good code practices that will help you in debugging (this)<br />
<a href="http://gamesarchitecture.com/easy-method-to-fix-bugs-in-your-game/" target="_blank" rel="noopener">Part II &#8211; What to do when Centralized Control does not work?</a></p>
<h1>Content</h1>
<p>1. One instruction per line<br />
2. Keep variables&#8217; span and live time the lowest possible<br />
3. Move complex logical operations to a separate variables/methods<br />
4. If you have getters/setters, don&#8217;t use variables directly<br />
5. Program offensively/defensively<br />
6. Keep nesting as shallow as possible<br />
7. Write comments</p>
<p>Let me start with the quite &#8216;obvious&#8217; advice, but a crucial one.</p>
<h1>1. One instruction per line</h1>
<p style="text-align: justify;">It may sound almost like a cliché, but let&#8217;s compare it to reading a recipe. Below you have two instruction sets.</p>
<ol>
<li>Boil at least 250ml of water, take a cup to which you&#8217;ll need to put a tea bag and when the water boils, pour it into the cup until it&#8217;s full.</li>
</ol>
<p>And&#8230;</p>
<ol>
<li>Boil at least 250ml of water,</li>
<li>Take a cup,</li>
<li>Put the tea bag inside the cup,</li>
<li>Wait for the water to boil,</li>
<li>Pour boiled water to the cup until it&#8217;s full.</li>
</ol>
<p style="text-align: justify;">While making a tea is fairly simple, imagine thai soup recipe (no cheating, you&#8217;re doing a curry paste!) written in one, enormous sentence. Besides easier reading, one instruction per line rule does one more thing. Assume that something went wrong and you can see line number of the error displayed in the console. You immediately know which instruction or method call is guilty before even launching a debugger. Write something like</p>
<pre class="brush: csharp; collapse: false; title: ; notranslate">
{
    // ...
    return ShowMatchSummaryWindow(GetWinner().Name, GetLoser().Name, Time.ToSeconds(GetMatchTime())).Owner == GetHost();
}
</pre>
<p style="text-align: justify;">and you are screwed. You have to launch the debugger first and go through every method call to see where the game blew up exactly. It wastes your time.<br />
Instead, you can write this:</p>
<pre class="brush: csharp; collapse: false; title: ; notranslate">
string winnerName = GetWinner().Name;
string loserName = GetLoser().Name;
int matchTimeInMilliseconds = GetMatchTime();
float matchTimeInSeconds = Time.ToSeconds(matchTimeInMilliseconds);
Player windowOwner = ShowMatchSummaryWindow(winnerName, loserName, matchTimeInSeconds);

bool windowOwnerIsHost = windowOwner == GetHost();
return windowOwnerIsHost;
</pre>
<p style="text-align: justify;">Yes, you can split it to even more lines, that&#8217;s okay. Do it until you know that there is only one thing that can go wrong on every line. Caching the value before returning it may be a good idea as well. Try to avoid things like <code>return MethodCall();</code></p>
<h1>2. Keep variables&#8217; span and live time the lowest possible</h1>
<p style="text-align: justify;">Steve McConnel describes two metrics about variables that you should be aware of in his book, <em>Code Complete</em>. The first one is a <em>live time</em> which says for how many code lines the variable exists. If it has been declared in line 100 and its last use is in line 150, the variable&#8217;s live time equals 51. The second metric is a <em>span</em>. It references to a number of lines between next usages of the variable. Declare a variable in line 10, use it in line 12 and the span will be 1. Then use it again in line 20 and an AVERAGE span will be equal to 4. Minimizing variable&#8217;s live time and average span reduces a chance that somewhere in between the variable will be changed incorrectly. Keeping in mind what the variable is and how it is changed for tens of lines of code is difficult. A couple of such variables and you no longer know what you are reading. Maintaining these metrics low allows you to focus on little pieces of the code and keeping all the variable usages on one screen (in case of local/auto variables).</p>
<p style="text-align: justify;">Here&#8217;s a quote about scope minimizing from the mentioned book:</p>
<blockquote><p>The difference between the &#8220;convenience&#8221; philosophy and the &#8220;intellectual manageability&#8221; philosophy boils down to a difference in emphasis between writing programs and reading them. The maximizing scope might indeed make programs easy to write, but a program in which any routine can use any variable at any time is harder to understand than a program that uses well-factored routines. In such a program, you can&#8217;t understand only one routine; you have to understand all the other routines with which that routine shares global data. Such programs are hard to read, hard to debug, and hard to modify.<br />
&#8211; Steve McConnel, Code Complete 2nd</p></blockquote>
<h1>3. Move complex logical operations to a separate variables/methods</h1>
<p style="text-align: justify;">Look at this:</p>
<pre class="brush: csharp; collapse: false; title: AttackPlayer() method:; notranslate">
public void AttackPlayer(Player player)
{
    if (GetDistanceTo(player) &lt;= rangeOfView &amp;&amp; player.HasCamouflageActive() &amp;&amp; !Physics.Raycast(this, player, rangeOfView))
    {
        // attack the player
    }
}</pre>
<p style="text-align: justify;">In a code like the above one, you have to read through the condition to see what is being checked. If you split it into separate variables and method, the code will look as follows:</p>
<pre class="brush: csharp; collapse: false; title: AttackPlayer() method (improved):; notranslate">
public void AttackPlayer(Player player)
{
    if (IsPlayerVisible(player))
    {
        // attack the player
    }
}

private bool IsPlayerVisible(Player player)
{
    bool isInRangeOfView = GetDistanceTo(player) &lt;= rangeOfView;
    bool hasCamouflageActive = player.HasCamouflageActive();
    bool isCoveredByObstacle = Physics.Raycast(this, player, rangeOfView);

    return isInRangeOfView &amp;&amp; !hasCamouflageActive &amp;&amp; !isCoveredByObstacle;
}</pre>
<p style="text-align: justify;">And we both know that the conditions can become way more complicated than this. By moving them to separate methods and/or naming each condition, you have a code that is easier to understand and fix.</p>
<h1>4. If you have getters/setters, don&#8217;t use variables directly</h1>
<p style="text-align: justify;">In C# they are called Properties, in C++ or any other language they might be just <code>GetVariable()</code> and <code>SetVariable()</code> methods. They are used to access the variable in a proper, cohesive way. Set it or get it and do something else in the middle. This &#8216;something else&#8217; part might be serializing the variable to string format in getter or calling the <code>StrengthChanged()</code> event in <code>SetStrength()</code> so the other parts of the game can react to that change.</p>
<p style="text-align: justify;">If the getters and setters are methods that do only getting and setting and nothing else, it may seem like in the class&#8217; body it doesn&#8217;t matter which one you use. But that is not true. If you need to add &#8216;something&#8217; to the accessors, your job should be done there. When you are using the variable directly, you have to take that change into account in every direct usage of the variable. And you can simply forget to do that.</p>
<p style="text-align: justify;">Secondly, if you&#8217;re looking for all references to the variable (Shift + F12 in VS), you can do this for accessor method only, not for accessor AND for the variable. You&#8217;ve got full list of usages, instead of two scraps of it.</p>
<p style="text-align: justify;">Sometimes you might think that it is better (or even that it is the only way) to use the variable directly, but stop yourself there and take a moment to rethink it. <strong>Very often it is a signal that the accessor method does too much.</strong> Every operation in the getter/setter should be an operation that can be called no matter how the world outside the class looks like or what is the current state of the class. It is a bit of generalization, but just consider it when you feel tempted to use a variable directly while having an accessor for it already. Try to move some of the operations that &#8216;shouldn&#8217;t be there&#8217; out of the accessor and to a separate method. Then, after every accessor usage, you can call this new method if you need it.</p>
<h1>5. Program offensively/defensively</h1>
<p>Your game will be evolving very often during the design&#8217;n&#8217;development process. That&#8217;s the good thing. Preconditions for methods will stop to be fulfilled, you&#8217;ll have null pointer references, types of the variables will change and mismatches will appear. And that&#8217;s a natural order of things. Don&#8217;t be afraid of bugs during the development time. They are very valuable pieces of information about what went wrong. The worst thing you can do is to hide them or try to secure every possible case by writing a lot of additional code. This is a very important subject and I am gonna write a whole post about it, but for now, take this with you</p>
<pre class="brush: csharp; collapse: false; title: Don&#039;t:; notranslate">
    public void RemoveCondition(ConditionType conditionType)
    {
        if (!_initialized) Initialize();

        _conditionsManager.RemoveCondition(conditionType);
    }
</pre>
<p style="text-align: justify;">You&#8217;re admitting here that you didn&#8217;t consider very well when the class can be used and when it shouldn&#8217;t be. When it is not initialized, you made a mistake in the code. Don&#8217;t try to &#8216;patch&#8217; it here and there. Maybe you forgot about the class that initializes this one? The object that initializes it hasn&#8217;t been loaded during the level load? Fix it, don&#8217;t patch or mask it.</p>
<pre class="brush: csharp; collapse: false; title: OMG, OMG, please DON&#039;T:; notranslate">
    public void RemoveCondition(ConditionType conditionType)
    {
        if (!_initialized) return;

        _conditionsManager.RemoveCondition(conditionType);
    }
</pre>
<p style="text-align: justify;">This is so bad and so frequent. Man, you&#8217;ve just called the method and you think that the condition has been removed. Your game is running. And weird things happen. Something went wrong. You don&#8217;t know what. You don&#8217;t know where or when you&#8217;ll see the results. You cannot even be sure that the results on the screen will be related to the original issue at first glance.</p>
<p><img class="aligncenter size-full wp-image-299" src="http://gamesarchitecture.com/wp-content/uploads/2017/07/Zrzut-ekranu-2017-07-02-o-16.25.06.png" alt="" width="1924" height="390" srcset="http://gamesarchitecture.com/wp-content/uploads/2017/07/Zrzut-ekranu-2017-07-02-o-16.25.06.png 1924w, http://gamesarchitecture.com/wp-content/uploads/2017/07/Zrzut-ekranu-2017-07-02-o-16.25.06-300x61.png 300w, http://gamesarchitecture.com/wp-content/uploads/2017/07/Zrzut-ekranu-2017-07-02-o-16.25.06-768x156.png 768w, http://gamesarchitecture.com/wp-content/uploads/2017/07/Zrzut-ekranu-2017-07-02-o-16.25.06-1024x208.png 1024w" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" /></p>
<pre class="brush: csharp; collapse: false; title: Do:; notranslate">
    public void RemoveCondition(ConditionType conditionType)
    {
        if (!_initialized) Debug.LogError(&quot;Conditions Behaviour is not initialized, so the conditions won't be applied. Initialize it first.&quot;);

        _conditionsManager.RemoveCondition(conditionType);
    }
</pre>
<p style="text-align: justify;">Boom! &#8216;Boy, you cannot use me, you forgot about the initialization!&#8217;. Your game is your spaceship. If something is wrong with it, you want to know what it is, so it can be fixed before the launch. You can ignore any checking and let things to be handled by the compiler, but I think it is not quite a good idea either. First, you cannot be sure if the compiler&#8217;s error will be displayed immediately or if the issue will be propagated for a few more methods before doing that. Secondly, you give up on your own, human, messages, written to your future self.</p>
<p style="text-align: justify;">Don&#8217;t secure your game against every possible issue. That&#8217;s a lot of code that is just rubbish. Inform that there is an issue and expect that this particular case will be fixed. You don&#8217;t have to check if an index is within a range right before accessing an array. The compiler will do this for you. But on the other hand, if your compiler&#8217;s message, in this case, is a mess or you want to add additional information to the message &#8211; feel free to do that. Cooperate with the complier. As I said, I will talk about it later.</p>
<p style="text-align: justify;">You can read more about defensive programming <a href="https://en.wikipedia.org/wiki/Defensive_programming" target="_blank" rel="noopener">here</a> and offensive one <a href="https://en.wikipedia.org/wiki/Offensive_programming" target="_blank" rel="noopener">here</a>.<br />
If you found some great articles on these topics, feel free to share them with us in the comments.</p>
<h1>6. Keep nesting as shallow as possible</h1>
<p style="text-align: justify;">There&#8217;s an if in another if, in a for loop in one of the switch cases&#8230; No. Try to keep number of nests below 4.</p>
<p><img class="aligncenter size-full wp-image-293" src="http://gamesarchitecture.com/wp-content/uploads/2017/07/1.png" alt="" width="1984" height="864" srcset="http://gamesarchitecture.com/wp-content/uploads/2017/07/1.png 1984w, http://gamesarchitecture.com/wp-content/uploads/2017/07/1-300x131.png 300w, http://gamesarchitecture.com/wp-content/uploads/2017/07/1-768x334.png 768w, http://gamesarchitecture.com/wp-content/uploads/2017/07/1-1024x446.png 1024w" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" /></p>
<h1>7. Write comments</h1>
<p style="text-align: justify;">Yeah, some people say &#8216;Don&#8217;t write comments, write a code that comments itself&#8217;. Other ones say &#8216;Write a lot of comments&#8217;. Or something like that. But you can be a more balanced. Write comments if you decided to write something &#8216;this way&#8217;, even if it may not be the most obvious way of all. Explain in the comment WHY you&#8217;ve written it like that. &#8216;Because here I don&#8217;t have a reference to that and this method is called before that one, and I didn&#8217;t want to&#8230;&#8217;. Yes, you&#8217;re describing &#8216;why&#8217; you did it, not &#8216;what&#8217; it does. Maybe consider writing comments for more complicated blocks of code. Even if your code is really good, treat it as a possibility to check yourself after reading. Low-level code usually requires more comments than the high-level one.</p>
<p style="text-align: justify;">I think this is a good place to say it loud and clear. It is impossible to write and describe every case where the rule is applicable. To point out every &#8216;unless&#8217; with an exception. It often HAS TO be a little simplified to make the text done and readable by a human being with a job and/or life. But that&#8217;s a good thing. The major goal of mine is to help you to be aware of the rules, good practices and what they are for. The main advice is to THINK what are the solutions for the problem, which one seems the best (and the simplest) NOW (for the current problem). Be AWARE of the good practices and rules, so you can write the code consciously. But the good part of it is, that very often you don&#8217;t have to find the perfect solution. Usually, your decisions are trade-offs anyway. But make them consciously and it will be okay. Calm down. Think. Go for it. And see you soon.</p>
<div class="grammarly-disable-indicator"></div>
<p>The post <a rel="nofollow" href="http://gamesarchitecture.com/debugging-part-i-7-good-code-practices-that-will-help-you-in-debugging/">Debugging (Part I) &#8211; 7 good code practices that will help you in debugging</a> appeared first on <a rel="nofollow" href="http://gamesarchitecture.com">Games Architecture</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gamesarchitecture.com/debugging-part-i-7-good-code-practices-that-will-help-you-in-debugging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
