<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: How Can I Design Code Better?</title>
	<atom:link href="http://gbgames.com/blog/2006/03/how-can-i-design-code-better/feed/" rel="self" type="application/rss+xml" />
	<link>http://gbgames.com/blog/2006/03/how-can-i-design-code-better/</link>
	<description>An Indie Game Developer's somewhat interesting thoughts</description>
	<lastBuildDate>Wed, 08 Feb 2012 22:27:01 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: GBGames</title>
		<link>http://gbgames.com/blog/2006/03/how-can-i-design-code-better/comment-page-1/#comment-12457</link>
		<dc:creator>GBGames</dc:creator>
		<pubDate>Mon, 06 Mar 2006 20:41:38 +0000</pubDate>
		<guid isPermaLink="false">http://gbgames.com/blog/?p=329#comment-12457</guid>
		<description>Thanks for your advice!  I have some things to think about. B-)</description>
		<content:encoded><![CDATA[<p>Thanks for your advice!  I have some things to think about. B-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neil</title>
		<link>http://gbgames.com/blog/2006/03/how-can-i-design-code-better/comment-page-1/#comment-12456</link>
		<dc:creator>Neil</dc:creator>
		<pubDate>Mon, 06 Mar 2006 19:19:07 +0000</pubDate>
		<guid isPermaLink="false">http://gbgames.com/blog/?p=329#comment-12456</guid>
		<description>&quot;Pulling data out of an object to decide what to do is just procedural programming, whereas telling the object to do something and letting it figure out for itself how to do so would be more appropriate.&quot;

Yikes!  Please don&#039;t take that as 100% true!  If you design all your classes that way, you will find that some features become extremely difficult to implement in a game.

For example, if you have a Door and a Key object in your game, how do you use the key to unlock the door?  Is it Key-&gt;unlockDoor( door ) or is it Door-&gt;unlock( Key ) ?  What if this door is in a room in which all locks do not function?  So, all unlock() functions must fail.  How do you code that feature?  Do you add another parameter to Door&#039;s unlock() method?  Do you keep adding parameters to all methods in all object classes whenever you want to add a new feature?  Maintenance nightmare!  Yes, it is good academic object-oriented design, but it can be bad game design.

I say keep some classes as data containers (nouns), and have other classes perform the actions (verbs).  A game can be seen as a view of data.  Merging the view of the data into the data itself is a bad idea, in my experience.

Good luck!
Neil</description>
		<content:encoded><![CDATA[<p>&#8220;Pulling data out of an object to decide what to do is just procedural programming, whereas telling the object to do something and letting it figure out for itself how to do so would be more appropriate.&#8221;</p>
<p>Yikes!  Please don&#8217;t take that as 100% true!  If you design all your classes that way, you will find that some features become extremely difficult to implement in a game.</p>
<p>For example, if you have a Door and a Key object in your game, how do you use the key to unlock the door?  Is it Key-&gt;unlockDoor( door ) or is it Door-&gt;unlock( Key ) ?  What if this door is in a room in which all locks do not function?  So, all unlock() functions must fail.  How do you code that feature?  Do you add another parameter to Door&#8217;s unlock() method?  Do you keep adding parameters to all methods in all object classes whenever you want to add a new feature?  Maintenance nightmare!  Yes, it is good academic object-oriented design, but it can be bad game design.</p>
<p>I say keep some classes as data containers (nouns), and have other classes perform the actions (verbs).  A game can be seen as a view of data.  Merging the view of the data into the data itself is a bad idea, in my experience.</p>
<p>Good luck!<br />
Neil</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Larry Garfield</title>
		<link>http://gbgames.com/blog/2006/03/how-can-i-design-code-better/comment-page-1/#comment-12174</link>
		<dc:creator>Larry Garfield</dc:creator>
		<pubDate>Thu, 02 Mar 2006 04:52:32 +0000</pubDate>
		<guid isPermaLink="false">http://gbgames.com/blog/?p=329#comment-12174</guid>
		<description>In my experience, improvement in your coding skills is not incremental but comes in jumps.  I can even remember exactly where I was when certain things just suddenly made sense to me.  It really is an epiphany, and my code after that is almost magically more elegant.

So, you want to try and increase the chances you have for such epiphanies.  I find that reading *good* open source code and listening to others explain something, even if it&#039;s something I supposedly already know, really helps.  Also, explaining it to others.  You have to really understand something to teach it, and trying to figure out how best to teach it can help you internalize it.

For instance, my native language, so to speak, is PHP.  I had been working on finding good tips on how to write a good modular, plugin architecture and come up dry, and my multiple own attempts hadn&#039;t gotten very far.  Then I stumbled across &lt;a href=&quot;http://drupal.org&quot; rel=&quot;nofollow&quot;&gt;Drupal&lt;/a&gt;.  Suddenly, after a week or two of on-and-off studying, (fortunately it&#039;s a very well documented project), I realized how to do function-level polymorphism in PHP and how to use objects and arrays &quot;properly&quot;.  Even when I&#039;m not working on Drupal now, my own code follows similar patterns and is almost gleefully elegant in places.  (You know you&#039;re doing something write when you can look at it and go &quot;wow, that&#039;s so elegant&quot; rather than &quot;wow, that&#039;s so cool.)

Also helpful is to mimicry.  Find an API that you like, or think is well implemented.  Then try to reimplement it yourself.  It doesn&#039;t have to be an exact copy, and you can look at the actual code at times to figure out how they handled some particularly thorny issue, but try to make it your own.  It gives you a good target to shoot for, but a challenge to figure out how to make it elegant.  You&#039;ll probably end up with something that&#039;s similar to but not identical to (in API or internals) what you were copying.  If you get a great idea in the process, go ahead and do it.  Even if the code you end up writing you never use, it&#039;s still a great learning experience.  (In my case, I had to write a new CMS at work, fast.  What I ended up with is not Drupal, but is very Drupal-esque, and in some places I find it even more elegant than Drupal&#039;s code after I&#039;ve gone back and looked at it.)

The Ruby folks are big on &quot;beautiful code&quot;.  I find that a waste of time.  &quot;Elegant code&quot;, however, is something to shoot for.  I define elegant code as being code that logically makes sense to read, accounts for all possibilities without funky if-then or switch statements, and just &quot;feels clean&quot;.  When you write a routine that seems to &quot;just work&quot; in your mind, and does so in code, you just know it in your gut.  Let yourself take the time to write such code, and you&#039;ll find your quality improving over time.

Much of that is language-specific, of course.  What makes something elegant in PHP (arrays, implode, and explode are AWESOME) is not what makes it elegant in Perl (if it can&#039;t be done in a regex it&#039;s not worth doing) is not what makes it elegant in Java (switch should never be used, and else-if is the enemy) is not what makes it elegant in C++ (uh, not really sure :-) )

Another refactoring guideline I use is what I call the Hawking Rule.  Steven Hawking (at least I think it was him; it could have been Einstein :-) ) once noted that the most unlikely number in the universe is 2.  Something can be unique, and occur only once.  Or it could occur lots and lots of times.  But for something to occur twice and only twice?  Extremely unlikely.  (In context, it means that either we&#039;re the only intelligent species in the universe or there&#039;s a crapload of them out there, but there won&#039;t be just one more.)

In code, that means the first time you write something, go ahead and write it inline if it makes sense to.  Then if you find yourself writing it again elsewhere, or something very similar, 99% chance it means you&#039;re going to write it a 3rd, 4th, and 497th time.  STOP RIGHT THERE, and refactor it into a general routine (function, object, method, library, whatever your language does with such things).  The extra 30 minutes you spend over copying and pasting it &quot;just one time&quot; will save you days of debugging next month.  

You know, I really should just put these long entries in my own blog and trackback to you. :-)</description>
		<content:encoded><![CDATA[<p>In my experience, improvement in your coding skills is not incremental but comes in jumps.  I can even remember exactly where I was when certain things just suddenly made sense to me.  It really is an epiphany, and my code after that is almost magically more elegant.</p>
<p>So, you want to try and increase the chances you have for such epiphanies.  I find that reading *good* open source code and listening to others explain something, even if it&#8217;s something I supposedly already know, really helps.  Also, explaining it to others.  You have to really understand something to teach it, and trying to figure out how best to teach it can help you internalize it.</p>
<p>For instance, my native language, so to speak, is PHP.  I had been working on finding good tips on how to write a good modular, plugin architecture and come up dry, and my multiple own attempts hadn&#8217;t gotten very far.  Then I stumbled across <a href="http://drupal.org" rel="nofollow">Drupal</a>.  Suddenly, after a week or two of on-and-off studying, (fortunately it&#8217;s a very well documented project), I realized how to do function-level polymorphism in PHP and how to use objects and arrays &#8220;properly&#8221;.  Even when I&#8217;m not working on Drupal now, my own code follows similar patterns and is almost gleefully elegant in places.  (You know you&#8217;re doing something write when you can look at it and go &#8220;wow, that&#8217;s so elegant&#8221; rather than &#8220;wow, that&#8217;s so cool.)</p>
<p>Also helpful is to mimicry.  Find an API that you like, or think is well implemented.  Then try to reimplement it yourself.  It doesn&#8217;t have to be an exact copy, and you can look at the actual code at times to figure out how they handled some particularly thorny issue, but try to make it your own.  It gives you a good target to shoot for, but a challenge to figure out how to make it elegant.  You&#8217;ll probably end up with something that&#8217;s similar to but not identical to (in API or internals) what you were copying.  If you get a great idea in the process, go ahead and do it.  Even if the code you end up writing you never use, it&#8217;s still a great learning experience.  (In my case, I had to write a new CMS at work, fast.  What I ended up with is not Drupal, but is very Drupal-esque, and in some places I find it even more elegant than Drupal&#8217;s code after I&#8217;ve gone back and looked at it.)</p>
<p>The Ruby folks are big on &#8220;beautiful code&#8221;.  I find that a waste of time.  &#8220;Elegant code&#8221;, however, is something to shoot for.  I define elegant code as being code that logically makes sense to read, accounts for all possibilities without funky if-then or switch statements, and just &#8220;feels clean&#8221;.  When you write a routine that seems to &#8220;just work&#8221; in your mind, and does so in code, you just know it in your gut.  Let yourself take the time to write such code, and you&#8217;ll find your quality improving over time.</p>
<p>Much of that is language-specific, of course.  What makes something elegant in PHP (arrays, implode, and explode are AWESOME) is not what makes it elegant in Perl (if it can&#8217;t be done in a regex it&#8217;s not worth doing) is not what makes it elegant in Java (switch should never be used, and else-if is the enemy) is not what makes it elegant in C++ (uh, not really sure <img src='http://gbgames.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  )</p>
<p>Another refactoring guideline I use is what I call the Hawking Rule.  Steven Hawking (at least I think it was him; it could have been Einstein <img src='http://gbgames.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  ) once noted that the most unlikely number in the universe is 2.  Something can be unique, and occur only once.  Or it could occur lots and lots of times.  But for something to occur twice and only twice?  Extremely unlikely.  (In context, it means that either we&#8217;re the only intelligent species in the universe or there&#8217;s a crapload of them out there, but there won&#8217;t be just one more.)</p>
<p>In code, that means the first time you write something, go ahead and write it inline if it makes sense to.  Then if you find yourself writing it again elsewhere, or something very similar, 99% chance it means you&#8217;re going to write it a 3rd, 4th, and 497th time.  STOP RIGHT THERE, and refactor it into a general routine (function, object, method, library, whatever your language does with such things).  The extra 30 minutes you spend over copying and pasting it &#8220;just one time&#8221; will save you days of debugging next month.  </p>
<p>You know, I really should just put these long entries in my own blog and trackback to you. <img src='http://gbgames.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: GBGames</title>
		<link>http://gbgames.com/blog/2006/03/how-can-i-design-code-better/comment-page-1/#comment-12157</link>
		<dc:creator>GBGames</dc:creator>
		<pubDate>Wed, 01 Mar 2006 22:23:48 +0000</pubDate>
		<guid isPermaLink="false">http://gbgames.com/blog/?p=329#comment-12157</guid>
		<description>People have mentioned Python as an easy-to-use game development language, and I will look into it once Oracle&#039;s Eye is finished.   

As for my C++ code, I suppose my fears aren&#039;t very specific.  Sometimes it is easy to look at my code and say, &quot;Oh, no! What if I am doing it wrong?!?&quot;  

And such questions aren&#039;t very helpful at all.  I should make an effort to ask &quot;Is there a way I can improve this code?&quot;   I think there will be less freaking out and more productivity as a result. B-)</description>
		<content:encoded><![CDATA[<p>People have mentioned Python as an easy-to-use game development language, and I will look into it once Oracle&#8217;s Eye is finished.   </p>
<p>As for my C++ code, I suppose my fears aren&#8217;t very specific.  Sometimes it is easy to look at my code and say, &#8220;Oh, no! What if I am doing it wrong?!?&#8221;  </p>
<p>And such questions aren&#8217;t very helpful at all.  I should make an effort to ask &#8220;Is there a way I can improve this code?&#8221;   I think there will be less freaking out and more productivity as a result. B-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William Willing</title>
		<link>http://gbgames.com/blog/2006/03/how-can-i-design-code-better/comment-page-1/#comment-12156</link>
		<dc:creator>William Willing</dc:creator>
		<pubDate>Wed, 01 Mar 2006 21:03:22 +0000</pubDate>
		<guid isPermaLink="false">http://gbgames.com/blog/?p=329#comment-12156</guid>
		<description>Code design is tricky, and in my experience it&#039;s easier to overdo, than to underdo (if that&#039;s a word...). I&#039;m leaning more and more towards the &#039;just get it to work&#039; philosophy and I&#039;m adjusting my toolset for that. I find that C++ code really needs a fair bit of code design, otherwise you get stuck pretty quickly. So these days, I do my game programming in Lua mostly, and that makes it much easier.

My advice to you would be: find the coding style that works best for &lt;em&gt;you&lt;/em&gt;. The benefit you have as a lone wolf is that you don&#039;t have to design your code for others to work with. If you like to structure your code in a certain way - even if others may frown on it - then go for it; you don&#039;t need to defend your coding style to others. Think only of what &lt;em&gt;you&lt;/em&gt; want from you code. When you&#039;re alone, it pays to be egocentric. :-)</description>
		<content:encoded><![CDATA[<p>Code design is tricky, and in my experience it&#8217;s easier to overdo, than to underdo (if that&#8217;s a word&#8230;). I&#8217;m leaning more and more towards the &#8216;just get it to work&#8217; philosophy and I&#8217;m adjusting my toolset for that. I find that C++ code really needs a fair bit of code design, otherwise you get stuck pretty quickly. So these days, I do my game programming in Lua mostly, and that makes it much easier.</p>
<p>My advice to you would be: find the coding style that works best for <em>you</em>. The benefit you have as a lone wolf is that you don&#8217;t have to design your code for others to work with. If you like to structure your code in a certain way &#8211; even if others may frown on it &#8211; then go for it; you don&#8217;t need to defend your coding style to others. Think only of what <em>you</em> want from you code. When you&#8217;re alone, it pays to be egocentric. <img src='http://gbgames.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: GBGames</title>
		<link>http://gbgames.com/blog/2006/03/how-can-i-design-code-better/comment-page-1/#comment-12155</link>
		<dc:creator>GBGames</dc:creator>
		<pubDate>Wed, 01 Mar 2006 18:45:01 +0000</pubDate>
		<guid isPermaLink="false">http://gbgames.com/blog/?p=329#comment-12155</guid>
		<description>I realized after reading through the website  that I had already seen &lt;a title=&quot;The AGG Project&quot; href=&quot;http://www.antigrain.com&quot; rel=&quot;nofollow&quot;&gt;Anti-Grain&lt;/a&gt; before.  Someone mentioned it months ago, although I don&#039;t remember where.   I&#039;ll definitely look into the source.</description>
		<content:encoded><![CDATA[<p>I realized after reading through the website  that I had already seen <a title="The AGG Project" href="http://www.antigrain.com" rel="nofollow">Anti-Grain</a> before.  Someone mentioned it months ago, although I don&#8217;t remember where.   I&#8217;ll definitely look into the source.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Billy Zelsnack</title>
		<link>http://gbgames.com/blog/2006/03/how-can-i-design-code-better/comment-page-1/#comment-12149</link>
		<dc:creator>Billy Zelsnack</dc:creator>
		<pubDate>Wed, 01 Mar 2006 17:07:25 +0000</pubDate>
		<guid isPermaLink="false">http://gbgames.com/blog/?p=329#comment-12149</guid>
		<description>The lone wolf coder has a very big advantage in growing his programming skills over when working with others. Working in a shop does increase your skills, but it will eventually limit your skills growth rate. The pure weight of the huge projects and large teams slow your progress.

This all becomes incredibly obvious as you grow and you come across less and less code that is up to your own personal standards, let alone code that you can learn from.

So.. Don&#039;t worry so much about working on your own.

btw. If you want to look at some very nice high-quality code (IMO) in an open project, take a look at antigrain.</description>
		<content:encoded><![CDATA[<p>The lone wolf coder has a very big advantage in growing his programming skills over when working with others. Working in a shop does increase your skills, but it will eventually limit your skills growth rate. The pure weight of the huge projects and large teams slow your progress.</p>
<p>This all becomes incredibly obvious as you grow and you come across less and less code that is up to your own personal standards, let alone code that you can learn from.</p>
<p>So.. Don&#8217;t worry so much about working on your own.</p>
<p>btw. If you want to look at some very nice high-quality code (IMO) in an open project, take a look at antigrain.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: GBGames</title>
		<link>http://gbgames.com/blog/2006/03/how-can-i-design-code-better/comment-page-1/#comment-12143</link>
		<dc:creator>GBGames</dc:creator>
		<pubDate>Wed, 01 Mar 2006 15:47:50 +0000</pubDate>
		<guid isPermaLink="false">http://gbgames.com/blog/?p=329#comment-12143</guid>
		<description>For some time I&#039;ve been downloading the source of game projects in varying states of maturity.  Sometimes they can be enlightening, but other times they are exercises in frustration.  

While I can expect documentation in code to be sparse, it can be like reading an old work of Shakespeare without notes on the left page to help you along.  If you get it already, there is no problem, but if you are exposed to it for the first time, it can be a real chore to get through.  

I suppose it is just a matter of experience.  I&#039;ll learn what works and what doesn&#039;t.   Five years from now, I&#039;ll probably look back at my concerns today and laugh.</description>
		<content:encoded><![CDATA[<p>For some time I&#8217;ve been downloading the source of game projects in varying states of maturity.  Sometimes they can be enlightening, but other times they are exercises in frustration.  </p>
<p>While I can expect documentation in code to be sparse, it can be like reading an old work of Shakespeare without notes on the left page to help you along.  If you get it already, there is no problem, but if you are exposed to it for the first time, it can be a real chore to get through.  </p>
<p>I suppose it is just a matter of experience.  I&#8217;ll learn what works and what doesn&#8217;t.   Five years from now, I&#8217;ll probably look back at my concerns today and laugh.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ziggy</title>
		<link>http://gbgames.com/blog/2006/03/how-can-i-design-code-better/comment-page-1/#comment-12132</link>
		<dc:creator>Ziggy</dc:creator>
		<pubDate>Wed, 01 Mar 2006 09:39:29 +0000</pubDate>
		<guid isPermaLink="false">http://gbgames.com/blog/?p=329#comment-12132</guid>
		<description>Hi,

I would recommend not to focus to much on &quot;perfect&quot; code. The search for this holy grail prevents one to learn from experience. During development, you will find parts that are not as flexible as you would want, or cause more bugs than you can tolerate. Then you can find a solution, using development forums etc. 

And working with other developers does work. I think reading others code will help if working with others is not an option.

Ziggy</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I would recommend not to focus to much on &#8220;perfect&#8221; code. The search for this holy grail prevents one to learn from experience. During development, you will find parts that are not as flexible as you would want, or cause more bugs than you can tolerate. Then you can find a solution, using development forums etc. </p>
<p>And working with other developers does work. I think reading others code will help if working with others is not an option.</p>
<p>Ziggy</p>
]]></content:encoded>
	</item>
</channel>
</rss>

