Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is circular reference between objects a bad practice?

I have a Model that will "carry" (Model.validator) a validator instance with it, and I need the Validator to have access to the Model's attributes. So, what I have come up with is the following

var Validator = function(model) {
    this.model = model;
};

var Model = function() {
    this._attributes = {};
    this.validator = new Validator(this);
};

var model = new Model();

This code creates a circular reference between those 2 objects. Is this a bad practice that will cause memory leaks? Any other ideas on how to implement it?

P.S. I have seen such circular references between objects in Angular.js scopes.

like image 970
Knorcedger Avatar asked May 02 '13 09:05

Knorcedger


People also ask

Are circular references bad?

Circular references aren't a bad thing in itself: you can use them to achieve complex calculations that are otherwise impossible to do, but first you must set them up properly.

Is it okay to have circular dependency?

and, yes, cyclic dependencies are bad: They cause programs to include unnecessary functionality because things are dragged in which aren't needed. They make it a lot harder to test software. They make it a lot harder to reason about software.

Why are circular references bad Java?

Objects with circular references are more difficult to unit test because they cannot be tested in isolation from one another. Object lifetime management.

What is a circular object reference?

A circular reference occurs when one heap variable contains a reference to a second heap variable, and the second one contains a reference back to the first. For instance, if A is an object, and somewhere in A, there is a reference to B, and within B is a reference back to A, there is a circular reference.


3 Answers

This kind of code will not cause memory leaks with today's browsers; as mentioned on MDN all major browsers have been shipping with mark-and-sweep GCs (that can handle cycles just fine) for some time now (e.g. Firefox itself has had a cycle collector since version 3).

From an architectural standpoint, this kind of code introduces moderately tight coupling between the two objects (if one changes in even a minor way, the other needs to be reviewed to determine if it needs to change as well) and should consequently be avoided if possible. But there is nothing inherently wrong with it.

like image 60
Jon Avatar answered Oct 04 '22 10:10

Jon


It will not be a problem for garbage collection: any new Garbage Collector (>IE6) will handle circular references just fine!

It might be a problem though if you are doing recursive functions, or printing the object.

So the answer is: it is no problem unless you screw up yourselves :-)

like image 30
Willem Mulder Avatar answered Oct 04 '22 11:10

Willem Mulder


There will not be any problems I'm sure. The most browsers' JS parsers can work with cycle dependecnies while garbage collecting. No more potential issues here.

like image 23
Sergey Metlov Avatar answered Oct 04 '22 10:10

Sergey Metlov