0

I was working on some code recently. Here's the gist -

class Student {
   private String name;
   private List<Performance> performances;
}

class Performance {
   private Student student;
   private List<Integer> perfData;
}

Student & Performance have a circular dependency. Is this bad? How do we prevent / break this up without losing the ability to find all performances of a Student and given a Performance map it back to a Student.

Null
  • 384
  • 2
  • 14
  • Personally, a `Student` shouldn't care what performances they have, as that's maintain by the `Performance` class. May some kind `Schedule` or `PerformancesManager` would be more useful to hold the list of performances, which could provide look ups to list which performances a given `Student` is assigned to...as a thought. Maybe, instance of `Performance` should have a `List` of `Students`? – MadProgrammer Jun 06 '14 at 07:05
  • 1
    No, circular dependencies are not per se bad, this is only a modeling perspective - for example, if you want to model relationships between persons, where a person object has references to other person objects (self-reference), then a circular/reflexive dependency is inevitable. – Smutje Jun 06 '14 at 07:09
  • It is a question of your software design. And if you want to do it, it is perfectly OK. I saw things like that a hundred times in software and did it myself. – Thomas Uhrig Jun 06 '14 at 07:09

1 Answers1

2

The question you should always ask yourself when doing these things is: would my design, in a real-world-scenario, make sense? And before someone jumps in to correct me, there are cases where design does not map to logic, and there are cases where it does.

When I looked at your code, the first thing which came to mind was how are things stored inside the university software. You need to have information about students, performance evaluation per student and a lot of other inter-connected platforms (email, enrolled courses and so on). This means, that for a given student there are some Performances which have some data. So they belong to the student, so what you did there is good by linking the student to the performance. What I think is wrong is the fact that you may be using that Performance somewhere where you shouldn't and are trying to map it back to the student. Having no bigger picture leaves room for interpretation.

Now, what you could do to decouple the system, if we are to imagine a bigger picture would be to assign an id to a student.

class Student {
   private String name;
   private String address;
   private String city;
   private String nationality;
   private Data dob;
   private long studentId;
} 

class Performance {
   private long studentId;
   private List<Integer> perfData;
}

class PerformanceManager{
    private List<Performance> performances;

    public List<Performance> getPerformanceForId(long id);
}

class MailItem{
    private String to;
    private String message;
    private long studentId;
}

class MailManager{
    private List<MailItem> mails;
    private List<MailItem> getMailForId(long id);
}

Of course, the above example is just a simplification, but the idea is that systems which have nothing to do with each other (Performance and Mail) are linked through an id, not through a reference.

Daniel
  • 179
  • 2
  • 9