160 votes
Accepted

How to warn other programmers of class implementation

In such cases, it is best to use the type system of your language to help you with proper initialization. How can we prevent a FooManager from being used without being initialized? By preventing a ...
amon's user avatar
  • 134k
121 votes
Accepted

When using the Single Responsibility Principle, what constitutes a "responsibility?"

One way to wrap your head around this is to imagine potential requirements changes in future projects and ask yourself what you will need to do to make them happen. For example: New business ...
John Wu's user avatar
  • 26.4k
107 votes

What would be the disadvantage to defining a class as a subclass of a list of itself?

Frankly, I don't see the need for inheritance here. It doesn't make sense; Node is an ArrayList of Node? If this is just a recursive data structure, you would simply write something like: public ...
Robert Harvey's user avatar
80 votes

How to warn other programmers of class implementation

The most effective and helpful way to prevent clients from "misusing" an object is by making it impossible. The simplest solution is to merge Initialize with the constructor. That way, the object ...
JacquesB's user avatar
  • 58.7k
79 votes

When using the Single Responsibility Principle, what constitutes a "responsibility?"

Practically speaking, responsibilities are bounded by those things that are likely to change. Thus, there's no scientific or formulaic way to arrive at what constitutes a responsibility, unfortunately....
svidgen's user avatar
  • 14.6k
68 votes

Best Practice - Avoid naming class and field the same?

Bart van Ingen Schenau has some good advice, but I'd like to offer some additional advice. Don't universally avoid naming a class and property the same, but definitely question it. Consider all of the ...
Greg Burghardt's user avatar
58 votes

What would be the disadvantage to defining a class as a subclass of a list of itself?

The “horrible for memory” argument is entirely wrong, but it is an objectively “bad practice”. When you inherit from a class, you don't just inherit the fields and methods you are interested in. ...
amon's user avatar
  • 134k
47 votes

Is it a bad idea have make a class method that is passed class variables?

Calling a class method with some class variables is not necessarily bad. But doing so from outside the class is a very bad idea and suggests a fundamental flaw in your OO design, namely the absence ...
Christophe's user avatar
  • 76.7k
47 votes
Accepted

Constructing an object: should I expose or hide parameters passed to the constructor?

The calling code which instantiated a FooRepository object is passing an IDbConnection object and therefore has the right to access this information later on This is not true when you're dealing with ...
Flater's user avatar
  • 48.7k
40 votes

Best Practice - Avoid naming class and field the same?

Fields/properties/attributes within a class should describe something of the concept that you are modelling with that class. You should be able to ask questions like "What is the <property> ...
Bart van Ingen Schenau's user avatar
39 votes

How to warn other programmers of class implementation

I would normally just check for intiialisation and throw (say) an IllegalStateException if you try and use it whilst not initialised. However, if you want to be compile-time safe (and that is ...
Brian Agnew's user avatar
  • 4,676
33 votes
Accepted

Is it a bad idea have make a class method that is passed class variables?

There are may things with the class that I would do differently, but to answer the direct question, my answer would be yes, it is a bad idea My main reason for this is that you have no control over ...
Peter M's user avatar
  • 2,029
32 votes

When using the Single Responsibility Principle, what constitutes a "responsibility?"

I follow "classes should have only one reason to change". For me, this means thinking of harebrained schemes that my product owner might come up with ("We need to support mobile!", "We need to go to ...
Telastyn's user avatar
  • 109k
31 votes
Accepted

When and why to use Nested Classes?

The main feature of nested classes is that they can access private members of the outer class while having the full power of a class itself. Also they can be private which allows for some pretty ...
Esben Skov Pedersen's user avatar
29 votes

When using the Single Responsibility Principle, what constitutes a "responsibility?"

No one knows. Or at least, we are unable to agree on one definition. That is what makes SPR (and other SOLID principles) quite controversial. I would argue being able to figure out what is or isn't a ...
Euphoric's user avatar
  • 37.3k
28 votes

When and why to use Nested Classes?

Typically, a nested class N is created inside of a class C whenever C needs to use something internally which should never be (directly) used outside of C, and for whatever reason that something needs ...
Ixrec's user avatar
  • 27.7k
28 votes

Too much abstraction making code hard to extend

If I try to make a new method to handle B differently, it gets called out for code duplication. Not all code duplication is created equal. Say you have a method that takes two parameters and adds ...
candied_orange's user avatar
24 votes

How specific should the Single Responsibility pattern be for classes?

The single responsibility principle can be tricky to understand. What I've found useful is to think of it like how you write sentences. You don't try to cram a lot of ideas into a single sentence. ...
Vaughn Cato's user avatar
  • 1,069
24 votes
Accepted

Optional parameters or overloaded constructors

I prefer multiple constructors over default values and personally I don't like your two constructor example, it should be implemented differently. The reason for using multiple constructors is that ...
t3chb0t's user avatar
  • 2,524
24 votes
Accepted

How does encapsulation actually work?

Coupling ClassA relies upon the interface only, delegating this responsibility of passing the classB object elsewhere This is the idea. ...
Theraot's user avatar
  • 9,091
22 votes
Accepted

Giving a class many constructors and assigning via them as many properties as possible

Excuse me while I react to everyone suggesting the builder pattern here: This is C#, not Java! A main reason for Joshua Bloch's builder pattern is to hack around Java's lack of named arguments. This ...
candied_orange's user avatar
22 votes

How does encapsulation actually work?

Your confusion is probably caused by focusing on meaningless class and interface names, with no real usage scenario behind it. So better let us make a concrete example (I prefer C#, but it is not ...
Doc Brown's user avatar
  • 206k
22 votes

Pros and cons of representing routes as legs or stops?

but I'm wondering if using legs would be better When one has two alternative data structures representing the same thing, I would recommend against the assumption that one representation could be ...
Doc Brown's user avatar
  • 206k
21 votes
Accepted

Should reused exceptions types be favored over single use ones?

I know people make a big deal out of using exceptions for flow control but that isn't the biggest problem here. I see a huge command query separation violation. But even that's not the worst. No, the ...
candied_orange's user avatar
19 votes
Accepted

Best way to naming classes and packages in Java

Short answer: Naming two classes the same in the same project/library/context is not advisable. Aside from the obvious inconvenience, there is a deeper point why it isn't a good idea and neither is ...
Robert Bräutigam's user avatar
18 votes
Accepted

How to resolve class interdependence in my C++ code?

There are two parts in your question. The first part is the organization of C++ header files and source files. This is solved by using forward declaration and the separation of the class declaration (...
rwong's user avatar
  • 16.8k
17 votes

Design patterns for creating objects that have a list of objects that also have a list of objects

Do yourself a favor, and keep it simple and stupid! In your example, you wanted to model Cookbooks, having Chapters, containing Recipes, owning Directions". In terms of good OO design, you model ...
Doc Brown's user avatar
  • 206k
17 votes

What would be the disadvantage to defining a class as a subclass of a list of itself?

Extending a container in as of itself is usually accepted to be bad practice. There's really very little reason to extend a container instead of just having one. Extending a container of yourself just ...
DeadMG's user avatar
  • 36.9k
17 votes

Optional parameters or overloaded constructors

Considering the only thing you're doing in the constructor is simple assignment, the single constructor solution may be a better choice in your case. The other constructor provides no extra ...
Andy's user avatar
  • 10.3k
17 votes
Accepted

Can renaming a method preserve encapsulation?

I think you are missing the point. Its not saying you should rename the setter and getter, but to have methods which add and remove items from the fridge. ie public class Fridge { private int ...
Ewan's user avatar
  • 74.7k

Only top scored, non community-wiki answers of a minimum length are eligible