jvelilla

jvelilla

Advanced Functional Programming with Elixir: B4.0

Chapter One makes a reference to DDD to use factories

In Domain-Driven Design, a factory creates domain objects in a consistent
way, keeping their internal structure loosely coupled from the rest of the
system. The FunPark.Ride module follows this pattern by exposing a make/2 factory
function for creating new rides.

I understand the idea that each module uses factories to maintain a consistent creation, but the current approach could create domain object that have invalid values.

Some factories in FunPark (chapter 1) enforce required arguments, but optional arguments are just passed without validation. We can accidentally pass an invalid value and the struct will happily accept it.

For example:
dark_mansion2 = FunPark.Ride.make(
“Dark Mansion”,
min_age: -10,
tags: [:dark]
)

Maybe the factory should only accept required fields.

  • Optional fields are updated with dedicated function (check preconditions)
    I guess this kind of validations will be treated in other sections of the book.

def make(name, opts \\ []) when is_binary(name) do
%__MODULE__{
id: :erlang.unique_integer([:positive]),
name: name
}
end

Example check precondition

def set_min_age(%__MODULE__{} = ride, age) when is_integer(age) and age >= 0 do
{:ok, %{ride | min_age: age }}
end

def set_min_age(_, _) do
{ :error, invalid_age }

end

This is very close to design by contract style in Eiffel.

Where Next?

Popular Pragmatic Bookshelf topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
GilWright
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New

Sub Categories: