45

Domain driven design has become my architecture of choice. I've been able to find a abundance of books & tutorials for applying DDD principles within the ASP.net framework. It mostly seems inspired from what Java developers have been doing for a good while now.

For my personal projects, I'm starting to lean more towards Python even though I'm finding it difficult to abandon static typing. I was hoping to find lots of help with applying DDD using a dynamic language. There doesn't seem to be anything out there about Python & DDD. Why is that? Obviously DDD can apply quite well to Python. Do people not take on as large of projects in Python? Or is applying DDD simply easier in Python given the dynamic typing therefore reducing the amount of required learning?

Perhaps my questionning is due to my lack of experience with Python. Any advice you might have for me will be appreciated.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
srmark
  • 7,942
  • 13
  • 63
  • 74
  • 6
    Having just read the Wikipedia page on domain-driven design I understand approximately as much of what it is as I did before. – Chris Morgan Nov 17 '10 at 05:54
  • 1
    Make this community wiki, please. – Odomontois Nov 17 '10 at 06:09
  • 3
    "I'm starting to lean more towards Python even though I'm finding it difficult to abandon strong typing" -> Python is strongly typed. – stefanw Nov 17 '10 at 10:23
  • 1
    @stefanw I guess he is thinking of static type checking. – daramarak Nov 17 '10 at 14:20
  • I'd say the "static" versus "dynamic" distinction has less to do with why DDD is associated with Java, C#, etc than those languages are associated more with "enterprise" software systems and contexts where both rapid change and low tolerance for failure co-exist. For example, DDD is actually fairly popular with some very large institutions on Wall Street that have a substantial python codebase. – C S Oct 14 '18 at 01:24

6 Answers6

20

I think it is definitely popular elsewhere, especially functional languages. However, certain patterns associated with the Big Blue Book are not as applicable in dynamic languages and frameworks like Rails tend to lead people away from ideas of bounded context

However, the true thrust of DDD being ubiquitous language is certainly prevalent in dynamic languages. Rubyists especially takes a great deal of joy in constructing domain specific languages - think of how cucumber features end up looking, that's as DDD as it gets!

Keep in mind, DDD is not a new idea at all, it was just repackaged in a way that got good uptake from C# and Java guys. Those same ideas are around elsewhere under different banners.

George Mauer
  • 117,483
  • 131
  • 382
  • 612
13

I think it is perfectly possible to write good DDD projects in dynamic languages, but is harder to maintain than in the static ones. Why?

Tooling

With static typed laguages the toolings are usually stronger. Thats why some people are using TypeScript instead of plain JS, because it helps you to scale your code by making refactorings easier. Refactoring is something present at everytime when you mainting a DDD code because the business sometimes changes and your knowledge about the model evolves at every day, with this knowledge your code must evolve too. Most of my experience has been with C# and I've built a lot of DDD projects with it. Now I'm working in a DDD project written in Ruby and one of the things I miss the most is the lack of a strong IDE. In Ruby or Python people are used to work using text editors, not IDE's. It's hard to me to see people writing things that some IDE or text editor should be writing for me (i.e. lack of autocomplete). It's hard to see people searching for the full path of a file in Vim just to open it and peek the details of a method or a class - in VS Code or Visual Studio, for example, a single hit on F12 should be enough to go to the definition class or method, without file ambiguity. And I don't even talked about debugging experience, it hurts me to see people writing binding.pry (for non-ruby developers it is somewhat like a "debugger" keyword in js) in their code just to debug it in the terminal instead of just setting a break point on the line. The list is bigger than this, but I think its enough to make the point about "tooling".

OOP expressiveness

In some dynamic languages like Python and Ruby you don't have all the OOP features like interfaces and abstract classes. That sometimes brings some difficulties in making the code expressive and clear.

Unit testing

You need to write a lot more unit tests to replace what the compiler could do for you.

Dynamic typing

You need to use duck typing if you want to do some kind of type checking. No help from the compiler you get.

Benefits of dynamic typed languages

Safe from Typification Hell

There are always tradeoffs when choosing between OOP dynamic vs static languages. One common problem in statically typed languages like C# and Java is that sometimes the type system can make the code a lot inexpressive and too verbose. Some developers tends to fall in the generics typification hell. But not all statically typed languages have this problem (F# is one of them - because of the strong type inference).

Testing

Not having static types also helps in some cases when, for example, you don't want to create an interface just to inject to your class and make it testable. In these cases the interface doesn't help in readability, in fact it hurts the readability because you need to create a dumb file (the interface) that doesn't represent anything other than the desire to test the code. In Ruby you could do that in several ways without needing to create an interface, one example would be this:

class DispatchOrderService
  def initialize(overrides = {})
    @repository = overrides.fetch(:repository) do
      ::Infra::OrderRepository.new
    end

    @mail_service = overrides.fetch(:mail_service) do
      ::Infra::MailService.new
    end
  end

  def dispatch(order)
    order.dispatched
    repository.save(order)
    mail_service.notify_order_dispatched(order)
  end
end

Yes, with this approach we broke the clean architecture because the class knows the concrete "infra" implementations. But it is a problem that can be solved with Dependency Injection (in Ruby these frameworks always broke clean architecture too or are too ugly for someone to want to use it, in our projects we did our own DI container by instantiating the dependencies manually on the project startup).

Conclusion

So concluding, I think it is possible to write good "enterprise" DDD applications in Ruby even if it's difficult than in the static languages. My current project is one example of that (60k lines of code and still maintanable).

