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 ...
- 130k
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 ...
- 25.7k
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 ...
- 198k
79
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 ...
- 56.2k
78
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....
- 13.3k
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 ...
- 32.5k
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. ...
- 130k
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 ...
- 72k
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.6k
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> ...
- 70.3k
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 ...
- 4,666
35
votes
Accepted
Singleton or instantiate everytime I use?
If the class has no state, you could consider turning it into a function or static method depending on your language.
- 56.2k
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 ...
- 2,029
31
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 ...
- 108k
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 ...
- 35.9k
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 ...
- 97.8k
27
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 ...
- 27.5k
27
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 ...
- 5,018
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. ...
- 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 ...
- 2,496
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. ...
- 8,821
23
votes
Singleton or instantiate everytime I use?
It is sometimes considered that the singleton is an anti-pattern. Unless the problem being solved specifically requires the use of the singleton pattern, it is generally best to avoid it.
You mention ...
- 1,821
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 ...
- 97.8k
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 ...
- 194k
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 ...
- 194k
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 ...
- 97.8k
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 ...
- 11.3k
18
votes
Accepted
Python classes with only one instance: When to create a (single) class instance and when to work with the class instead?
Given a Python class which will be instantiated only once, i.e. there
will be only one object of the class. I was wondering in which cases
it makes sense to create a single class instance instead ...
- 5,855
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 (...
- 16.6k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
class-design × 434object-oriented × 133
object-oriented-design × 86
c# × 76
design × 59
design-patterns × 51
java × 49
c++ × 48
class × 34
python × 27
class-diagram × 23
inheritance × 21
uml × 19
php × 18
architecture × 16
programming-practices × 15
single-responsibility × 15
coding-style × 14
methods × 13
refactoring × 11
solid × 11
interfaces × 10
constructors × 10
database × 9
encapsulation × 9