There's a concept in leadership theory called the Einstellung effect; it's sometimes also known as the mechanisation of thought. It's a very helpful explanation for a lot of (bad) programming practices.
A guy called Lurchins gave people three jugs of varying sizes, and asked them to draw a certain amount of water into a bucket. So if you have a 21cc jug, (A) a 127cc jug, (B) and a 3cc jug, (C) and you want 100cc, then you fill jug B and put that in the bucket; then you fill jug A from the bucket and take it away; then you fill jug C from the bucket and take it away, and repeat this final step. Essentially, you've just done the sum B-A-2C.
He gave them a few exercises like that: A=14, B=163, C=25, make 99; A=18, B=43, C=10, make 5. That sort of thing. All of them can be solved with B-A-2C. Then he gave them A=23, B=49, C=3, make 20. 83% of them went for B-A-2C, even though A-C is somewhat simpler. When it got to A=28, B=76, C=3, make 25, 64% of them gave up, as B-A-2C no longer worked.
From this Lurchins deduced that when we find a solution that works in a general class of problems, we form a mental mould that is incredibly difficult to break so long as we're just solving problems. This is the Einstellung effect, and from this follows cut-and-paste code, "but we've always done it this way", and any number of other programming disasters.
But here's the interesting bit. As soon as we stop solving problems, and pay attention to something else, the Einstellung effect can be shattered:
After problem 5, Lurchins told some subjects "Don't be blind", which caused more than 50% to find the simpler solution on the remaining problems.
Interruption - any kind of interruption - can cause us to re-evaluate the problem from scratch. From this follows the common sense advise that if you can't see how to code what you want, get up and take a break. Chances are you just need to shift your brain away from a pattern it used to solve a similar problem before.
And if you catch someone cutting-and-pasting an inappropriate solution, don't be too harsh on them; they probably honestly can't see the simpler way due to the Einstellung effect. Just tell them to get up and make a cup of coffee, and see what they come up with when they look at it again...
Full version - 2 Comments