<?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>Psychology of Programming &#187; Writing Code</title>
	<atom:link href="http://p.einarsen.no/category/writing-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://p.einarsen.no</link>
	<description></description>
	<lastBuildDate>Thu, 24 Jun 2010 01:19:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Does code base structure follow organization structure?</title>
		<link>http://p.einarsen.no/does-code-base-structure-follow-organization-structure/</link>
		<comments>http://p.einarsen.no/does-code-base-structure-follow-organization-structure/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 08:19:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Management]]></category>
		<category><![CDATA[Understanding Code]]></category>
		<category><![CDATA[Writing Code]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[organizations]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[structure]]></category>

		<guid isPermaLink="false">http://p.einarsen.no/does-code-base-structure-follow-organization-structure/</guid>
		<description><![CDATA[After writing about the Software Engineering Myths Microsoft claims to have busted, I&#8217;ve been thinking about their find that organization structure is the most predictive factor for bugs.
And it makes me think, to what degree are organizations&#8217; code bases shaped by their formal or informal organization structure? Are core modules and root objects often the [...]]]></description>
			<content:encoded><![CDATA[<p>After writing about the <a href="http://p.einarsen.no/debunking-software-engineering-myths-does-the-organization-matter-more-than-the-programming/" target="_blank">Software Engineering Myths Microsoft claims to have busted</a>, I&#8217;ve been thinking about their find that organization structure is the most predictive factor for bugs.</p>
<p>And it makes me think, to what degree are organizations&#8217; code bases shaped by their formal or informal organization structure? Are core modules and root objects often the domain of senior developers and objects lower in the hierarchy the domain of juniors? My experience is that it often tends to be, and it seems a reasonable overlap: after all, you want your more trusted developers fiddling where the damage can be greatest.</p>
<p>But how about other attributes of the code base? In the world of Perl, are CPAN authors often hired as external consultants? Are the most communicative programmers the ones that will write network services? Are the modules most used also written by the programmers that are most in contact with others?</p>
<p>And does organization structure also shape general code base structure? Will a more hierarchical organization tend more towards hierarchical object structures, while more chaotic or flat organizations tend towards more chaotic or flat code organization?</p>
<p>A lot of questions, but no answers&#8230; But one thing that comes to mind is, if organization and code structure follows each other, is this a good idea? I think few people designing a data model or object hierarchy starts with the organization structure as a blueprint, but speaking from my own limited experience, you can often at least see a reflection of either in both.  Is this a good or bad thing? What can the consequences be?</p>
]]></content:encoded>
			<wfw:commentRss>http://p.einarsen.no/does-code-base-structure-follow-organization-structure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Debunking Software Engineering Myths: Does the organization matter more than the programming?</title>
		<link>http://p.einarsen.no/debunking-software-engineering-myths-does-the-organization-matter-more-than-the-programming/</link>
		<comments>http://p.einarsen.no/debunking-software-engineering-myths-does-the-organization-matter-more-than-the-programming/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 13:27:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Management]]></category>
		<category><![CDATA[Writing Code]]></category>
		<category><![CDATA[Case study]]></category>
		<category><![CDATA[code quality]]></category>
		<category><![CDATA[dynamic typing]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://p.einarsen.no/?p=270</guid>
		<description><![CDATA[I&#8217;ve been wanting to post a link to this article for a while, but ever since I discovered it, research.microsoft.com has been unreachable for me, so I&#8217;ll post a small summary:
Microsoft has done research on some popular conceptions about software engineering and come up with hard numbers on some factors affecting code quality.  Here are [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been wanting to post a link to this article for a while, but ever since I discovered it, <a href="http://research.microsoft.com/" target="_blank">research.microsoft.com</a> has been unreachable for me, so I&#8217;ll post a small summary:</p>
<p>Microsoft has done research on some popular conceptions about software engineering and come up with hard numbers on some factors affecting code quality.  Here are the main findings reported in the artice, with links to the research papers, in case the original is lost forever:</p>
<ul>
<li>More test coverage does <strong>not</strong> equal better code quality, as measured by number of post-release fixes.  Usage patterns and code complexity are the main reason test coverage is a poor predictor of quality.</li>
<li><a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank">Test Driven Development</a> increases quality, creating code that was up to 40% to 90% better in the Microsoft research. The cost is increased development time, 15% to 35% in the same studies. (<a href="http://portal.acm.org/citation.cfm?id=1380664" target="_blank">Realizing quality improvement through test driven development: results and experiences of four industrial teams</a>)</li>
<li>Use of <a href="http://en.wikipedia.org/wiki/Assertion_(computing)" target="_blank">Assertions</a> decrease program faults. However, this is also correlated with programmer experience, meaning more experienced programmers generally write more assertions and have less program faults. (<a href="http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=4021986" target="_blank">Assessing the Relationship between Software Assertions and Faults: An Empirical Investigation</a>). And here is <a href="http://lambda-the-ultimate.org/node/1912" target="_blank">more about Assertions</a>.</li>
<li>Organizational metrics, which are not related to the code, can predict software failure-proneness with a precision and recall of 85 percent. Not only that, but organizational structure was by far <em>the best predictor of code quality</em>, and was at least 8% percent better than the best predictor the researchers could get from code-based measurements. (<a href="http://portal.acm.org/citation.cfm?id=1368160" target="_blank">The influence of organizational structure on software quality: an empirical case study</a>)</li>
<li>Organizational closeness is more important than geographical closeness, which has no effect on quality (<a href="http://macbeth.cs.ucdavis.edu/distributed.pdf" target="_blank">Does Distributed Development Affect Software Quality? An Empirical Case Study of Windows Vista</a>)</li>
</ul>
<p>Here&#8217;s Microsoft&#8217;s article: <a href="http://research.microsoft.com/en-us/news/features/nagappan-100609.aspx" target="_blank">Exploding Software-Engineering Myths</a> (or if that doesn&#8217;t work, <a href="http://74.125.77.132/search?q=cache:HRiBUTlXJmQJ:research.microsoft.com/en-us/news/features/nagappan-100609.aspx+http://research.microsoft.com/en-us/news/features/nagappan-100609.aspx&amp;cd=1&amp;hl=en&amp;ct=clnk&amp;client=safari" target="_blank">a Google Cache link).</a></p>
<p>One drawback with this research is that this is primarily based on case studies, which is a generally poor research method for drawing general conclusions. How valid are these observations for other organizations outside Microsoft? Is the organizational structure of your project or company actually more decisive than your programming methodology?</p>
<p>Also, how transferable is this to other programming frameworks. In dynamic typed languages like Perl, is test coverage more important? I often find that a sub-set of my tests do what a compiler could have done in a statically typed language, or even for Perl if I just had a more automatic testing tool. So maybe coverage would be more predictive of bugs if the compiler catches fewer mistakes? That would be a good candidate for further research.</p>
<p>However, this is excellent as a step towards more <a href="http://portal.acm.org/citation.cfm?id=999432" target="_blank">evidence-based software engineering</a>.</p>
<p>Found through <a href="http://johntellsall.blogspot.com/2009/10/empirical-data-behind-software.html" target="_blank">John Tells All</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://p.einarsen.no/debunking-software-engineering-myths-does-the-organization-matter-more-than-the-programming/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Why do programmer personality types matter?</title>
		<link>http://p.einarsen.no/programmer-personality-types-and-why-it-matters-at-all/</link>
		<comments>http://p.einarsen.no/programmer-personality-types-and-why-it-matters-at-all/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 21:13:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Metrics]]></category>
		<category><![CDATA[Understanding Code]]></category>
		<category><![CDATA[Writing Code]]></category>
		<category><![CDATA[API's]]></category>
		<category><![CDATA[Cognition]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Personality]]></category>
		<category><![CDATA[Style]]></category>

		<guid isPermaLink="false">http://p.einarsen.no/?p=238</guid>
		<description><![CDATA[How do programmers differ, and why should you care? Steven Clarke from Microsoft&#8217;s usability labs has identified and demonstrated at least three different programmer styles, which has been reported in quite a few places, hence programmers do indeed differ. The types Clarke found are:
THE SYSTEMATIC DEVELOPER: Writes code defensively. Does everything he or she can [...]]]></description>
			<content:encoded><![CDATA[<p>How do programmers differ, <em>and why should you care</em>? <a href="http://blogs.msdn.com/stevencl/" target="_blank">Steven Clarke</a> from Microsoft&#8217;s usability labs has identified and demonstrated at least three different programmer styles, which has been reported in quite <a href="http://www.mertner.com/morten/?p=11" target="_blank">a</a> <a href="http://asserttrue.blogspot.com/" target="_blank">few</a> <a href="http://blogs.msdn.com/brada/archive/2003/11/18/50737.aspx" target="_blank">places</a>, hence programmers do indeed differ. The types Clarke found are:</p>
<blockquote><p><strong>THE SYSTEMATIC DEVELOPER: </strong>Writes code defensively. Does everything he or she can to protect code from unstable and untrustworthy processes running in parallel with their code. Develops a deep understanding of a technology before using it. Prides himself or herself on building elegant solutions.<br />
<strong>THE PRAGMATIC DEVELOPER:</strong> Writes code methodically. Develops sufficient understanding of a technology to enable competent use of it. Prides himself or herself on building robust applications.<br />
<strong>THE OPPORTUNISTIC DEVELOPER: </strong>Writes code in an exploratory fashion. Develops a sufficient understanding of a technology to understand how it can solve a business problem. Prides himself/herself on solving business problems.</p></blockquote>
<p><em>Now why should you care?</em></p>
<p>Almost every mention I&#8217;ve seen of this online &#8211; or of any other personality type categorization system &#8211; is usually followed by a &#8220;<em>which type are you</em>?&#8221;. This misses the point utterly and completely. Psychological research like this first becomes really valueable when you <strong>stop thinking about yourself </strong>and start asking how this can help you <strong>understand</strong> <strong>other people</strong>. If you design API&#8217;s and base your design on what makes most sense to your own coding style, you will create something that two thirds of your audience will find difficult to use. Even if you don&#8217;t like or agree with their style.</p>
<p>Granted, that makes the assumption that programmers are always equally distributed among styles, which is a pretty wild assumption. The point is that other people are more likely to think differently than similarly to you.</p>
<p>That is also a good thing to keep in mind when formatting code for readability: if your coding style differs from standard <a href="http://perltidy.sourceforge.net/" target="_blank">Perl Tidy</a> or your company&#8217;s coding standard, keep in mind that you are not formatting for yourself, but a colleague, maintainer or anonymous CPAN downloader. They are more likely to understand a common standard than your standard.  It sounds obvious, don&#8217;t it? I don&#8217;t think many (any) programmers think like this even so.</p>
<p><img class="alignright" title="Figure 1" src="http://p.einarsen.no/wp-content/upload/clarke.fig1.png" alt="" width="232" height="237" />Now, <a href="http://brad_abrams.members.winisp.net/Projects/APIDesignPapers/MeasuringAPIUsability.pdf" target="_blank">Clarke, in an article to Dr. Dobbs Journal</a>, has an example of a cognitive mapping of programmer types and API traits which is quite illustrative.  In Figure 1, thick blue lines shows the expectations of a particular programmer type, while the dark lines shows the score of a particular API. As you can see in this case, the match is bad. Now the good thing is that Clarke&#8217;s research gives you a framework to discuss how and why.</p>
<p><a href="http://brad_abrams.members.winisp.net/Projects/APIDesignPapers/Dagstuhl.pdf" target="_blank">That&#8217;s important because programming style isn&#8217;t related to experience level or educational background.</a> An programmer with an opportunistic style will not necessarily grow into a systematic programmer with more experience, neither will a systematic programmer become more pragmatic with age. Or perhaps they will &#8211; but you can&#8217;t assume that.</p>
<p>Finally, the dimensions which Clarke suggests APIs can be understood on at a cognitive level:</p>
<blockquote><p>• <strong>Abstraction level. </strong>The minimum and maximum levels of abstraction exposed by the API, and the minimum and maximum levels usable by a targeted developer.</p>
<p>• <strong>Learning style.</strong> The learning requirements posed by the API, and the learning styles available to a targeted developer.</p>
<p>• <strong>Working framework.</strong> The size of the conceptual chunk (developer working set) needed to work effectively.</p>
<p>• <strong>Work-step unit. </strong>How much of a programming task must/can be completed in a single step.</p>
<p>• <strong>Progressive evaluation.</strong> To what extent partially completed code can be executed to obtain feedback on code behavior.</p>
<p>• <strong>Premature commitment</strong>. The amount of decisions that developers have to make when writing code for a given scenario and the consequences of those decisions.</p>
<p>• <strong>Penetrability</strong>. How the API facilitates exploration, analysis, and understanding of its components, and how targeted de- velopers go about retrieving what is needed.</p>
<p>• <strong>API elaboration.</strong> The extent to which the API must be adapted to meet the needs of targeted developers.</p>
<p>• <strong>API viscosity.</strong> The barriers to change inherent in the API, and how much effort a targeted developer needs to expend to make a change.</p>
<p>• <strong>Consistency.</strong> How much of the rest of an API can be inferred once part of it is learned.</p>
<p>• <strong>Role expressiveness.</strong> How apparent the relationship is between each component exposed by an API and the program as a whole.</p>
<p>• <strong>Domain correspondence.</strong> How clearly the API components map to the domain and any special tricks that the developer needs to be aware of to accomplish some functionality.</p></blockquote>
<p>Steven Clarke seems to have discontinued his blog, but his scientific work is <a href="http://en.scientificcommons.org/steven_clarke" target="_blank">chronicled at Scentific Commons</a>.</p>
<p>(Found, among other places, <a href="http://asserttrue.blogspot.com/" target="_blank">assertTrue()</a> ).</p>
]]></content:encoded>
			<wfw:commentRss>http://p.einarsen.no/programmer-personality-types-and-why-it-matters-at-all/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Functional programming and disappearing variable roles</title>
		<link>http://p.einarsen.no/functional-programming-and-disappearing-variable-roles/</link>
		<comments>http://p.einarsen.no/functional-programming-and-disappearing-variable-roles/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 19:25:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Language]]></category>
		<category><![CDATA[Writing Code]]></category>

		<guid isPermaLink="false">http://p.einarsen.no/?p=212</guid>
		<description><![CDATA[After writing about variable roles a while back, I&#8217;ve been thinking a lot about the way I use variables. The post also got quite a lot of attention, so it seems to have hit a note with people. And no wonder it does: the concept kinda promises to tell you something profound about programming. The [...]]]></description>
			<content:encoded><![CDATA[<p>After writing about <a href="http://p.einarsen.no/variables-and-the-roles-they-play/">variable roles a while back</a>, I&#8217;ve been thinking a lot about the way I use variables. The post also got quite a lot of attention, so it seems to have hit a note with people. And no wonder it does: the concept kinda promises to tell you something profound about programming. The problem is that it doesn&#8217;t really deliver on the promise.</p>
<p>The first observation anyone with some experience in programming will see, is that the majority of the roles are loop control variables (check the post, but basically they mention &#8220;stepper&#8221;, &#8220;most-recent-holder&#8221;, &#8220;most-wanted-holder&#8221;, &#8220;gatherer&#8221; and &#8220;follower&#8221; as distinct roles, as well as &#8220;fixed-value&#8221; and &#8220;temporary&#8221;, who also could be said to be typical loop control roles. </p>
<p>My first idea was that if all these are necessary to traverse loops, perhaps a historized scalar variable would be helpful. I.e. the &#8220;follower&#8221; could just be a ->prev() method, there could be a sum of the history and so on. And it turns out there is a variable history module: <a href="http://search.cpan.org/dist/Tie-History/">Tie::History</a> (just remember to turn on AutoCommit).</p>
<p>Now maybe that wasn&#8217;t such a great idea. Or maybe it just isn&#8217;t thought properly through. Because what happens then is that you just start doing list operations on the history. Hence, maybe the list operations are just the way to go, and a more functional programming style will get rid off the elaborate loop control variables. Or so I suggested in the post. </p>
<p>Here, I&#8217;m trying out some attempts at bypassing the loop control variable roles by using a more <a href="http://en.wikipedia.org/wiki/Declarative_programming">declarative/functional programming style</a>. I&#8217;ve made some examples of typical imperative style loops, and have tried to see I could get rid of the traditional variable roles by doing it in a declarative style.  Have a look and see if it seems meaningful.</p>
<p>(Also, it gives me an opportunity to try out the awesome <a href="http://kpumuk.info/projects/wordpress-plugins/codecolorer/">CodeColorer</a> code viewer).</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@countries</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> USA China Netherlands<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Norway Finland Sweden <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$longest</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;"># $most wanted holder;</span><br />
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$country</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@countries</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># most recent holder</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a> <span style="color: #0000ff;">$longest</span> &nbsp;<span style="color: #339933;">&lt;</span> <a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a> <span style="color: #0000ff;">$country</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$longest</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$country</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Longest name: $longest<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></div></div>
<p>In my alternative take, I realized I ended up with both sort of a &#8220;most-wanted-holder&#8221; and TWO temporary variables. However, that I want to store my value somewhere, I can&#8217;t help, and at least there are no iterations using it. The temporary variables are worse, however. The point was to get rid of them after all.</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@countries</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> USA China Netherlands<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Norway Finland Sweden <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$longest</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/sort.html"><span style="color: #000066;">sort</span></a> <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a> <span style="color: #0000ff;">$b</span> <span style="color: #339933;">&lt;=&gt;</span> <a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a> <span style="color: #0000ff;">$a</span> <span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@countries</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Longest name: $longest<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></div></div>
<p>This version looks a bit sleeker, but sorting could be slower than a single iteration. Another approach that should have the same profile as the intial loop is <a href="http://search.cpan.org/~gbarr/Scalar-List-Utils-1.21/lib/List/Util.pm#reduce">reduce</a> from the core module <a href="http://search.cpan.org/~gbarr/Scalar-List-Utils-1.21/lib/List/Util.pm">List::Util</a>:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">use</span> List<span style="color: #339933;">::</span><span style="color: #006600;">Util</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span>reduce<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@countries</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> USA China Netherlands<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Norway Finland Sweden <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$longest</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> reduce <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a> <span style="color: #0000ff;">$b</span> <span style="color: #339933;">&gt;</span> <a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a> <span style="color: #0000ff;">$a</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">$b</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">$a</span> <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">@countries</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Longest name: $longest<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></div></div>
<p>And now we have even more internal variables in use..</p>
<p>Ok, the &#8220;stepper&#8221; role should be easier to remove. Here is a simple loop with a &#8220;stepper&#8221; and a &#8220;gatherer&#8221;:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@countries</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> China Netherlands Norway<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; USA Finland Sweden <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># stepper</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@uppercased</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># gatherer</span><br />
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">@countries</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$i</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/uc.html"><span style="color: #000066;">uc</span></a> <span style="color: #0000ff;">$countries</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">eq</span> <span style="color: #0000ff;">$countries</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/push.html"><span style="color: #000066;">push</span></a> <span style="color: #0000ff;">@uppercased</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$countries</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Uppercase names: @uppercased<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></div></div>
<p>It breaks my heart to write code like this. But it is also typical imperative code! This one lends itself easier to list operations too:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@countries</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> China Netherlands Norway<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; USA Finland Sweden <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@uppercased</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/grep.html"><span style="color: #000066;">grep</span></a> <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/uc.html"><span style="color: #000066;">uc</span></a> <span style="color: #0000ff;">$_</span> <span style="color: #b1b100;">eq</span> <span style="color: #0000ff;">$_</span> <span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@countries</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Uppercase names: @uppercased<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></div></div>
<p>One construct I vividly recognize is the &#8220;follower&#8221;, described as &#8220;A data item that gets its new value always from the old value of some other data item&#8221;. I am probably not the only one having written code balancing a pair of previous/this type variables. Here is a slightly contrived example that reports every adjacent pair of the same length:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@countries</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> USA China Netherlands<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Norway Sweden France Finland <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$previous_country</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;"># Follower</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@pairs</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Gatherer</span><br />
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$country</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@countries</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># most recent holder</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a> <span style="color: #0000ff;">$country</span> <span style="color: #339933;">==</span> <a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a> <span style="color: #0000ff;">$previous_country</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/push.html"><span style="color: #000066;">push</span></a> <span style="color: #0000ff;">@pairs</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$previous_country</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$country</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$previous_country</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$country</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Got pairs @pairs&quot;</span><span style="color: #339933;">;</span></div></div>
<p>This did not turn out as easy as I expected. If you try to do functional operations in Perl using core libraries, <em>reduce</em> is basically the only method that let you operate on list items in a non-independent fashion. And the only way I found to do this with reduce is even more contrived than my imperative example:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">use</span> List<span style="color: #339933;">::</span><span style="color: #006600;">Util</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> reduce <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@countries</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> USA China Netherlands<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Norway Sweden France Finland <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@pairs</span><span style="color: #339933;">;</span><br />
reduce <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a> <span style="color: #0000ff;">$a</span> <span style="color: #339933;">==</span> <a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a> <span style="color: #0000ff;">$b</span> <span style="color: #b1b100;">and</span> <a href="http://perldoc.perl.org/functions/push.html"><span style="color: #000066;">push</span></a> <span style="color: #0000ff;">@pairs</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$a</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$b</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$b</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@countries</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Got pairs @pairs&quot;</span><span style="color: #339933;">;</span></div></div>
<p>Now, except from the last example, I find the declarative approach to be generally far more easy to read. While that could just be because they are shorter, I don&#8217;t think that is sufficient as an explanation for that &#8211; there is plenty of compact code that would be easier to understand if it was more explicitly stated. </p>
<p>Now, cognitive psychology actually has an explanation of why declarative list operations like this are easier to understand.  And I&#8217;ll present the evidence for that in my next post. </p>
<p>Until then, there is <a href="http://www.joelonsoftware.com/items/2006/08/01.html">an article about functional programming on Joel on Software</a> that is worth reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://p.einarsen.no/functional-programming-and-disappearing-variable-roles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cognition, computers and electric currents</title>
		<link>http://p.einarsen.no/cogntition-computers-and-electric-currents/</link>
		<comments>http://p.einarsen.no/cogntition-computers-and-electric-currents/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 12:31:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Language]]></category>
		<category><![CDATA[Understanding Code]]></category>
		<category><![CDATA[Writing Code]]></category>
		<category><![CDATA[action potentials]]></category>
		<category><![CDATA[cogntion]]></category>
		<category><![CDATA[physiology]]></category>

		<guid isPermaLink="false">http://p.einarsen.no/?p=207</guid>
		<description><![CDATA[This is just a quick quote from the course description of a ten year old course in Cognitive Psychology and Computer Programming at the Texas A&#38;M:
Computer programming perhaps more than any other manufacturing endeavor begins with a thought and through skilled application of knowledge yields an intrinsically proven object that is itself almost mental (encoded [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick quote from the course description of a ten year old <a href="http://www.rtis.com/nat/user/jfullerton/school/psyc345/program.htm" target="_blank">course in Cognitive Psychology and Computer Programming</a> at the Texas A&amp;M:</p>
<blockquote><p>Computer programming perhaps more than any other manufacturing endeavor begins with a thought and through skilled application of knowledge yields an intrinsically proven object that is itself almost mental (encoded electrical information).</p></blockquote>
<p>It&#8217;s a good argument for why cognitive psychology is relevant for computer programming, but even more important, it points out the almost mental nature of computer programs.</p>
<p>Physiologically, the way our brains operate is mainly through bursts of electrical current called <a href="http://en.wikipedia.org/wiki/Action_potential" target="_blank">Action Potentials</a>, that propagate information down neurons in a on-off fashion. Some people will call it binary, but the information conveyed is typically frequencies rather than binary patterns  (But this is a somewhat contentious question).</p>
<p>Here&#8217;s a beautiful drawing of neural communication from Wikipedia:</p>
<p><a href="http://en.wikipedia.org/wiki/Action_potential" target="_blank"><img class="alignnone" title="Neuron" src="http://upload.wikimedia.org/wikipedia/commons/3/3e/Neurons_big1.jpg" alt="" width="510" height="539" /></a></p>
<p>So you have a mental construction in a human brain contained in electrical signals, and this is transferred over to a construction of electrical signals in a computer.  Add that you usually want these two representations to be identical, you have a good argument why programming language design should be based strongly on cognitive science!</p>
]]></content:encoded>
			<wfw:commentRss>http://p.einarsen.no/cogntition-computers-and-electric-currents/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to make a search field, for dummies</title>
		<link>http://p.einarsen.no/how-to-make-a-search-field/</link>
		<comments>http://p.einarsen.no/how-to-make-a-search-field/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:20:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Writing Code]]></category>
		<category><![CDATA[Common Sense]]></category>
		<category><![CDATA[HCI]]></category>
		<category><![CDATA[Heuristics]]></category>
		<category><![CDATA[Interfaces]]></category>

		<guid isPermaLink="false">http://p.einarsen.no/?p=78</guid>
		<description><![CDATA[Wil Shipley has an excellent blog post on how using simple heuristics - ie. less-than-perfect shortcuts to a goal &#8211; can improve life significantly, and how we tend to ignore that.
He makes a point about user interfaces, but it is good as a general observation:  A good-enough solution that improves life for many people is [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://wilshipley.com/blog/" target="_blank">Wil Shipley</a> has an excellent blog post on how <a href="http://wilshipley.com/blog/2009/08/pimp-my-code-part-16-heuristics-and.html" target="_blank">using simple heuristics </a>- ie. less-than-perfect shortcuts to a goal &#8211; can improve life significantly, and how we tend to ignore that.</p>
<p>He makes a point about user interfaces, but it is good as a general observation:  <em>A good-enough solution that improves life for many people is better than a perfect solution that can&#8217;t possibly be made. </em>Also, he points out that classic programming theory will teach you a lot of computer-sciency methods for problem solving, while in real-life are heuristic methods trying to understand user input as good as possible. That, however, I think is because he is a good programmer, not something all programmers come across: I bet the majority still will approach unexpected input with a <em>dying program</em>..</p>
<p>Or, as an otherwise very good programmer of the old-school I had the honour of knowing once said to a customer who complained about a particularly harshly crashing program: <em>&#8220;Considering what you tried to do, that was a mild reaction&#8221;</em>.</p>
<p>Also, there is <a href="http://stuffthathappens.com/blog/2008/03/05/simplicity/" target="_blank">this cartoon</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://p.einarsen.no/how-to-make-a-search-field/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
