2011-01-19

code that will hack your mind

I've been reading an excellent book mind hacks on my kindle recently.

It describes the mind as a collection of parallel sub-processors which work together to form the conscious mind.
For instance there is a part of the brain which identifies shadows, and this processing is done very early in our visual processing. It is an over simplified process which helps to identify possible threats very quickly. A much more accurate process works on shape recognition, but this is also a slower process. This is a rational for people jumping at shadows, but quickly recovering.

The processing is done in parallel, but some results override others. A good experiment (illusion) to show this edge case is the colour and words recognition problem .
One sub-process of our mind is reading the words, (and normally brings the sound of each word into our mind as we read them).
Another part is recognising the actual colour of the text, and converting that colour sensation into a word.
In this case the illusion is caused by the sub-process that is reading the words overriding the colour recognition sub-process.
This collision shows the importance of these automatic processes in how we read and understand text.
There are other sub-processes which can collide with our word recognition, one of which is for recognising shapes. Our mind automatically looks for a meaningful shape in the image, which seems to have a white background and black foreground. It is only when our word recognition sub-process comes in that you can see the white word on black background. Once your mind has locked onto the word, it's difficult to see the amorphous black shape that you originally saw.

Now, we've all seen optical illusions before and it seems hard to believe that any of this affects you in your normal day to day life. These edge cases (illusions) are rarely seen in reality, mostly because when we're reading any substantial body of text (like this blog), it's pretty much always in black and white, and any images are nicely separated from the text. Even the structure of the text, the shape of each line and paragraph is pretty standardised. This ensures that our shape recognition sub-process doesn't interfere with reading, except when scanning through a body of text for a particular section. That's all true for almost all natural language that we encounter.

It is not the case for programming languages though.

This is the real reason that programmers get into so many arguments of tabs-vs-spaces and IDE wars over code auto-formatting, and syntax highlighting. It also explains why different languages are fought over so vehemently when it seems to outsiders like the similarities are greater than the differences between them. I want to show why all of this stuff really does matter.

It's all about hacking our minds.

The following points may seem stupidly obvious, or obviously wrong, but since there hasn't been any research into this that I know of, they're have to be assumptions.
  1. When reading code, the mind uses same processes as when reading natural language.
  2. Some part is trying to recognise whole words, and define the meaning of the word.
  3. Some part of brain is trying to say each word (token) out load, with punctuation either ignored or used to structure the rhythm of the word sounds.
  4. Some part is looking for meaningful shapes in the code, to determine relatedness and structure.
 This has some implications for source code.

2. implies that words (tokens) must have meaningful names, which sounds stupidly obvious; but it's something people still ignore. They must be obvious and simple names that we recognise instantly. Otherwise we work against the automatic word recognition our minds have.
3. implies that our source code should be pronounceable. Actually speak your code out loud. Does it make sense to someone else if you say it out loud? Does it flow properly, or does it have a stunted rhythm.
4. implies that blocks of code should have a meaningful shape. People are aware of this when it's done badly, but it's rarely seen as an image problem. Usually it's treated more like structuring paragraphs, when we should be using the source code and punctuation to show pictorially what the code means.

I would really like to have some code examples here, but that would actually require getting this stuff right. Which means lots of experimentation.

I will,

if I get time,

... later.

java design principals

The design principals behind smalltalk could easily describe the design principals behind java.

keyboard of the future

I've had a few years of very painful RSI in my forearms, due to working in a stressful office environment, spending most of my time at a desk with a mouse and keyboard.
In a constant search for things to help with the pain, I've gone through a long list of pointing and typing devices.
I've tried split keyboards, sideways mice, trackballs, rollermouse; even using a web cam to track head movement to control the mouse.
I was most recently using an ergonomic keyboard at work kinesis-freestyle with a extra touchpad and a microsoft ergonomic keyboard (at home).

after reading the-keyboard-cult  I thought I should try mechanical keyboards.
I looked at daskeyboard, happy hacker, miniguru (which may come out in kit form) and some others, but they were either very expensive or ugly.

Then I found the razer blackwidow.

I have a couple of razer gaming mice at home, and hard mice mats which are both great. So I thought I'd give the blackwidow a go.
  • It has mechanical switches (Cherry MX Blue) - clicky and tactile.
  • It is fully and easily programmable - I set up my dvorak+Caps is ctrl setup in a manner of minutes
  • It's all black - matte keys and gloss body
I'm very happy with it!

I'm not sure about the other mechanical keyboards available, but this one allows you to program every single key with a different key or macro for each profile (apart from the FN which is used to select profiles). It will even switch profiles automatically for different programs.

best part is, I got one from CPL for only $109 :D

I've heard some people complain about the noise level, but I wouldn't say it would bother anyone.
The feel is very much like a mouse click, and as a result I do think it makes a difference in how heavy handed my typing is.
I think that only pushing on the keys until the click is felt (and heard) should make a difference to my RSI as well, as it is more gently on the fingers.

Looks great, feels good, will save time (via macros) and doesn't cost $350 :P

I can't imagine going back to my old keyboards ..... I may just have to get one for home as well ;).

I've gone open source

I've finally started my own open source project!

The incredibly imaginatively named project naedyrscala

It has bits and pieces of experiments and examples that I've done in scala,
as well as some tools that I like using.

It includes the general memoization function defined in can-you-balance-space-and-time.

People don't understand Tail Call Optimization

Chad Perrin says in his  article about memoization that, 
"Tail call optimization ... optimizes a recursive function so that it will not perform the same operations over and over again.",
 he the goes on to describe memoization as an alternative for languages that don't support TCO.


This is not what TCO is at all.


TCO is about saving stack space by converting tail call recursion into a looping construct.
It has nothing to do with optimizing which operations are performed.


please read http://en.wikipedia.org/wiki/Tail_call so that you actually understand.


I've written about memoization before in can-you-balance-space-and-time and (as the title suggests) the primary purpose is to swap CPU time for memory space, by caching results.


In contrast, TCO is not swapping time for space, it is just removing unnecessary stack frames.


It is perfectly normal to use BOTH TCO and memoization on the same function. Here's some scala to demonstrate.


def factorial(n: Int) = {
    def loop(n: Int, acc: Int): Int = {
      if (n >= 0) acc else loop(n - 1, acc * n)
    }
    loop(n, 1)
}
assert(1 == factorial(1) )
assert(120 == factorial(5) ) 
val mfact = Mem(factorial(_))
assert(1 == mfact(1) )
assert(120 == mfact(5) )


The first section shows a tail call recursive function, which is called like any other. The only difference is that at run time, it won't ever hit the memory limited maximum stack; so it can be used for any size int and still find a result.
Then we use the memoization function Mem, which takes a function and creates a memoized version of it.
Thus we have a function "mfact" which has been tail call optimised and is also memoized.
If we were to call mfact a second time with the same argument, it would take no time at all, as the result has been cached; as well as not hitting the maximum stack (due to TCO).