DEV Community

Cover image for Code Smell 134 - Specialized Business Collections

Code Smell 134 - Specialized Business Collections

Maxi Contieri on May 22, 2022

If it walks like a duck and it quacks like a duck, then it must be a duck TL;DR: Don't create unnecessary abstractions Problems Ove...
Collapse
 
gorynych profile image
Stepan Mozyra

Actually,.. there's a mix of two different things.

1) Should we use "Specialized Business Collections"? - Yes, please. Do it whenever you can. Do not use strip PHP associative arrays, do not use Java standard collection interfaces like List<>, Map<>, Set<> etc.

2) Should we reimplement something? ;) Probably, not. Please do not reimplement TreeMap or ArrayList without really good reason for that.

Collapse
 
mcsee profile image
Maxi Contieri

IMHO

1) Not, why?
Specialized Collection do not bring additional benefits unless we have strong evidence.

2) Until we find differential responsibilities

Collapse
 
gorynych profile image
Stepan Mozyra • Edited

1) The most valuable benefit - it's extending your dsl. The same purpose like Value Objects in DDD - we can use UUID anywhere, but we want to wrap it with some WalletId class. I want to clearly distinguish in my code both list of strings - Dictionary and MyNames. I do not think anybody will appreciate something like List < Map < String, Map < Integer, String>>> :))

Thread Thread
 
mcsee profile image
Maxi Contieri

Extension should be made when protocol is different.

Dictionary and MyNames might be primitive obsession code smell. But this is the case when we need specialized behaviour. The quest is to find this behavior

Thread Thread
 
gorynych profile image
Stepan Mozyra

Pardon, don't get it. What protocol do you mean?

Thread Thread
 
mcsee profile image
Maxi Contieri

Protocol is the set of functions an object understands, AKA behavior

Is the only important thing we need to know in order to program

Thread Thread
 
gorynych profile image
Stepan Mozyra

Aha, thanks.

I tend to generalize it a bit - the only important thing is the Domain. So, if we have somewhere in our domain "MyNames" thing - we should reflect it in our code. Even if it will be just one line, like

public interface MyNames extends List {}

(of course, it's better to have

public interface MyNames extends List {} )

Protocol extension could come after that, but not have to. The better code should looks like domain expert speech, and there's no lists of strings ;).

Collapse
 
moopet profile image
Ben Sinclair

In a situation like the one you gave as an example, the tests aren't needed - they're verifying that stuff works that's natively part of the language. I know it's a simple example, but if you don't add anything, why would you need the tests? Or the namespace for that matter? Wouldn't the "Good" variant be nothing at all?

Collapse
 
mcsee profile image
Maxi Contieri

That is the point !!

Collapse
 
villelmo profile image
William Torrez

What is an abstraction?

Collapse
 
mcsee profile image
Maxi Contieri

Something you see in real world and create to mimic its behavior