68
votes
Accepted
When is it appropriate to introduce a new layer of abstraction into a class hierarchy?
I've built a 2D roguelike from pretty much scratch, and after lots of experimentation, I used an entirely different approach. Essentially an entity component architecture.
Each game object is an ...
48
votes
When is it appropriate to introduce a new layer of abstraction into a class hierarchy?
This is why we often like interfaces over inheritance: Many real-world problems cannot be modeled in an object hierarchy.
interface IMove
{
// returns an intermediate location chosen with
// ...
37
votes
Accepted
What is "premature abstraction"?
At least in my opinion, premature abstraction is fairly common, and was especially so early in the history of OOP.
At least from what I saw, the major problem that arose was that people read through ...
34
votes
Accepted
How to comprehend abstraction in code?
Programming concretely is the impulse to pull details towards you so you can nail them all down in one place. We all start this way and it's hard to let go.
Programming abstractly is most definitely &...
31
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 ...
23
votes
When is primitive obsession not a code smell?
Primitive Obsession is using primitive data types to represent domain ideas.
The opposite would be "domain modeling", or perhaps "over engineering".
Would you create a ...
23
votes
Accepted
Where should interfaces be used?
I don't think there's really a simple answer that can do a substantially better job than the statements of the principles and practices that you've already encountered.
I know you're aware of this on ...
18
votes
Usage of the word "override" in C++ and it's virtual functions
C++ has a rule about overridden functions: they don't have to be explicitly declared as virtual. Consider your example without override: double getAge(int id);. If there is a base class function ...
15
votes
What is "premature abstraction"?
It's important to remember that abstraction is a means to an end. You use abstraction to uniform behavior across your program and make the addition of new classes straightforward in cases where you'd ...
13
votes
When is primitive obsession not a code smell?
A possible rule of thumb may depend on the program's layer. For the Domain (DDD) aka Entities Layer (Martin, 2018), this might as well be "to avoid primitives for anything representing a domain/...
13
votes
How to comprehend abstraction in code?
At the bottom, there are some updates to how this fared for me every quarter of the year or so, I think they're valuable.
Good naming. Or, if it's someone else's code, trying to attribute good names /...
11
votes
Where should interfaces be used?
In a nutshell, you should define an interface when you need the ability to map that interface (i.e. capability) to one or more implementations.
What does that phrase mean?
As an example, if you're ...
10
votes
Accepted
What is the benefit of encapsulating a collection inside a class?
The problem with your original code example, is that Orders breaks the encapsulation of _orders as you are returning the list. Cast that IEnumerable<Order> to IList<Order> and code outside ...
10
votes
Is there a reason to define an interface for a pure data class?
When used in this manner, interfaces denote capabilities.
For example, the IEnumerable interface indicates that the class in question is capable of being enumerated, in this manner:
foreach(var item ...
9
votes
When is primitive obsession not a code smell?
To be honest: it depends.
There is always the risk of overengineering your code. How widespread will DateOfBirth and Salary be used? Will you only use them in three tightly coupled classes, or will ...
9
votes
Too much abstraction making code hard to extend
The usual saying that we all read here and there is:
All problems can be solved by adding another layer of abstraction.
Well, this is not true ! Your example shows it. I’d therefore propose the ...
9
votes
When is it appropriate to introduce a new layer of abstraction into a class hierarchy?
I would follow your first option, but then use the strategy pattern for the different move styles. This will allow you to swap move styles and alter moves styles easier moving forward.
So you’d have ...
9
votes
When is it appropriate to introduce a new layer of abstraction into a class hierarchy?
The answer to your main question:
in general, when is it appropriate to introduce a new layer of
abstraction at the cost of complicating the program architecture?
is relatively simple and ...
8
votes
When is primitive obsession not a code smell?
Better suffer from Primitive Obsession or being an Architecture Astronaut?
Both cases are pathological, in one case you have too few abstractions, leading to repetition and easily mistaking an apple ...
8
votes
Accepted
Is Abstracting your code too much a bad use of SOLID Principles?
I would side with your enemy on this one.
The 'create a database and table logic' is clearly technically a separate responsibility from the 'get data from database' logic.
You can imagine the creation ...
8
votes
Is there a reason to define an interface for a pure data class?
As the code currently stands,
public interface IResult
{
bool Succeeded { get; set; }
string ResponseMessage { get; set; }
}
we have an interface that replicates a simple data class. It adds ...
7
votes
Can too much abstraction be bad?
One thing that hasn't been mentioned so far is the KISS principle and YAGNI: Keep it simple and stupid; You aren't gonna need it.
Sometimes, there are very good reasons for abstractions:
You are ...
7
votes
Too much abstraction making code hard to extend
Whenever I see a method where the behavior switches on the type of its parameter, I immediately consider first if that method actually belongs on the method parameter. For example, instead of having a ...
7
votes
Accepted
What's an abstraction?
Probably an example could help.
Let's say you want to do an HTTP request to an API and process its JSON response. Such process involves, at software level:
Doing a DNS request in order to transform ...
7
votes
Where should interfaces be used?
You say you get nervous and lose time over-thinking the use of interfaces. And you seem to be seeking permission or an excuse to stop doing that.
I do not believe you need permission from anyone to ...
7
votes
Should I stop using jQuery and create custom abstractions?
Custom abstractions should feel custom. That is, they should take advantage of knowing your particular needs. Without that you’re simply replacing a well known library with an obscure one that has ...
7
votes
Accepted
C++ Is it okay to use nested classes as a way to namespace derived classes?
This is a very bad idea, because it breaks the Open/Closed Principle:
Classes should be open for extension, but closed for modification. This means that it should be easy to "extend" your ...
7
votes
Accepted
Confused on how abstraction and encapsulation is helpful
the function doesn't rely on assimp as much meaning it would be easier to change the assimp code or replace it with another library, but I still find it confusing how it's useful or helpful it just ...
6
votes
Is it good design to have one constructor that supplies a "default" concrete class to another that takes an abstraction?
Is there a name for this pattern?
Let's start with this question as the its name gives a succinct answer to your more general question. This is known as the Bastard Injection (Anti) Pattern. ...
6
votes
confused with abstraction definition?
Language features like interfaces and abstract classes and methods are "just" tools. Using them is not the actual act of abstraction.
An abstraction as the book is correct to point out is the act of ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
abstraction × 220object-oriented × 45
design-patterns × 24
design × 21
interfaces × 20
c# × 18
object-oriented-design × 17
java × 16
encapsulation × 14
inheritance × 13
c++ × 11
architecture × 9
c × 9
abstract-class × 7
database × 6
data-structures × 6
terminology × 6
dependency-injection × 6
clean-code × 6
class-design × 6
polymorphism × 6
php × 5
python × 5
unit-testing × 5
domain-driven-design × 5