How much do you use domain-specific types?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 990 Views - Last Post: 23 March 2020 - 01:31 AM

#1 Yossu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-November 18

How much do you use domain-specific types?

Posted 24 February 2020 - 01:30 PM

I hope this sort of question is OK here. It's very opinion-based, not a direct programming question.

I've been reading about DDD, and one of the topics that often comes up is domain-specific types. For example, if I have a Person class, instead of having an int property for their age, I would create an Age type, which could only be created via a constructor that validates the int passed in. That way, invalid state becomes impossible to represent, cutting down the amount of validation needed elsewhere.

This all looks very sensible, but what bothers me is the proliferation of types. I can see that even a modest-sized project would end up with hundreds of types.

How far do people actually go with this in practice? I like the idea, but want to know if it's really done like this.

Thanks for any replies.

Is This A Good Question/Topic? 0
  • +

Replies To: How much do you use domain-specific types?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15743
  • View blog
  • Posts: 63,068
  • Joined: 12-June 08

Re: How much do you use domain-specific types?

Posted 24 February 2020 - 01:39 PM

FYI for anyone: DDD == Domain-driven design
https://en.wikipedia...n-driven_design
Was This Post Helpful? 0
  • +
  • -

#3 Yossu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-November 18

Re: How much do you use domain-specific types?

Posted 24 February 2020 - 01:42 PM

Sorry, my bad, I thought that was a common abbreviation.
Was This Post Helpful? 0
  • +
  • -

#4 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1474
  • View blog
  • Posts: 4,327
  • Joined: 12-May 09

Re: How much do you use domain-specific types?

Posted 24 February 2020 - 01:43 PM

Another reason those types can be useful is when you have a constructor that takes e.g. several ints or several strings - it makes it harder to send the address to the name field by sending them in the wrong order. However, I find that supporting named arguments (like in python) largely solves this problem for me.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15743
  • View blog
  • Posts: 63,068
  • Joined: 12-June 08

Re: How much do you use domain-specific types?

Posted 24 February 2020 - 01:43 PM

Could have been mistaken for Diners, Drive-ins, and Dives. ;)

(Not to be confused with DDDD)
Spoiler

Was This Post Helpful? 1
  • +
  • -

#6 Yossu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-November 18

Re: How much do you use domain-specific types?

Posted 24 February 2020 - 01:49 PM

View Postxclite, on 24 February 2020 - 01:43 PM, said:

Another reason those types can be useful is when you have a constructor that takes e.g. several ints or several strings - it makes it harder to send the address to the name field by sending them in the wrong order. However, I find that supporting named arguments (like in python) largely solves this problem for me.


Yeah, that was one of the other big advantages they offered. I forgot to mention that one in my post, thanks.

But again, how far do you take this? I use C#, and Visual Studio gives me full details of the parameters as I'm typing, even without using named arguments, so I'm highly unlikely to pass an address to the name field. That's why I'm wondering if people really do this, or it's just something you see in the books.
Was This Post Helpful? 0
  • +
  • -

#7 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1474
  • View blog
  • Posts: 4,327
  • Joined: 12-May 09

Re: How much do you use domain-specific types?

Posted 24 February 2020 - 03:17 PM

People mess it up all the time. However, it's really just another failsafe to me. I don't really go as far as making a Name class. Code review, tests (especially integration tests), and consistency in argument ordering can do wonders here.
Was This Post Helpful? 0
  • +
  • -

#8 Yossu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-November 18

Re: How much do you use domain-specific types?

Posted 24 February 2020 - 03:28 PM

View Postxclite, on 24 February 2020 - 03:17 PM, said:

People mess it up all the time.

Yup, which I guess is the motivation for such ideas. I was just pointing out that it's not as big a problem as is sometimes made out. As you said, code reviews, tests, etc go a long way to catching these things.

View Postxclite, on 24 February 2020 - 03:17 PM, said:

However, it's really just another failsafe to me. I don't really go as far as making a Name class.

But do you use it at all? If so, how much?

Failsafes are a good thing, but only if they don't cause more work than they save. I'm really not sure about this one.

View Postxclite, on 24 February 2020 - 03:17 PM, said:

Code review, tests (especially integration tests), and consistency in argument ordering can do wonders here.

Thanks for the reply.
Was This Post Helpful? 0
  • +
  • -

#9 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1474
  • View blog
  • Posts: 4,327
  • Joined: 12-May 09

Re: How much do you use domain-specific types?

Posted 24 February 2020 - 04:46 PM

I guess for me if I see a Name class I'm bracing myself for The Kingdom of Nouns. I just think it's overkill and if your class takes 50 strings just make a builder or something.
Was This Post Helpful? 1
  • +
  • -

#10 Yossu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-November 18

Re: How much do you use domain-specific types?

Posted 25 February 2020 - 07:19 AM

View Postxclite, on 24 February 2020 - 04:46 PM, said:

I guess for me if I see a Name class I'm bracing myself for The Kingdom of Nouns. I just think it's overkill and if your class takes 50 strings just make a builder or something.

That's what I thought. I guess there are places where it could be useful, but not for something like a Name class.

Thanks
Was This Post Helpful? 0
  • +
  • -

#11 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3136
  • View blog
  • Posts: 11,945
  • Joined: 03-December 12

Re: How much do you use domain-specific types?

Posted 25 February 2020 - 07:49 AM

There are value types, but an Age type is not needed and over engineering at its finest. It is a value object however.

Because of our scale, we use DDD and microservice architecture.
Was This Post Helpful? 0
  • +
  • -

#12 Yossu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-November 18

Re: How much do you use domain-specific types?

Posted 25 February 2020 - 08:46 AM

View Postastonecipher, on 25 February 2020 - 07:49 AM, said:

There are value types, but an Age type is not needed and over engineering at its finest. It is a value object however.

When (if at all) would you use custom types?

Thanks
Was This Post Helpful? 0
  • +
  • -

#13 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3136
  • View blog
  • Posts: 11,945
  • Joined: 03-December 12

Re: How much do you use domain-specific types?

Posted 25 February 2020 - 09:04 AM

That is too generic a question. Everything in development and software architecture is a trade off of some sort. Sticking to DDD, does it need to be an entities or a value object, or something else? I don't know what type of studying you have done so far, but the biggest advantage of it, is separated concerns, isolating the domain to only what the domain needs to know or control, and using the same language so there is no confusion when anyone speaks.



https://airbrake.io/...n-driven-design
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7407
  • View blog
  • Posts: 24,985
  • Joined: 05-May 12

Re: How much do you use domain-specific types?

Posted 25 February 2020 - 11:15 AM

But don't you need a full blown Name class to handle all the internationalization issues with regards to names: first name vs. last name first. More than just 2 or 3 names. Just one name. Suffixes. Titles. Etc.

Don’t call me by my first name! Naming conventions in different countries
Was This Post Helpful? 0
  • +
  • -

#15 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1027
  • View blog
  • Posts: 3,951
  • Joined: 13-June 14

Re: How much do you use domain-specific types?

Posted 28 February 2020 - 11:56 AM

We do this. Our domain model is quite complicated and I think expressing it in the codebase helps to understand what's going on. A lot of our types have validation logic and I think the correct place for testing that is in the code that constructs those objects - I like my test pyramid the right way up! I do agree that you then have a proliferation of these types, which to me only seems annoying when I want to create test data. That problem tends to be easily solved by hiding that in helper functions, though.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2