<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.jenitennison.com/blog" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>coding</title>
 <link>http://www.jenitennison.com/blog/taxonomy/term/30</link>
 <description>The taxonomy view with a depth of 0.</description>
 <language>en</language>
<item>
 <title>Programming robots the feminine way?</title>
 <link>http://www.jenitennison.com/blog/node/44</link>
 <description>&lt;p&gt;I recently filled in a &lt;a href=&quot;https://www.surveymonkey.com/s.aspx?sm=yrLrPneVwzAm50dPNax3gQ_3d_3d&quot; title=&quot;Survey on Robots in Computer Science Education&quot;&gt;questionnaire&lt;/a&gt; that asked about the use of robots in teaching programming. (You can win a robot!) Some of the questions seemed to be particularly about attracting women into the field; I guess the thinking is that programming something that does something in the real world is more engaging (particularly for women?) than doing artificial exercises in linked list manipulation. Or something.&lt;/p&gt;

&lt;p&gt;I like programming robots as much as the next geek, and am the proud owner of two regular &lt;a href=&quot;http://mindstorms.lego.com/&quot; title=&quot;Lego Mindstorms NXT&quot;&gt;Lego Mindstorms&lt;/a&gt; kits as well as a less complex, but more evil, Dark Side Developers Kit. Thinking around this, it struck me that there are two classes of projects you can do with robots:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;directive&lt;/strong&gt; program, where you tell the robot exactly what to do (go forward for 5 seconds, turn, forward for 2 seconds etc.)&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;facilitative&lt;/strong&gt; program, where you define the feedback between sensors and motors, then just let the robot go&lt;/li&gt;
&lt;/ul&gt;

&lt;!--break--&gt;

&lt;p&gt;It&amp;#8217;s a bit of a stretch, but these two classes of projects seem like they might be associated with masculine and feminine approaches to dealing with children. There was a great &lt;a href=&quot;http://www.bbc.co.uk/parenting/tv_and_radio/child_of_our_time/&quot; title=&quot;BBC: Child of Our Time&quot;&gt;Child of Our Time&lt;/a&gt; episode a couple of years ago where parents helped their children draw a house on an &lt;a href=&quot;http://en.wikipedia.org/wiki/Etch_A_Sketch&quot; title=&quot;Wikipedia: Etch A Sketch&quot;&gt;Etch A Sketch&lt;/a&gt;. The fathers basically took over the controls &amp;#8212; &amp;#8220;turn yours&amp;#8230; keep going&amp;#8230; stop&amp;#8221; &amp;#8212; whereas the mothers let the child do it while uttering general words of encouragement. Both approaches are absolutely necessary for a child to learn how to do it on their own: they can&amp;#8217;t know what to do unless they&amp;#8217;re told, and they can&amp;#8217;t learn to do it themselves unless they&amp;#8217;re given space to try.&lt;/p&gt;

&lt;p&gt;So I wonder whether programming in a directive way is more attractive to masculine people and programming in a facilitative way is more attractive to feminine people. Of course it&amp;#8217;s kind of hard to simply &lt;em&gt;encourage&lt;/em&gt; a computer to do something, but I certainly find it more engaging to see how &lt;em&gt;little&lt;/em&gt; I need to tell a robot to do in order to get interesting behaviour. My favourite robot projects were creating ones that would locate and hide in the darkest part of a room (through a combination of random and goal-oriented movement), and setting up two kits to &amp;#8220;sing&amp;#8221; with each other (each responding to the others&amp;#8217; song in a feedback loop). In other words, simple programs that elicit complex behaviour simply by being used in a complex environment. (XSLT programming can be like this as well: the art of creating complex XSL-FO/HTML from complex XML with as little intervention as possible.)&lt;/p&gt;

&lt;p&gt;Actually, I don&amp;#8217;t think that simply programming robots would be any more attractive to women than other kinds of programming. What matters, I think, is whether there&amp;#8217;s a real &lt;em&gt;task&lt;/em&gt; to achieve. So getting a robot to do something useful, like vacuuming or tidying away toys, would be attractive. But equally so would designing a diary application, or a community website. Programming in the abstract isn&amp;#8217;t exciting, but being able to do something with a program is. (And surely it can&amp;#8217;t just be women who feel like that?)&lt;/p&gt;
</description>
 <comments>http://www.jenitennison.com/blog/node/44#comments</comments>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/30">coding</category>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/25">equality</category>
 <pubDate>Sun, 22 Jul 2007 22:26:29 +0100</pubDate>
 <dc:creator>Jeni</dc:creator>
 <guid isPermaLink="false">44 at http://www.jenitennison.com/blog</guid>
