tags:

views:

175

answers:

4

Is it bad javascript practice to not assign a newly created object to a variable if you're never going to access it?

For example:

for(var i=0;i<links.length;i++){
    new objectName(links[i]);
}

And again, I won't be accessing it, so there's no need for a variable to reference it.

+3  A: 

That’s absolutely fine if you don’t need to use it again.

Gumbo
+11  A: 

If you're not accessing it but it's still useful, that suggests that the constructor itself has visible side effects. Generally speaking, that's a bad idea.

What would change if you didn't call the constructor at all?

If your constructor is doing something to the global state, that strikes me as very bad. On the other hand, you could be using it just for the sake of validation - i.e. if the constructor returns without throwing an exception, it's okay. That's not quite so bad, but a separate method for validation would make things a lot clearer if that's the case.

Jon Skeet
new ColorModulator('blue').displayText('hello world').
le dorfier
The constructor and object itself is in a var/object, so it's not in the global state.
Jeff
If I didn't call the constructor at all, then I wouldn't get independent instances of the animations that I want on each element passed to it.
Jeff
@Jeff: So you're saying that when you call a constructor, it adds itself (or something similar) to the argument passed to it? That feels somewhat smelly to me.
Jon Skeet
@Jon Skeet: Maybe I am not understanding it correctly. I am not adding anything really, I am just using the for loop to create separate instances of an object that will handle an animation for each element that is passed through the constructor. I have no reason to access the object that handles the animation, and in order to keep the animations from conflicting with each other I just made it work through separate instances.
Jeff
This is a common case. You create a `new Animation(some_element)` and maybe you'll need to keep hold of the Animation instance so you can call `animation.stop()` on it. But maybe in this case you're not ever going to want to stop the animation. In which case there is no harm in throwing it away.
bobince
Sam - it's fine, you don't need a variable. Skeet is only correct *almost* all the time. But he gets a lot of reflexive up-votes.
le dorfier
@bobince @le dorfier: So, it sounds like it's fine and won't cause any harm. @Bobince: And I have the object itself stopping the animation with a event handler, so that won't be an issue.Thanks for all the replies and input, I'll take it as of now that it's fine unless otherwise disputed.
Jeff
@Jeff: Can you create a static method (assuming such things exist in JavaScript) to do this instead? That would make it clearer that you're expecting side-effects than just calling the constructor.
Jon Skeet
@Jon Skeet: If I created a static method for each element to reference to, how could I have the animations act independently?
Jeff
@Jeff: I'm not sure what you mean. I'm just suggesting a static method which just called the constructor, with a comment explaining it. You then call the static method from the loop. How would that stop the animations from being independent?
Jon Skeet
@Jon Skeet: Maybe this I don't understand, but what would be the difference from calling the constructor from the loop, or a static method that is called from the loop? Thanks for all your input btw.
Jeff
@Jeff: Calling a normal method looks like you *deliberately* want the side-effects. Side-effecting methods are expected. Calling a constructor and then discarding the results looks like something that can be removed harmlessly (or is a bug) because *usually* constructors don't have side-effects. It feels to me like a method which explicitly documents that it's calling the constructor for the sake of its side effect is clearer than putting it in the loop. Mind you, I'd personally then try to make the constructor itself side-effect free, and put the side-effect in the static method :)
Jon Skeet
I'm not convinced. Would you be happier with `new Animation(element).start()`?
bobince
@bobince: That would look less odd, yes. It's still not clear to me where the state is, or whether it really is changing global state (Jeff's original comment didn't make much sense to me). But basically calling `start()` suggests a side-effect much more than just calling a constructor.
Jon Skeet
@Jon Skeet: I can see what you mean about deliberately wanting to call the constructor without assigning it to a variable. I am slightly confused on what you mean when you use the term "side-effects". Are you referring to random bugs that might pop-up and break the code?
Jeff
@Jeff: Not really; I'm referring to anything that the code does which isn't captured by its return value. In a purely functional programming language, for example, side-effects are largely forbidden: if you don't use the return value, it's as if you never called the function, and if you call the function multiple times it makes no odds. Obviously JavaScript is *not* a functional language, but constructors are *usually* somewhat "pure" in that sense.
Jon Skeet
static methods not requiring instantiation is not a mappable concept to javascript.
le dorfier
@le_dorfier: Darn - that certainly makes it harder :(
Jon Skeet
+1  A: 

"Is it bad javascript practice to not assign a newly created object to a variable if you're never going to access it?"

I feel it is bad practice to make an assignment that is not needed, and, I would argue, not just for javascript but in general. If there are side-effects you want, getting them from the action of an assignment is bad practice for the simple reason that it would be fairly opaque from a maintenance point of view.

.

Richard T
So, are you saying that since the assignment is not needed, that this is fine/acceptable?
Jeff
Too many pronouns: What's "this?" What I'm saying is that because the assignment appears to serve no function (purpose) it is bad form to do it, so don't.
Richard T
@Richard T: Sorry, I'll try to be more understanding. You're saying that the assignment (var something = newObject(links[i]);) serves no purpose, so don't do it and that it's fine/acceptable to just create an object without assigning it to a variable?
Jeff
Sorry, Jeff, for not noticing your comment earlier. ... I think somehow I am not being understood, so I'll try afresh: If an assignment serves no purpose at all, don't do that, and, likewise, if the creation of an object serves no purpose at all, don't do that either. If however, either the creation of an object or the action of an assignment isn't of direct use but has some side-effect that's desired, then I'd recommend finding another way so that it's more clear in the code what you're trying to do. For example, if trying to cause an event to fire maybe do that directly if possible.
Richard T
A: 

It seems like your constructor is doing something else besides creating/initializing an object.

It would be a cleaner solution to implement that extra functionality into a function or method.

Constructors should be used to create and initialize objects.

Techpriester
I disagree. I think constructor should do everything needed to make the object "usable", which may involve a lot of complicated code.
Kristopher Johnson
How does it seems that the constructor is doing something else beside creating an object?
Jeff
@kristopher: You're right, a constructor should do initialization. After all that's what it's for. Maybe I should've pointed that out more clearly.
Techpriester
@Jeff He instantiates a new object without storing it anywhere. if the constructor wouldn't do anything besides object creation and initialization, that call would be totally useless.
Techpriester
@Techpriester: I don't see how it's totally useless because is that's all it is doing in object creation and initialization. Isn't that what you originally said?
Jeff
@Jeff: It'd be useless, because useless the constructor does something outside it's own scope (which would be bad practice), calling it without assigning the new object to a variable will do exactly nothing.
Techpriester