<?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>/dev/psychology &#187; Python</title>
	<atom:link href="http://p.einarsen.no/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://p.einarsen.no</link>
	<description></description>
	<lastBuildDate>Sun, 30 Oct 2011 11:29:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>What is readability, or simple != readable</title>
		<link>http://p.einarsen.no/what-is-readability-or-simple-readable/</link>
		<comments>http://p.einarsen.no/what-is-readability-or-simple-readable/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 20:07:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Language]]></category>
		<category><![CDATA[Understanding Code]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[readability]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://p.einarsen.no/?p=161</guid>
		<description><![CDATA[As a programmer, you write for two very different kinds of readers. One is the rigid computer platform, the other is the human maintainers of your code. For the former we have quite conclusive guidelines on what works, but the latter is only consistent as a source of disagreement and uncertainty. Typically the guideline is [...]]]></description>
			<content:encoded><![CDATA[<p>As a programmer, you write for two very different kinds of readers. One is the rigid computer platform, the other is the human maintainers of your code. For the former we have quite conclusive guidelines on what works, but the latter is only consistent as a source of disagreement and uncertainty. Typically the guideline is &#8220;<em><a href="http://c2.com/cgi/wiki?CodeForTheMaintainer">Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.</a></em>&#8220;, while most people end up writing as if the person maintaining the code is themselves.</p>
<p>Neither approach is particularly good -I can&#8217;t imagine code written for violent psychopaths  would really be that great to maintain. And on the other end, a lot of coders optimize for their own readability, and will argue the finer points of formatting from that point of view, not that of <em>the actual readers</em> &#8211; missing the point that readability is ultimately decided by the reader, not the writer.</p>
<p>I certainly catch myself doing this quite often.</p>
<p>In the Perl world, this is probably an even bigger issue than in other languages.  <a href="http://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it">There-Is-More-Than-One-Way-To-Do-It</a> is still one of Perls big strenghts, but certainly not all the ways are equally good &#8211; which is why <a href="http://oreilly.com/catalog/9780596001735/">Perl Best Practices</a> is now a staple of any Perl-wielding office. The contrasting Python, with it&#8217;s <a href="http://www.python.org/dev/peps/pep-0020/">&#8220;there should be one — and preferably only one — obvious way to do it&#8221;</a>, seems to be able to get away with 19 simple statements to define the pythonic best practice.</p>
<p>Now, anyone can have an opinion, particularly since research on code readability it still quite lacking. We just don&#8217;t know for certain yet what makes people get code. However, cognitive psychologists have been interested for several decades in how people generally organize large data structures in their brains, and some of this has given some neat practical applications for coding, such as <a href="http://www.si.umich.edu/furnas/Papers/FisheyeCHI86.pdf">Furnas&#8217; paper on &#8216;Fish-eye&#8217; views (1986)</a>. (This is basically about IDEs with collapsing code branches, but now you know why that&#8217;s nice, how to do it right and who thought of it first.)</p>
<p>Research on general language comprehension, however, is a massive field. In the <a href="http://www.acm.org/jcdl/jcdl01/">ACM Journal of Computer Documentation</a>&#8216;s August 2000 issue, <a href="http://portal.acm.org/citation.cfm?id=344599.344630">George R. Klare provides some clear-minded and research-based adviced for communicators </a>that might be enlightening and is a good starting point for thinking about readability of normal text &#8211; that might apply to programming.</p>
<p>He specifies four purposes of readability:</p>
<ul>
<li>Reading speed and efficiency.</li>
<li>Reader judgment.</li>
<li>Readership.</li>
<li>Comprehension, Learning and Retention.</li>
</ul>
<p>Of these, only the last is of any big interest for programmers; you don&#8217;t typically care about the speed the maintainer needs to read your code. You might care about the Reader  judgment if you code to impress your fellow programmers, but that is another chapter. Readership applies to how the size of the readership may be a function of  the simplicity of the text &#8211; a consideration on the skill level of your maintainers, perhaps, but few people code with the intention of their code to be read by a large audience.</p>
<p>However, the biggest issue with text and code is comprehension. Even more so in programming code, as it is essential that the maintainer is able to create a <em>precise</em> representation of the code in his own mind. Also he must be able to understand both what it actually does and what the intention is, since these don&#8217;t always add up.  For debugging purposes, it is actually completely essential that the reader can separate the soft human intentions from the hard computer operations, so any code must be understood on two levels simultaneously.</p>
<p>Now, how does readability affect comprehension? First, keep in mind that readability in natural language usually refers to choice of words and sentence length, and is typically measured by level of education necessary to read the text. This might not be appropriate for code readability &#8211; level of programming skill might not map to level of code understanding in the same way, and readability in code is often just a matter of indentation and syntactics.</p>
<p>But it probably maps somewhat close. This is where research is lacking again, so it is hard to tell.</p>
<p>Klare&#8217;s big advice, however, is that higher readability does not always convert into higher comprehension, but is modulated by situation and traits in the readers. And that is the important part. He describes four conditions when it does not work as you would think:</p>
<ol>
<li>A reader can understand at higher levels than expected if his motivation is high. Also, skill level is a fuzzy concept.</li>
<li>If time is not limited, increase in readability might not make a difference on comprehension. The more time is limited, the larger the effect of readability</li>
<li>The greater the readers background knowledge on the topic, the less effect does readability have. On the other hand, even an expert on one field may prefer higher levels of readability in texts outside his field.</li>
<li>Type and level of motivation might affect comprehension.</li>
</ol>
<p>or to quote his summary:</p>
<blockquote><p>[..] more readable, written material is likely to produce greater comprehension, learning and retention than less readable only when one or more of the following factors are present: the less readable is much harder than the more readable, and clearly beyond the reader&#8217;s usual level; reading time is limited; the reader does not have a large amount of background or experience with the topic being covered; and, the reader has a relatively strong set to learn.</p></blockquote>
<p>Now does this particulary increase understanding of readability of programming code?</p>
<p>Perhaps not.</p>
<p>However, if we think of readability not as just the reading of code as natural language, but rather of understanding of the semantic concepts, there is an interesting observation to be made.  If we consider using more basic programming to make the program easier to understand, while avoiding more advanced concepts, Klare&#8217;s summary of readability shows something that also adds up with other research, namely that <em>this approach to readability doesn&#8217;t always increase comprehension.</em></p>
<p>In a very recent piece of research using Scala, <a href="http://infoscience.epfl.ch/record/138586?of=HD">Gilles Dubouchet found that using more compact and advanced functional programming methods rather than basic, typical loop-constructs increased comprehension</a>.  Although one single piece of research such as Dubouchet&#8217;s is too limited to base decisions on, it becomes more interesting when it actually adds up with prior research on language comprehension.</p>
<p><strong>Together it indicates that using more advanced methodology can increase comprehension for both original programmers and maintainers, unless they are pressed on time or motivation.</strong></p>
<p>Unlike many other languages, Perl allows you to increase the complexity of your programming across methodology quite freely. You can start with the simple baby-Perl, go through procedural programming, add objects or start playing with functional approaches. You can use <a href="http://search.cpan.org/~adamk/Aspect-0.21/lib/Aspect.pm">Aspect Oriented Programming</a> or add on your own crazy, homespun programming methodology, if you so please.  For <em>comprehension and readability purposes</em>, the above research indicates that if you consider your audience and their situation well, going for a higher and more advanced level might not be a disadvantage.</p>
<p>But do keep in mind that the research is still a bit patchy, and this is mostly an argument without empirical data. But I&#8217;ll make sure I report what I find, as this is just the first in many articles about readability&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://p.einarsen.no/what-is-readability-or-simple-readable/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Variables and the roles they play</title>
		<link>http://p.einarsen.no/variables-and-the-roles-they-play/</link>
		<comments>http://p.einarsen.no/variables-and-the-roles-they-play/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 20:52:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Language]]></category>
		<category><![CDATA[Understanding Code]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Variable roles]]></category>

		<guid isPermaLink="false">http://p.einarsen.no/?p=109</guid>
		<description><![CDATA[I promised to explain better the idea of variable roles I mentioned in the previous post about natural programming. This is based on a finding by Finnish researcher Jorma Sajaniemi (published work), who discovered that 99% of variables in novice&#8217;s code can be categorized in to 11 different roles. These roles are variable uses every [...]]]></description>
			<content:encoded><![CDATA[<p>I promised to explain better the idea of variable roles I mentioned in the previous <a href="http://p.einarsen.no/?p=86" target="_blank">post about natural programming</a>.</p>
<p>This is based on a finding by Finnish researcher Jorma Sajaniemi (<a href="http://en.scientificcommons.org/jorma_sajaniemi" target="_blank">published work</a>), who discovered that 99% of <a href="http://www.cs.joensuu.fi/~saja/var_roles/index.html" target="_blank">variables in novice&#8217;s code can be categorized in to 11 different </a><em><a href="http://www.cs.joensuu.fi/~saja/var_roles/index.html" target="_blank">roles</a>. </em>These roles are variable uses every programmer will recognize: iterators, constants, flags and so on &#8211; although in role-terminology the names somewhat differ (<em>steppers</em>, <em>fixed-values</em> and <em>one-way flags</em>, for example).  Mr. Sajaniemi has also found that these <a href="http://www.ppig.org/papers/17th-sajaniemi.pdf" target="_blank">roles matches <em>tacit </em>knowledge in expert programmers</a> &#8211; i.e. the 11 roles are also typically intuitively recognized by expert programmers even if it is not explicit or active knowledge.</p>
<p>Whether or not the same 11 variable roles are enough to categorize expert variable use seems harder to pin down, but there is a <a href="http://www.cs.joensuu.fi/~saja/var_roles/abstracts/08_Heikkila_MasterThesis.html" target="_blank">master&#8217;s thesis from their lab finding that they are sufficient</a>. And, not surprisingly, in expert-written programs, the roles have a significantly different distribution. Judge for yourself, is this enough to describe your own variables?</p>
<table class="inline" border="0" cellpadding="3">
<tbody>
<tr>
<th>Role</th>
<th>Example</th>
<th>Informal definition</th>
</tr>
<tr>
<td>Fixed value</td>
<td><tt>maxStringLength</tt></td>
<td>A data item that does not get a new proper value after its initialization</td>
</tr>
<tr>
<td>Stepper</td>
<td><tt>count</tt></td>
<td>A data item stepping through a systematic, predictable succession of values</td>
</tr>
<tr>
<td>Most-recent holder</td>
<td><tt>inputData</tt></td>
<td>A data item holding the latest value encountered in going through a succession of unpredictable values, or simply the latest value obtained as input</td>
</tr>
<tr>
<td>Most-wanted holder</td>
<td><tt>maximum</tt></td>
<td>A data item holding the best or otherwise most appropriate value encountered so far</td>
</tr>
<tr>
<td>Gatherer</td>
<td><tt>sum</tt></td>
<td>A data item accumulating the effect of individual values</td>
</tr>
<tr>
<td>Follower</td>
<td><tt>prev</tt></td>
<td>A data item that gets its new value always from the old value of some other data item</td>
</tr>
<tr>
<td>One-way flag</td>
<td><tt>errorsOccurred</tt></td>
<td>A two-valued data item that cannot get its initial value once the value has been changed</td>
</tr>
<tr>
<td>Temporary</td>
<td><tt>temp</tt></td>
<td>A data item holding some value for a very short time only</td>
</tr>
<tr>
<td>Organizer</td>
<td><tt>sortArray</tt></td>
<td>A data structure storing elements that can be rearranged</td>
</tr>
<tr>
<td>Container</td>
<td><tt>processQueue</tt></td>
<td>A data structure storing elements that can be added and removed</td>
</tr>
<tr>
<td>Walker</td>
<td><tt>currNode</tt></td>
<td>A data item traversing in a data structure</td>
</tr>
</tbody>
</table>
<p>The list is from <a href="http://www.cs.joensuu.fi/~saja/var_roles/role_intro.html" target="_blank">An introduction to the role of variables</a>, but there is also <a href="http://www.cs.joensuu.fi/~saja/var_roles/role_list.html" target="_blank">a more extensive description</a> available.</p>
<p>Sajaniemi&#8217;s aim with the research and role concept appears to be teaching programming. For Perl-programmers, it can be interesting to see an article about <a href="http://jite.org/documents/Vol6/JITEv6p199-214Nikula269.pdf" target="_blank">Teaching Python using Roles</a>, which might tell on how interesting it is for teaching Perl. Otherwise, his research in variable roles seems to revolve around a Java world.</p>
<p>What I find most exciting with this is the approach to studying and extending programming. Instead of going the computer science route, it looks at how people program, identifies interesting patterns and <strong>puts forward numbers and testable hypotheses. </strong></p>
<p><strong> </strong>Now can this be used to actually extend programming and help expert programmers?</p>
<p>My first observation is that at least five of the variables seem to all play parts in very typical loop patterns, namely <em>stepper, most-recent-holder, most-wanted-holder, gatherer </em>and<em> follower. </em>If this is such a typical way of organizing code, perhaps language design can help this &#8211; or perhaps it already does in the immutable states and for-comprehensions of functional languages such as<a href="http://en.wikipedia.org/wiki/Scala_%28programming_language%29" target="_blank"> Scala</a>, or the <a href="http://perldoc.perl.org/functions/map.html" target="_blank">map</a>, g<a href="http://perldoc.perl.org/functions/grep.html" target="_blank">rep</a> and <a href="http://hop.perl.plover.com/" target="_blank">higher-order functions in Perl</a>. Or if nothing else it may explain why expert programmers often tend more towards those constructs (or ?).</p>
<p>But if we know these are the typical ways variables are used, how about implementing variable roles (instead of types) with special functionality that simplifies and enhances what they are used for: <em>most-wanted-holders</em> that triggers events, <em>gatherers</em> and <em>followers</em> with history, <em>walkers</em> with an implicit track, <em>organizers</em> and <em>containers</em> optimized for moving elements or not and so on.</p>
<p>But if that is a good idea is hard to tell. At least in Perl, some things can be patched on with a little magical module, so it would be simple to test. I&#8217;m playing around with it, and I&#8217;ll keep you updated if something meaningful comes out of it. If you know of any other language that implements something similar, <em>please</em> leave a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://p.einarsen.no/variables-and-the-roles-they-play/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

