<?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>Rui Figueira's Blog &#187; UML</title>
	<atom:link href="http://www.crazygaze.com/blog/tag/uml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.crazygaze.com/blog</link>
	<description>Knowing is not enough; we must apply. Willing is not enough; we must do.</description>
	<lastBuildDate>Fri, 24 Sep 2010 16:22:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Simplifying code using states</title>
		<link>http://www.crazygaze.com/blog/2007/12/19/simplifying-code-using-states/</link>
		<comments>http://www.crazygaze.com/blog/2007/12/19/simplifying-code-using-states/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 16:46:23 +0000</pubDate>
		<dc:creator>ruifig</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[code design]]></category>
		<category><![CDATA[State Diagrams]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://www.crazygaze.com/blog/archives/39</guid>
		<description><![CDATA[Lately I have been refactoring czPlayer to add more features and make future maintenance easier. Some parts of the code are almost 10 years old, when I first started playing with sound coding. Needless to say, my code style and quality has improved a lot in 10 years, and it&#8217;s a funny thing to look [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I have been refactoring <a href="http://www.crazygaze.com/blog/czplayer">czPlayer</a> to add more features and make future maintenance easier.<br />
Some parts of the code are almost 10 years old, when I first started playing with sound coding.<br />
Needless to say, my code style and quality has improved a lot in 10 years, and it&#8217;s a funny thing to look at code this old. <img src='http://www.crazygaze.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
As I was refactoring the code, I found a lot of ambiguities and redundancies caused by member variables added over time.<br />
Let me explain&#8230;.<br />
I had a class responsible for MOD processing, and some of the member variables were:</p>
<pre class="brush: cpp;">
class MODModule
{
	// ...
	bool m_loaded;
	bool m_playing;
	bool m_paused;
	bool m_reachedEnd;
};
</pre>
<p>What&#8217;s the problem with this approach ?<br />
Let&#8217;s see&#8230;<br />
Suppose I want to call methods on the MOD object (e.g: Pause(), Resume(), etc)<br />
I need to make some checks in each method.</p>
<p>To pause&#8230;</p>
<pre class="brush: cpp;">
if (!m_loaded || m_playing || m_paused)
	return SOME_ERROR;
m_pause = true;
// ... do pause stuff
</pre>
<p>Then to resume&#8230;</p>
<pre class="brush: cpp;">
if (!m_loaded || !m_paused)
	return SOME_ERROR;
m_paused = false;
// ... do resume stuff
</pre>
<p>When the MOD finishes playing, I needed something like this&#8230;</p>
<pre class="brush: cpp;">
// ... some code that checks for end
m_reachedEnd = true;
//ups... How about m_playing, m_paused?
//We need to change their values, so we don't break code elsewhere
m_playing = false;
m_pause = false;
</pre>
<p>And similar things when playing, stopping, etc.<br />
See the problems?</p>
<ul>
<li>We need checks for multiples variables</li>
<li>We need to make sure the variables have valid values in relation to each other</li>
<li>What if we want to add an extra state? For example, &#8220;bool m_fading&#8221;. We would need to examine a lot of code to check how the other variables are used, and hardwire this new one somewhere. Every time you need to add an extra variable, you&#8217;ll need to check more and more code to make sure everything is ok.</li>
</ul>
<p>Now look at the meaning of the variables. They are mostly mutually exclusive.</p>
<ul>
<li>If it&#8217;s playing, it can&#8217;t be paused and hasn&#8217;t reached the end.</li>
<li>If it&#8217;s paused, it&#8217;s not playing, and hasn&#8217;t reached the end yet.</li>
<li>&#8230; and so on&#8230;.</li>
</ul>
<p>The more data members a class has, the harder it gets to keep the object in a valid state, so, the less data to manage, the better. Less maintenance and less bugs.</p>
<p>One technique I use a lot when trying to simplify code is to look at it with state diagrams whenever possible.<br />
Here it is what happens (kind of) with instances of the MODModule class:</p>
<p><a title="mod_states.png" href="http://www.crazygaze.com/blog/wp-content/uploads/2007/12/mod_states.png"><img src="http://www.crazygaze.com/blog/wp-content/uploads/2007/12/mod_states.png" alt="mod_states.png" /></a></p>
<p>I can easily translate it to code with enums.</p>
<pre class="brush: cpp;">
enum MODState
{
    MOD_STATE_NOT_LOADED,
    MOD_STATE_STOPPED,
    MOD_STATE_PLAYING,
    MOD_STATE_PAUSED,
    MOD_STATE_REACHED_END
};

class MODModule
{
    // ...
    MODState m_state; // Now we have only one variable
};
</pre>
<p>This approach is a lot easier to maintain, and less error prone.<br />
Now the code in most functions become something like this:</p>
<p>To play&#8230;</p>
<pre class="brush: cpp;">
if (m_state!=MOD_STATE_STOPPED)
    return SOME_ERROR;
m_state = MOD_STATE_PLAYING;
// ... do play stuff
</pre>
<p>To pause&#8230;</p>
<pre class="brush: cpp;">
if (m_state!=MOD_STATE_PLAYING)
    return SOME_ERROR;
m_state = MOD_STATE_PAUSED;
// ... do pause stuff
</pre>
<p>To resume&#8230;</p>
<pre class="brush: cpp;">
if (m_state!=MOD_STATE_PAUSED)
    return SOME_ERROR;
m_state = MOD_STATE_PLAYING;
// ... do resume stuff
</pre>
<p>To stop&#8230;</p>
<pre class="brush: cpp;">
// We cannot stop if it's not even loaded yet, or it's already stopped
if (m_state==MOD_STATE_NOT_LOADED || m_state==MOD_STATE_STOPPED)
    return SOME_ERROR;
m_state = MOD_STATE_STOPPED;
// ... do stop stuff
</pre>
<p>The code may look similar, but it&#8217;s a lot less error prone, easier to follow and maintain.<br />
Looking at the State Diagram, I could easily squeeze in a &#8220;Fading&#8221; state and understand the conditions to enter or leave that state.</p>
<p>As a bonus, using enums will enforce more rules and help you out with errors the compiler might catch. Always prefer compile-time errors over run-time errors. <img src='http://www.crazygaze.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazygaze.com/blog/2007/12/19/simplifying-code-using-states/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Good UML Design Tool</title>
		<link>http://www.crazygaze.com/blog/2007/11/16/good-uml-design-tool/</link>
		<comments>http://www.crazygaze.com/blog/2007/11/16/good-uml-design-tool/#comments</comments>
		<pubDate>Fri, 16 Nov 2007 17:32:34 +0000</pubDate>
		<dc:creator>ruifig</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://www.crazygaze.com/blog/archives/31</guid>
		<description><![CDATA[In some previous posts I talked a little about UML, but I never talked about what tool I use. I have already tried a few. - BOUML - UML Pad - Rational Rose . Only used some years ago, and never liked it. - Visual Paradigm for UML So far, and by far&#8230; the one [...]]]></description>
			<content:encoded><![CDATA[<p><!--adsense--><br />
In some previous posts I talked a little about UML, but I never talked about what tool I use.</p>
<p>I have already tried a few.</p>
<p>- <a href="http://bouml.free.fr/index.html">BOUML</a></p>
<p>- <a href="http://web.tiscali.it/ggbhome/umlpad/umlpad.htm">UML Pad</a></p>
<p>- Rational Rose . Only used some years ago, and never liked it.</p>
<p>- <a href="http://www.visual-paradigm.com/">Visual Paradigm for UML</a></p>
<p>So far, and by far&#8230; the one I like the most is <a href="http://www.visual-paradigm.com/">Visual Paradigm for UML</a> . The first time I looked at it, it scared me a little, with all the buttons, and functionality. But its quite a complete package. Great documentation, somewhat clutter free windows (considering the amount of functionality), and an amazing intuitive interface, that can get you going in no time.</p>
<p>For example, in a State Machine Diagram, just place the initial state, and you can do most stuff from there without ever leaving the design area, simply pulling the other states from that one. Mouse gestures really help with productivity. First time I discovered those, it reminded me of &#8220;Black and White&#8221;. Depending on the kind of diagram we are working on, we&#8217;ll have some specific gestures available. For example, in a Class Diagram, we have a gestures to create classes, packages, add operations, attributes, and so on.</p>
<p>Most gestures are easy to remember, but to make things even easier, we even have a fast lookup window:</p>
<p><img src="http://www.crazygaze.com/blog/wp-content/uploads/2007/11/tj200711161728-1.jpg" alt="visual-paradigm_1.png (521x339 pixels)" height="339" hspace="1" vspace="1" width="521" /></p>
<p>That menu option will show you the available mouse gestures for each diagram type, like this:</p>
<p><img src="http://www.crazygaze.com/blog/wp-content/uploads/2007/11/tj200711161728-2.jpg" alt="visual-paradigm_2.png (410x511 pixels)" height="511" hspace="1" vspace="1" width="410" /></p>
<p>Thumbs up for Visual Paradigm. Really intuitive interface, that can really boost productivity.</p>
<p>Of course it has a lot more features, but the productivity boost was the most interesting to me.</p>
<p>If you&#8217;re looking for a good UML tool, give it a try. Maybe you&#8217;ll like it too. You can always use the Community Edition for a while, which is free for non-commercial use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazygaze.com/blog/2007/11/16/good-uml-design-tool/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>UML Applied &#8211; Second Edition</title>
		<link>http://www.crazygaze.com/blog/2007/06/06/uml-applied-second-edition/</link>
		<comments>http://www.crazygaze.com/blog/2007/06/06/uml-applied-second-edition/#comments</comments>
		<pubDate>Wed, 06 Jun 2007 14:34:12 +0000</pubDate>
		<dc:creator>ruifig</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://www.crazygaze.com/blog/archives/15</guid>
		<description><![CDATA[For some of the programming tasks I&#8217;ve been doing, I found myself planning things with UML State Diagrams. I really helped to understand some details, find problems, and follow the specifications. Since I haven&#8217;t been using UML for some time, I took out of the dust a book I downloaded and printed back in 2001 [...]]]></description>
			<content:encoded><![CDATA[<p>For some of the programming tasks I&#8217;ve been doing, I found myself planning things with UML State Diagrams. I really helped to understand some details, find problems, and follow the specifications.  Since I haven&#8217;t been using UML for some time, I took out of the dust a book I downloaded and printed back in 2001 (I think) from <a href="http://www.ariadnetraining.co.uk/">Ariadne Training</a> .  Since I always liked that book, I noticed they already have the second edition available.<br />
That book is actually a course manual, but I always found the first edition quite good even without attending the course. Not sure about this second edition, but I&#8217;ve already printed it anyway.<br />
Here goes the link to the PDF:<br />
<a href="http://www.ariadnetraining.co.uk/ariadnetraining.co.uk/downloads.htm">http://www.ariadnetraining.co.uk/ariadnetraining.co.uk/downloads.htm</a><br />
Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazygaze.com/blog/2007/06/06/uml-applied-second-edition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

