Skip to main content
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 ...
Matthew Drury's user avatar
50 votes

How many design patterns and levels of abstraction are necessary?

How many ingredients are necessary for a meal? How many parts do you need to build a vehicle? You know that you have too little abstraction when a little implementation change leads to a cascade of ...
Arseni Mourzenko's user avatar
47 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 // ...
Martin K's user avatar
  • 2,897
35 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 ...
Jerry Coffin's user avatar
  • 44.5k
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 &...
candied_orange's user avatar
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

Is dependency injection by hand a better alternative to composition and polymorphism?

The best article I've ever read on the topic was James Shore's. I've been doing "DI by Hand" for ages, as part of abstraction and polymorphism. So after I got into the professional world and I kept ...
Nick's user avatar
  • 741
24 votes

How many design patterns and levels of abstraction are necessary?

The problem with design patterns can be summed up with the proverb "when you're holding a hammer, everything looks like a nail." The act of applying a design pattern is not improving your program ...
Neil's user avatar
  • 22.8k
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 ...
Filip Milovanović's user avatar
22 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 DateOfBirth object and a ...
VoiceOfUnreason's user avatar
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 ...
Nicol Bolas's user avatar
  • 11.9k
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 /...
coolpasta's user avatar
  • 641
12 votes
Accepted

C++ derived class copy constructor

The copy constructor – by very definition – always takes a reference to the exact same type it was declared for. So the copy constructor signature for Coffee would be Coffee(const Coffee&). Any ...
amon's user avatar
  • 134k
12 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/...
schrödingcöder's user avatar
11 votes

Limiting your call stack can guide you to use less abstraction?

I agree that abstractions influence the call stack in the way you described. I also agree that limiting the call stack can give you less abstractions. But there are two major reasons for me to not ...
Frank's user avatar
  • 14.4k
11 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 ...
Neil's user avatar
  • 22.8k
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 ...
Robert Harvey's user avatar
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 ...
David Arno's user avatar
  • 39.3k
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 ...
Robert Harvey's user avatar
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 ...
CharonX's user avatar
  • 1,643
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 ...
Adam B's user avatar
  • 1,595
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 ...
Deduplicator's user avatar
  • 9,031
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 ...
Ewan's user avatar
  • 75.8k
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 ...
David Arno's user avatar
  • 39.3k
8 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 ...
Christophe's user avatar
  • 77.3k
8 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 ...
Vector Zita's user avatar
  • 2,442
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 ...
Arseni Mourzenko's user avatar
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 ...
Martin Maat's user avatar
  • 18.4k
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 ...
candied_orange's user avatar
6 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 ...
Martin Thoma's user avatar

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