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

yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
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
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1147 28379 760
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
sir.laksmana_wenk
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc. However, I don’t...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
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: