Skip to main content
123 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.9k
110 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
81 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
  • 15.2k
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
66 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
  • 135k
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
  • 80.6k
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
  • 56.3k
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
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,059
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
  • 110k
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.8k
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
25 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,592
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,201
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
  • 214k
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
  • 214k
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
20 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

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
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
  • 17k
17 votes

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

Adding to what has been said, there is a somewhat Java-specific reason to avoid this kind of structures. The contract of the equals method for lists requires a list to be considered equal to another ...
Lynn's user avatar
  • 886
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.4k
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
  • 79.8k
15 votes

When and why to use Nested Classes?

So why create a nested class? I can think of couple of important reasons: 1. Enable encapsulation Many times nested classes are implementation details of the class. Users of the main class should not ...
R Sahu's user avatar
  • 1,986
14 votes
Accepted

Large class with single responsibility

In my attempts to apply SRP to a problem I generally find that a good way to stick to a Single-responsibility-per-class is to choose class names which allude to their responsibility, because it often ...
Ben Cottrell's user avatar
  • 12.1k
14 votes
Accepted

Can the RxJava class Flowable legitimately have 460 methods?

TL;DL The lack of language features of Java compared to C# as well as discoverability considerations made us put source and intermediate operators into large classes. Design The original Rx.NET was ...
akarnokd's user avatar
  • 256
13 votes
Accepted

What is the proper place for complex business logic

"Graph" is an ambigous term, refering to different kinds of mathematical objects. But whatever kind of graph you have in mind, each of these objects is an abstraction on its own, with certain ...
Doc Brown's user avatar
  • 214k
13 votes
Accepted

How to respect Single responsibility principle and use exception handling at the same time?

Single responsibility ? There is a fundamental misunderstanding about SRP here: The single responsibility does not mean that a class should do only one thing, but that it should have only one reason ...
Christophe's user avatar
  • 80.6k
12 votes

Is there a better way of using getters and setters on private members of a class?

Using properties A proper getter and setter would look like this: public class Carrots { private string name; public string Name { get { return name; } set { name = ...
John Wu's user avatar
  • 26.9k
11 votes
Accepted

How to use 'Adapter' without any changes in the existing code in c++

1. Yes, but no: You could compile any code with a TVStyleAdapter instead of a TV, and it would work without a change in TV. So in a certain sense, the answer could be yes. But this compile-time ...
Christophe's user avatar
  • 80.6k

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