1 Replies - 6293 Views - Last Post: 22 July 2013 - 09:16 AM

#1 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2041
  • View blog
  • Posts: 4,223
  • Joined: 11-December 07

Mental Model v What Actually Happens

Posted 22 July 2013 - 01:04 AM

Inspired by this thread:
http://www.dreaminco...ng-whitespaces/

And given two programs when the variables are declared inside and outside a loop like this:

int x;
for(char c : myString.toCharArray()) {
  // some code here
}
for(char c : myString.toCharArray()) {
  int x;
  // some code here
}


The thread creator made the observation that the second snippet "resets" the variables on each iteration but the first snippet does not. I was about to offer the small correction that due to scoping, it is more like that the variables are destroyed and recreated on each iteration.

However, what would the observable difference be? No difference, I think. I do think my version of events is more semantically correct but recycling variables declared inside loops looks like an easy optimisation to implement. If it makes things faster then there is a good chance that javac makes that optimisation for you. And if javac doesn't then the Hot Spot compiler probably does.

Of course, I don't know if any of this is true but the point is that I don't know whose mental model is correct. This brings up some interesting discussion about reasoning about code, particularly when it comes to optimisation. If I can't even reason about what these trivial pieces of code are doing behind the scenes then I have no hope when it comes to complex code.

More importantly is what to tell the newbie?

Is This A Good Question/Topic? 1
  • +

Replies To: Mental Model v What Actually Happens

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7869
  • View blog
  • Posts: 13,347
  • Joined: 19-March 11

Re: Mental Model v What Actually Happens

Posted 22 July 2013 - 09:16 AM

Ultimately, your mental model is correct to the extent that it resembles the language spec and the VM spec - these are the definitions of "correct", after all. It is useful to the extent that it facilitates your reasoning about the code in the ways that matter to you.

These are different questions - a correct mental model of the code might produce far more detail than you need, and far too slowly than you care about, while a useful model produces answers that are "correct enough" in time to be useful. A correct model might give you a lot of noise, while a useful model presents insights pruned to your needs. Consider a chess game: there are a vast number of possible positions ten moves out from any position, but a good chess player is only thinking about the handful that matter - the good player has a useful model.

The useful model does a lot of background processing for you. For example, the useful model would raise an alarm if the int in your example were an object. In that case, it would want you to think about aliasing. For example:

myString = "Howdy!"
WidgetBox wb = new WidgetBox(); // a storage device for Widgets
Widget x = new Widget();  
for(char c : myString.toCharArray()) {
  x.setChar(c);
  wb.put(x);  // store the result
}


Ooops! We'll now have a box of bangs. Your mental model should be good enough to send off sirens and flares when you see this - but it should also be good enough to remain quiet when x is a primitive.

Obviously, I want you to think that a useful model is more important than a correct one. How do we build a useful model, and how do we help others to build one for themselves? I think the trick is to practice reasoning about code using correct models until the brain gets tired of repeating itself and optimizes. This is sort of like learning arithmetic, really. You drill a student on addition, not to torture them but so they'll eventually start to see answers "without thinking".

This is why I often go into far more detail than the question seems to warrant. I want to provide the deep context for the answer which will allow a novice programmer to see how they'd reason this out for themselves.

Quote

More importantly is what to tell the newbie?


I would want to give them enough to be able to reason about this and similar situations in the future.
This has the added advantage of making me think through the issue for myself, and perhaps learning something in the process.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1