Primitive Obsession | Code Walks 055

  Рет қаралды 6,858

Christopher Okhravi

Christopher Okhravi

7 жыл бұрын

Why is Primitive Obsession bad? Because the domains of allowed states for any given domain concept we want to represent using a primitive is often different (smaller) from (than) the primitive itself.
Liskov Substition Principle
► • Liskov Substitution Pr...
Replace Conditional With Polymorphism & Primitive Obsession
► • Replace Conditional Wi...

Пікірлер: 12
@ekstrajohn
@ekstrajohn 6 жыл бұрын
I'm watching all your code walks, they're great insights, refreshers, as well as new ways of looking at older concepts. Well spoken and makes it easy for my brain to absorb. Thanks for sharing!
@ChristopherOkhravi
@ChristopherOkhravi 6 жыл бұрын
Not a problem. I’m glad to hear that the snippets are useful :) :) Thanks for watching :)
@ievgeniiiablonsky1161
@ievgeniiiablonsky1161 7 жыл бұрын
Great video! In some languages, like C, C++, Paskal and Ada, we can use type aliases for this situations. For example: Type employeeName = array[ 1..30 ] of char; Or even better : Const NAME_LENGTH = 30; Type employeeName = array[ 1..NAME_LENGTH] of char;
@ChristopherOkhravi
@ChristopherOkhravi 7 жыл бұрын
Ah cool. Thanks for the comment. That's great info. I haven't done any C or C++ but that does indeed look like a neat way to introduce types without too much overhead in cases we're you're not sure whether they will be needed or not.
@urdans
@urdans 6 жыл бұрын
In Delphi (Object Pascal) Const MAXAGE = 110; Type TAgeOfPeople = 0..MAXAGE; You can configure the compiler to throw an exception when you are violating the range or just allow the overflow.
@thisisfunc4529
@thisisfunc4529 6 жыл бұрын
Awesome explanation
@kvishnuteja334
@kvishnuteja334 2 жыл бұрын
I am in obsession of this channel :D
@wobblyrampack9655
@wobblyrampack9655 7 жыл бұрын
Really interesting, thank you! For a person's age, for example, do you think using an integer property with get/set methods (C# or whatever) that limit the allowed range of values is the right way, or does age need a whole class? And if using get/set then what to do if given an invalid age to set - throw an exception? I realise I have been quite primitive obsessed in my code! ;)
@expurple
@expurple 3 жыл бұрын
Came to the comments to ask the same thing. Property seems like a perfect solution for this age scenario.
@DerMaiskolben
@DerMaiskolben 3 жыл бұрын
I asked myself the same thing. Normally I did nearly never check any of these constraints. Lets assume that you get an age of 200 somehow in your code and your code might be able to handle it, since it is just displaying userdata. There would be no harm done. But combine that age with a customer who gets discount based on age it may cause some problems depending on the implementation. For that case I assume its always safe to throw an IllegalArgumentException. In that case you will NEVER allow errors in the boundaries of your programm and you will be aware of them when they happen. To me it seems like nullchecking. What will your class do if there is a null value on a dependency? Well it will probably not work and throw an error. Some code caused this error and you need to fix it. So why treat this case distinctly? Since this comment is 3 years ago, maybe you two got another answer by now. Im looking forward to your answers :)
@wobblyrampack9655
@wobblyrampack9655 3 жыл бұрын
@@DerMaiskolben All very interesting and good to read! Thanks for posting. In my case I don't have an answer yet.
@Ozzah
@Ozzah 2 жыл бұрын
Another implication of primitive obsession is comparison of incomparable types. For instance if you have a Person class with an int age and an int weight, then you could go compare person1.age with person2.height. But that doesn't make any sense. In your age and height domain classes, you can specify exactly comparison works (or doesn't) with different types.
Bash Programming & The UNIX Chainsaw | Code Walks 054
8:04
Christopher Okhravi
Рет қаралды 2,3 М.
Replace Conditional With Polymorphism & Primitive Obsession | Code Walks 045
10:56
Stupid Barry Find Mellstroy in Escape From Prison Challenge
00:29
Garri Creative
Рет қаралды 17 МЛН
She ruined my dominos! 😭 Cool train tool helps me #gadget
00:40
Go Gizmo!
Рет қаралды 53 МЛН
Неприятная Встреча На Мосту - Полярная звезда #shorts
00:59
Полярная звезда - Kuzey Yıldızı
Рет қаралды 2,8 МЛН
Only Use Inheritance If You Want Both of These
9:10
Christopher Okhravi
Рет қаралды 13 М.
First Job As A Programmer? How To
11:02
Christopher Okhravi
Рет қаралды 14 М.
Covariance and Contravariance
13:31
Christopher Okhravi
Рет қаралды 11 М.
Always Use Interfaces
8:08
Christopher Okhravi
Рет қаралды 44 М.
Code like prose | Code Walks 013
6:10
Christopher Okhravi
Рет қаралды 4,1 М.
The Square-Rectangle Problem
9:59
Christopher Okhravi
Рет қаралды 9 М.
Downcasting Is A Code Smell | Code Walks 043
6:58
Christopher Okhravi
Рет қаралды 6 М.
Strategy Pattern - Design Patterns (ep 1)
35:11
Christopher Okhravi
Рет қаралды 1,4 МЛН
The Only Time You Should Use Polymorphism
13:55
Christopher Okhravi
Рет қаралды 87 М.