</item>
<item>
 <title>Copy-and-paste coding</title>
 <link>http://www.jenitennison.com/blog/node/37</link>
 <description>&lt;p&gt;Copy-and-paste coding drives me crazy. Here&amp;#8217;s some Javascript that I was passed today that illustrates the problem:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if (navigator.userAgent.toLowerCase().indexOf(&quot;safari&quot;) &amp;gt;= 0) {
  if (pageSize &amp;gt; 1000000) {
    hideLink();
  }
} else if (navigator.userAgent.toLowerCase().indexOf(&quot;opera&quot;) &amp;gt;= 0) {
  if (pageSize &amp;gt; 1000000) {
    hideLink();
  }
} else if (navigator.userAgent.toLowerCase().indexOf(&quot;firefox&quot;) &amp;gt;= 0) {
  if (pageSize &amp;gt; 1000000) {
    hideLink();
  }
} else if (navigator.userAgent.toLowerCase().indexOf(&quot;netscape&quot;) &amp;gt;= 0) {
  if (pageSize &amp;gt; 1000000) {
    hideLink();
  }
} else if (navigator.userAgent.toLowerCase().indexOf(&quot;msie&quot;) &amp;gt;= 0) {
  if (pageSize &amp;gt; 1000000) {
    hideLink();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The person who wrote this probably started with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if (navigator.userAgent.toLowerCase().indexOf(&quot;firefox&quot;) &amp;gt;= 0) {
  if (pageSize &amp;gt; 1000000) {
    hideLink();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;then decided to expand the code to test other browsers as well. So what did they do? They copied the lines and pasted them, edited a bit, pasted again, edited a bit, and so on, until they had code to cover all the situations, ending up with 21 lines of code: 15 are exact copies and 5 are very similar to each other. Ho boy.&lt;/p&gt;

&lt;p&gt;This is an example of copy-and-paste coding, and it has three disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;it bloats your code, and as we all know &lt;a href=&quot;http://www.codinghorror.com/blog/archives/000878.html&quot; title=&quot;Coding Horror: The Best Code is No Code At All&quot;&gt;the best code is no code at all&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;it makes your code less efficient, because you repeatedly do the same thing; for example, the above code converts the user agent string to lower case every time it hits one of the main conditions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;it makes your code much harder to maintain: imagine you have to maintain this code and work out that actually the test &lt;code&gt;navigator.userAgent.toLowerCase().indexOf(...)&lt;/code&gt; isn&amp;#8217;t the right test. Because the code is simply repeated, you have to edit five lines. What about if the numeric limit of &lt;code&gt;1000000&lt;/code&gt; needs to be changed? You have to edit five lines. Add an argument to the function call? Five lines.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Copy-and-paste once, and edit heavily, by all means, but if you find yourself copy-and-pasting code repeatedly, with small edits each time, Stop. Think. Refactor. Extract the commonalities, with the aim of coding each thing just once (not just the processor doing each thing once, actually &lt;em&gt;writing&lt;/em&gt; each thing once). Think about the changes that someone might need to make to your code, and factor those parts out so that they&amp;#8217;re easy to change.&lt;/p&gt;

&lt;p&gt;OK, so I guess I have to put my neck on the line and give an example of how I&amp;#8217;d write it. Assuming that you do actually need to have individual control over the allowed page sizes for each browser, then&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var browser = navigator.userAgent.toLowerCase();
var defaultLimit = 1000000;

var limits = new Array();
limits[&quot;safari&quot;]   = defaultLimit;
limits[&quot;opera&quot;]    = defaultLimit;
limits[&quot;firefox&quot;]  = defaultLimit;
limits[&quot;netscape&quot;] = defaultLimit;
limits[&quot;msie&quot;]     = defaultLimit;

for (limit in limits) {
  if (browser.indexOf(limit) &amp;gt;= 0 &amp;amp;&amp;amp;
      pageSize &amp;gt; limits[limit]) {
    hideLink();
    break;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;does the trick. I&amp;#8217;m getting the lower case browser name only once. The tests aren&amp;#8217;t repeated, so if they change, I only have to edit in one place. The most likely things to change are split off into variables at the top of the code, so that they&amp;#8217;re easy to locate and easy to alter.&lt;/p&gt;

&lt;p&gt;Why do I even bother to write about this, something that surely every coder knows? Because I&amp;#8217;ve seen programmers, real life ones, ones who earn money as developers, doing copy-and-paste coding, and I&amp;#8217;m fed up of being the muggins who has to wade through it, copying and pasting the same fixes, or rewriting swathes of it that they couldn&amp;#8217;t be bothered to refactor themselves along the way. I&amp;#8217;m tired of picking my way through code to confirm that, yes, this really is a character-by-character verbatim copy of exactly the same very complex XPath used just three lines above. And don&amp;#8217;t get me started on the same code repeated in separate modules. Argh.&lt;/p&gt;

&lt;p&gt;Show some empathy for the person who&amp;#8217;s going to be maintaining your code. Who knows: it might even be you.&lt;/p&gt;
</description>
 <comments>http://www.jenitennison.com/blog/node/37#comments</comments>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/30">coding</category>
 <pubDate>Wed, 04 Jul 2007 21:26:31 +0100</pubDate>
 <dc:creator>Jeni</dc:creator>
 <guid isPermaLink="false">37 at http://www.jenitennison.com/blog</guid>
</item>
</channel>
</rss>
