Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables Introducing Variables

Box concept is slightly flawed

If you put something into a box, say you put an apple into a box, then you decided to put a banana in the box, you would have two things in the box, but in programming you don't type something like var = (clear) before you enter a new variable, so the box concept is slightly flawed

5 Answers

Eric M
Eric M
11,546 Points

If my box variable is a dynamically sized array I can put both a banna and an apple in there. In some langauges I could even use the + operator and ignore the indexes, then say for each item in the box, take it out, or eat it, whatever.

For instance, a string is a variable, it's an array of characters, you can put a in a string, then z, then c.

let box = "a";
box += "z";
box += "c";

Metaphors for coding are to help get your head around a concept, not explain it perfectly. A perfect description of a programming function is like a mathematical proof - unerringly accurate but not necessarily helpful if you don't already understand it!

In Code Complete Steve McConnell finishes up Chapter 2 with the following key points (among others):

  • Metaphors are heuristics, not algorithms. As such, they tend to be a little sloppy.
  • Metaphors help you understand the sofwtare-development process by relating it to other activities you already know about.
  • Some metaphors are better than others.
  • Metaphors are not mutually exclusive.
  • Use the combination of metaphors that works best for you.

I like that last one!

Yea I know they are not supposed to be perfect, I was just bored so I decided to find a flaw

Ah none of you are understanding what I mean, I don't have a problem, it was just an observation

I understand. I'm similar in this way and most people never 'get it'.

Adam Beer
Adam Beer
11,314 Points

I guess he think he didn't save the object with dynamically. And when he logged out the variable content, he see no more items, like this, banana and apple.

// this is what he expect
console.log('variable name'); //Log out -> banana, apple

// that is what he got it
console.log('variable name'); //Log out -> apple
Lesley Bulbeck
Lesley Bulbeck
1,583 Points

Hi Nick Evershed,

By var = (clear) I think you meant setting the variable to nothing before setting it to something different, so the values don't add up in the box (following the analogy)

That's a valid thing to do, by the way - it's called setting a variable to null

As for analogies, they are all flawed in some way or another, aren't they? I've discovered this blog post on medium that uses lots of analogies to explain things.

What do make of it? Useful or not?

Jennifer Mitchell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Mitchell
Full Stack JavaScript Techdegree Student 3,917 Points

To me, the box model makes good sense when you know how the assignment operator works. If I assign an apple to a box:

var box = apple;

and then I decide to assign a banana to the box:

box = banana;

I have not added the banana. I have made the box "equal to" a banana. To add a banana to the box alongside the apple, I would use:

box += banana;

It's a metaphor that works for me.

I did in my original post