views:

55

answers:

3

Hi,

How do you recommend to work with long variables in Javascript? Thanks!

For example...

window.location.hash.substring(2).split("/")[0].something().hello;

object["object"].object(object.hello.how.are.you[another.object(function.inisde())]);

My solutions

  • Use with() { (...) }
+3  A: 

If you're going to be using it more than once, definitely assign it to another variable

var simple = object.hello.how.are.you[another.object(function.inisde())];
Sam Dufel
Especially if one of the array keys is the result of a function call that isn't going to change while you're using the var.
Alex JL
+2  A: 

I find it useful (in any language) to NOT chain method calls together in most cases. The cons to doing this are

1) If there is some sort of null exception, it is difficult to know where it occurs, especially if the code looks like

if (a.b.c.d.e || f.g.h.i.k) { ... }

then your NPE like exception could have happened in any of 10 places. Imagine if the variables are more than 1 letter long.

2) The code is less readable this way. The following is infinitely more readable.

var b = a.b,
    c = b.c,
    d = c.d,
    e = d.e;
var conditionOne = e.isTrue()

you don't necessarily need to create a var for every level, but you could for levels that make sense.

3) Its easier to see what is going on in a debugger if the variables are separated out.

The bottom line is, with some extra typing your code is a lot more readable and a lot more maintainable.

hvgotcodes
+1  A: 

If you have to use expressions like

object["object"].object(object.hello.how.are.you[another.object(function.inisde())]);

it usually means code is badly designed. :) The usual method to make this code comprehensible is to use variables. It requires more code, but at least it becomes readable.

var anotherObject = another.object(function.inisde())
var hello = object.hello.how.are.you[anotherObject];
object["object"].object(hello);

If the expression is linear, you can easily split it into several lines.

window.location.hash.substring(2)
    .split("/")[0]
    .something().hello;
Athari
I strongly disagree that having long variable paths is a sign of bad code. I'd even say that this could be proof of a well thought out code structure.
Georg
@Georg There're situations where long paths are used with purspose, like fluent interface, but the example provided in the question (the one "how.are.you") is obviously bad smell; some of call chains should be encapsulated. This code is hard to read and understand, one can make it more readable and understandable without losses, therefore it is bad and should be fixed.
Athari
I want to add that the given example will even become faster when accessing objects containing objects containing objects [etc.] using an extra variable (here `anotherObject`), as the JS engine doesn't have to resolve all those objects (because of all those `.` operators).
Marcel Korpel
Marcel, thanks for info! I didn't know that.
Randy Hartmanec
@Athari: That is absolutely true. Such code can be formatted in much better ways. I just read your answer as if having long variable paths was generally a code smell case. Obviously, that's not what you wanted to say.
Georg