News for hackers. Stuff that matters.
Re:Cognitive Biases in Programming by mikeash on Sunday October 15, 2017 @09:52AM (#15477106)
I'd like to add one which I might call "Excessive machine sympathy." This is where the programmer shies away from using a certain technique because it's too difficult, not quite grasping that it's difficult for the computer but not for the programmer. I often see newer programmers acting like:

  f1()
  for element in array {
    f2()
  }
Is easier to code than:

  for element in array {
    f1()
    f2()
  }
Re:Cognitive Biases in Programming by onion2k on Sunday October 15, 2017 @10:14AM (#15477175)
Did you mean to say 'easier'? I don't think I understand the point unless that should be 'harder'. Maybe I'm guilty of the bias you're talking about.
Re:Cognitive Biases in Programming by mikeash on Sunday October 15, 2017 @10:38AM (#15477270)
Yes, I see people thinking the first one is easier because they're thinking that you only do f1() once, instead of doing it thousands of times. In reality, both are equally easy to write, but it's hard for some people to shift their thinking from the work the computer does to the work the programmer does.
Re:Cognitive Biases in Programming by onion2k on Sunday October 15, 2017 @10:47AM (#15477312)
That's really interesting. So the point is that they're effectively the same in the sense of the amount of work necessary to write the code, and putting in the effort to code the first one might be premature optimisation if running f1() thousands of times isn't actually an issue, or that you might need to debounce f1(), or guard against side effects, which is 'harder'.

I think I'd find it really hard to let go of the idea that my code might be doing something wasteful like calling a function it doesn't really need to call. Maybe it's because I've been coding since the early 90s when clock cycles and memory actually mattered a bit more. It's certainly given me something to think about. Thanks.

Re:Cognitive Biases in Programming by mikeash on Sunday October 15, 2017 @04:07PM (#15479022)
What I've seen isn't even a matter of worrying about being wasteful with CPU cycles, it's a gut reaction against it because it feels like more work.
Re:Cognitive Biases in Programming by kruczek on Monday October 16, 2017 @07:31AM (#15482456)
Well, it's a good gut reaction to have. As you wrote yourself "In reality, both are equally easy to write" - therefore the first one is a good default to assume, because in some cases it may actually matter.

If you choose good default, then you won't spend any more time thinking about it; if you have bad defaults, then you will have to figure out where the slowdown comes from and eliminate it.

Re:Cognitive Biases in Programming by mikeash on Monday October 16, 2017 @08:50AM (#15482811)
It's a bad gut reaction to have for a newer programmer who is struggling to find any solution to the problem at hand.
Re:Cognitive Biases in Programming by kruczek on Tuesday October 17, 2017 @06:33AM (#15489883)
So, if I understand your point correctly, you mean that newer programmers, who are struggling to find any solution at all, would go for the (subjectively) easier one, i.e. the one with f1() outside of the loop?

I agree they would go for the easier one, but I don't understand why they would perceive the first version to be easier. And even if they would, why is it bad?

Re:Cognitive Biases in Programming by dualogy on Sunday October 15, 2017 @10:34AM (#15477254)
What kind of example is this, the 1st is an entirely different 'program' than the 2nd? Unless f1 is a no-op.
Re:Cognitive Biases in Programming by mikeash on Sunday October 15, 2017 @10:37AM (#15477262)
Yes, they're not the same program, but they're equally easy to write. I see people shying away from doing work in each iteration of the loop because of a feeling that it's more difficult to write the code.
Re:Cognitive Biases in Programming by metaobject on Sunday October 15, 2017 @11:29AM (#15477528)
Assuming f1() has side effects, those two blocks of code may be very different.
Re:Cognitive Biases in Programming by mikeash on Sunday October 15, 2017 @12:41PM (#15477885)
Of course. The point is that people seek solutions of the first form thinking that it’s easier to write.