Also the point mentioned by @GeorgeMaueris is important. You can face problems in implementing DDD in frameworks that imposes on you the way to organize your code. Here we choose to use Hanami instead of Rails because of this, but even Hanami is more "opinionated" that we would like. I really don't recommend anyone to find for "frameworks" to build DDD. The design/architecture changes from application to application and also it evolves. When you choose "DDD" frameworks sometimes you face yourself fighting against it (doing workarounds or monkey patches).

So, maybe you can ask me why we choose ruby at all. The main point to use Ruby here was that almost 100% of the team was composed by Ruby developers and we didn't want to duplicate the difficulties: learning DDD + a new programming language. More a strategic than purely technical decision. The company (a startup) probably wouldn't get so far without it.

EDIT 2018:

We give up from Ruby and Python for our projects that relies in DDD aspects. Now we are using Kotlin and are so much satisfied. The main benefetis were those listed here:

  • Best IDE support with IntelliJ. This makes us a lot faster in refactorings, giving erros at compile time and having less efforts writing tests for things that the compiler could do for us.
  • Good and popular frameworks for Dependency Injection, ORMs, functional programming
  • Another benefits of the language itself (null safety, data classes, etc)

EDIT 2019:

We wrote a series of posts about the move from Ruby to Kotlin. You can see it here.

Community
  • 1
  • 1
fabriciorissetto
  • 9,475
  • 5
  • 65
  • 73
  • 1
    im at a stage of deciding whether we continue developing a mid-size python project (~100 of source code files), team of 3 devs. unit tests, integrations tests, services, containerizations, A LOT of refactoring as the biz needs are not clear and will change on the fly. im a strong supporter of interface definitions and static typing and would rather use C#, at the same time all the team is familiar with python. so far its on the edge and i cannot decide which way to go. – ulkas Feb 07 '20 at 15:18
  • 1
    @ulkas I think as fast this type of change happens the less it hurts. When we decided to do this change we had 50 devs in the company, today we have more than 150. But... sometimes is better to wait for a "refactoring moment" to try to test another language, in a different service (call microservice if you want) and let the remaining old-and-working-code there. – fabriciorissetto Feb 18 '20 at 14:22
  • Thanks for sharing the experience. Main difference between static language and dynamic language is: Building software in static language is a process we called engineering. It's like constructing a house, we always start from an accurate blueprint (interfaces play here) then follow the exact design to build. Using dynamic language is like painting or composing a song that is kind of art. It always starts from vague concept then enrich the product progressively in a non-linear way. Build enterprise system is not a good use case for dynamic language because DDD requires strict design ahead. – ehe888 Aug 06 '21 at 04:03
  • @ulkas same here, can't decide, what did you do in the end.? – cikatomo Aug 23 '21 at 20:47
  • 2
    @cikatomo the project was stopped at some phase. my personal conclusion is, when you know what you are building, libraries/drivers in your language exists, go with static typing. when you are just doing PoCs, its ok with python. but i really dont recommend to go with python for a year longs projects for more 3+ devs. there is absolutely no added value, but there is certainly a lot of constraints. from what i can tell, people tend to pick python just because "they know it", every senior programmer i met, who already went through this journey in his life, is trying to avoid python – ulkas Aug 27 '21 at 13:54
11

This question bothers me for quite a while so I decide to collect all valuable data on this topic. Finally I end up with this github repo.

There are some code example too:

1) On Django

2) On Flask

3) On Ruby

And some more. But definitely not enough.

valignatev
  • 6,020
  • 8
  • 37
  • 61
  • I also have contributed to this [repo](https://github.com/mcapanema/ddd-rails-example). It's a sample application using Ruby on Rails with a layered architecture – fabriciorissetto Oct 11 '18 at 20:33
  • Nice! Feel free to open PR against ddd-dynamic repo and I'll gladly accept it! – valignatev Oct 12 '18 at 10:36
5

Python seems to be not too popular in enterprises till now compared to Java (but I believe the wind is in that direction. An example is Django, which was created by a newspaper company). Most programmers working with python are likely either into scientific computing or into web applications. Both of these fields relates to (computer) sciences, not domain-specific businesses, whereas DDD is most applicable within domain-specific businesses.

So I would argue that it is mostly a matter of legacy. C# and Java were targeted towards enterprise applications from the start.

Mansouri
  • 233
  • 4
  • 6
amit kumar
  • 20,438
  • 23
  • 90
  • 126
  • Building an Enterprise application is way more complex than what a dynamic language like python can handle. Most of the python developers only have the knowledge of building a website or CMS which is very simple application with standardized features. Python is a good language for quick prototype and personal productivity tool on data processing, image processing, scripting where DDD is not necessary and practical. For startups, there usually only 1 or 2 developers. Using Dynamic language make it faster to deliver without thinking about how to maintain the system before the company's death. – ehe888 Aug 06 '21 at 04:17
2

Most books on design/coding techniques such as TDD and design patterns are written in Java or C#, since that is currently the lowest common denominator language and have the widest user base, or at least the largest base of people who can read and understand the language. This is done largely for marketing reasons so that they appeals to the largest demographic.

That does not mean the the techniques are not applicable to or used in other languages. From what I know of DDD most of the principles are language independent and AFAICR the original DDD book had almost no code samples in it (but it is a couple of years since I read it, so I may be mistaken).

Dave Kirby
  • 25,806
  • 5
  • 67
  • 84
1

If Domain Driven Design is an effectively defined design pattern, why does it matter what language you're using? Advice for design philosophies and the like should be largely language agnostic. They're higher level than the language, so to speak.

asthasr
  • 9,125
  • 1
  • 29
  • 43
  • 5
    It is not a design pattern. It's a philosophy of how to architect a system and get requirements. The flagship book happened to contain some design patterns that everyone circles around like vultures, but those by no means define DDD. – George Mauer Nov 17 '10 at 19:30