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 ...
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 ...
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....
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 ...
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. ...
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 ...
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 ...
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> ...
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 ...
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 ...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 (...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 = ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
class-design × 451object-oriented × 139
object-oriented-design × 90
c# × 77
design × 62
design-patterns × 52
java × 50
c++ × 49
class × 35
python × 31
inheritance × 23
class-diagram × 23
uml × 19
php × 18
architecture × 16
programming-practices × 15
coding-style × 15
single-responsibility × 15
methods × 13
solid × 12
refactoring × 11
interfaces × 11
constructors × 10
database × 9
encapsulation × 9