When Microsoft Violated Liskov Substitution Principle in .NET

  Рет қаралды 37,725

Christopher Okhravi

Christopher Okhravi

Күн бұрын

It's easy to paint yourself into a corner where you end up violating the Liskov Substitution Principle. Let me show you an example of when Microsoft violated the principle in .NET.
MORE ABOUT LSP:
• Liskov Substitution Pr...
📚 geni.us/IBhtLnh (Clean Architecture)
MORE ABOUT ITERATOR PATTERN
• Iterator Pattern - Des...
CHAPTERS
00:00 Intro
00:36 Overview
02:07 The hierarchy
06:49 Read only
10:16 The violation
14:17 Confession
17:43 Conclusion

Пікірлер: 286
@francisadediran6311
@francisadediran6311 22 күн бұрын
"The whole point of static typing is to move error from runtime to compile time because that makes the code safer". Words of a genius
@drcl7429
@drcl7429 21 күн бұрын
Don't know if you're being sarcastic. That is precisely why static types exist and why I very strongly dislike dynamic and weakly typed languages. You have to think too much about what might go wrong. You already have to do that plenty.
@francisadediran6311
@francisadediran6311 20 күн бұрын
@@drcl7429 Not being sarcastic, just highlighting a very good point which i have forgotten
@ChristopherOkhravi
@ChristopherOkhravi 18 күн бұрын
I cannot take credit for this since it indeed is the very idea of static typing. Nevertheless I find it very important to keep reminding ourselves of that 😊😊 Thank you both for watching and sharing your thoughts 😊🙏
@_iPilot
@_iPilot 9 күн бұрын
This is why TypeScript has appeared. An attempt to relief all the pain related to JS brining a bit of static to the primal chaos.
@Bobbias
@Bobbias 4 күн бұрын
​​@@_iPilotexcept that there are too many ways to escape the static typing in typescript. It's an improvement over js, but still ultimately just papering over the gaping holes in js's type system.
@demarcorr
@demarcorr 25 күн бұрын
I *promise* you, cross my heart, swear to god, or smite me now, I violate every single SOLID principle, every single day, without fail.
@ChristopherOkhravi
@ChristopherOkhravi 6 күн бұрын
😊😊😊
@krozaine
@krozaine 25 күн бұрын
What a roller coaster ride in the explanation, especially with the entry of isReadOnly. Super awesome take on the issue and explanation!
@ChristopherOkhravi
@ChristopherOkhravi 25 күн бұрын
Glad you enjoyed it! 😊 Thanks for watching 🙏😊
@amerbashoeb2106
@amerbashoeb2106 24 күн бұрын
I just cant you thank you enough. There is no one on KZbin who teaches like you do. Thank you brother.
@BeatsByYari
@BeatsByYari 22 күн бұрын
another annoying thing is that Array implements IList in C#, but when you call IList.Add it throws an exception, so you can't be sure you can safely call .Add on a method accepting an IList
@Ockerlord
@Ockerlord 18 күн бұрын
So does IsReadOnly return true for Array then?
@modernkennnern
@modernkennnern 15 күн бұрын
It only throws if it's full, but yes. It's annoying
@renatogolia211
@renatogolia211 21 күн бұрын
To be fair to the .NET maintainers, certain types have been added later than others and they didn't want to break backwards compatibility. For example IReadOnlyCollection and IReadOnlyList were added in .NET Framework 4. Unfortunately, ROC has always been the weird guy.
@markw.schumann297
@markw.schumann297 9 күн бұрын
But... but... what even is the purpose of implementing IList in ReadOnlyCollection? By what logic does one _want_ a "read-only collection" to necessarily also be a "list"? I don't get what the objective of that decision even was.
@stardrake691
@stardrake691 18 күн бұрын
You’ve earned a new subscriber. Your explanation style is so unique and satisfying to watch, I think in mind the way you emphasize facts, and that’s why your teaching style is relatable to me.
@ChristopherOkhravi
@ChristopherOkhravi 18 күн бұрын
Thank you very much. I’m glad to have you 😊🙏 and I’m glad that the content is useful 😊
@user-ut8sk3lc3c
@user-ut8sk3lc3c 25 күн бұрын
What a clear explanation of complex Liskov violation on more complicated hierarchy of Collections... Hats off!.!! 😊😅
@ProjSHiNKiROU
@ProjSHiNKiROU 6 күн бұрын
For interface API design in general: If mutation is involved, interfaces should be split up into the reading part and the writing part (also works well with co/contra variance): IReadCollection and IWriteCollection. There are barely any use cases for write-only collections so that design isn't natural to most people.
@vincentjacquet2927
@vincentjacquet2927 23 күн бұрын
As you said, Add throwing when IsReadOnly returns true is not a violation of LSP per se. But there is a violation of LSP for ISet that also specialize ICollection. Given an empty list of integer, when you add 1 twice then the collection has a Count of 2. Given an empty set of integer, when you add 1 twice then the collection has a Count of 1. Had the signature of the method been bool Add(T item), like there is bool Remove(T item), it would have been a completly different story.
@ShubhamKhara
@ShubhamKhara 10 күн бұрын
Again, that's not really a violation but a very specific, arguably confusing behavior of the Add method. You can call Add and it works, that's all you need to not violate LSP. Also, some degree of freedom is required when we are implementing an interface, not every derived class has to Add to increase the Count. In your example, Set allowing Add on existing keys is actually the better approach, otherwise your Set class would be throwing exceptions which ICollection doesn't define. Actually, Set being an ICollection is a rather good example of trade offs in Software Engineering. By not being overly strict with how Add works, we are utilizing EVERYTHING else IEnumerable and ICollection interface gives us, and that's a lot more useful then defining a separate interface for Sets just to make it perfect.
@vincentjacquet2927
@vincentjacquet2927 8 күн бұрын
@@ShubhamKhara I beg to differ. This is the definition of Add. It "Adds an item to the ICollection", so you expect wether the operation to fail (i.e. throw) or to succeed, meaning the Count increases. For ISet, Microsoft had to create a "new" Add method, returning true when the value is added or false when the value was already present. With this signature, the test for the contract is if Add returns true then the Count increased by 1. Regardless of the return value, calling Contains with the value must return true. This is not a trade off, this is an oversight. The trade off is that we have to live with it.
@ShubhamKhara
@ShubhamKhara 8 күн бұрын
Yes, that makes sense. I had my tunnel vision set on ICollection's function's signature and not ISet's when I initially made the argument. Thank you for the revert, appreciate it.
@ZackJJones
@ZackJJones 25 күн бұрын
Thank you so much for this! I was thinking of mentioning it in your previous LSP video but I couldn't recall off hand where it was I ran into this. I even recall leaving a comment on an SO article that dug into it. I ran into this exact issue with either something from work or a side project I was working on explaining collection types. Anyways, thanks!
@janailtongoncalvesdesouza4160
@janailtongoncalvesdesouza4160 25 күн бұрын
I just love the way you explain things. Great vid!
@damianradinoiu4314
@damianradinoiu4314 7 күн бұрын
Your channel is wonderful. Giving out hidden gems as insights for whoever is patient enough to hear them. Do you plan on ever continuing the design pattern series ?
@dawid_dahl
@dawid_dahl 23 күн бұрын
I will send this video to anyone who’d like to learn the LSP! 👏🏻
@ChristopherOkhravi
@ChristopherOkhravi 23 күн бұрын
Thank you 😊🙏🙏
@user-ev9jg6ts6e
@user-ev9jg6ts6e 25 күн бұрын
Chris, you are fantastic. Highly appreciate!
@michaelwikstrom
@michaelwikstrom 10 күн бұрын
This is a brilliant explanation of LSP ! I love C#, it is a fantastic programming language, but as every programming language there are always some quirks due to historical reasons. I have heard about this "issue" before, but never explained at this level of clarity. Please keep posting these videos, preferably also using C#
@lawrencejob
@lawrencejob 17 күн бұрын
Thanks for articulating this. It’s been frustrating me for years!
@yamogebrewold8620
@yamogebrewold8620 23 күн бұрын
Thanks for a great explanation as always. My takeaways from this are: 1. A statically typed language gives you the capabilities to design robust software. But a type checker can not do all the work for you. It hands the responsibility to the engineer to make careful design decisions. 2. It was nice to see how applying LSP leverages the power of the compiler and makes the program safer at compile time. Lots of issues could be avoided if we crafted our systems with this in mind.
@jackwesleymoreira
@jackwesleymoreira 25 күн бұрын
Man what a great explanation of the LSP. To me one of the most complex principles to understand. Thanks for the video and for the great job explaining it in a way that makes sense.
@ChristopherOkhravi
@ChristopherOkhravi 25 күн бұрын
Thank you for the feedback. Much appreciated. And thank you for watching 😊🙏
@arjix8738
@arjix8738 25 күн бұрын
wikipedia says "objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program" which is pretty clear to me
@IroAppe
@IroAppe 6 күн бұрын
It seems so complex, because there are a lot of theoretical terms and definitions to make it logically strict. But the meaning is quite simple: Obey subtyping. A square must always be a rectangle. Not every rectangle must be a square. And then abide by that in all conditions and objects of the class or interface you are designing. It's also basically helpful advice to developers to adhere to the subtyping contract: - Don't forget method parameter types and return types - Don't forget exceptions - Don't forget input value checks and return value range restrictions - Don't forget value restrictions on instance variables and properties - both statically, and over time from state change to state change Of course it has applications in theoretical CS - in order to make any logical deductions, you need a strict definition. But in terms of application, it shows you what to look out for, when designing inheritance/subtype hierarchies of any kind.
@quentinparis1113
@quentinparis1113 22 күн бұрын
@chrisropher you should define the Okhravi principle: "never solve at runtime a problem that can be solved at compile time" 😊 By the way this is one of the best video I have seen on software development!!
@ChristopherOkhravi
@ChristopherOkhravi 22 күн бұрын
Thank you. Those are some very kind words 😊🙏 I’m happy that the content is useful. I can’t unfortunately claim ownership of the idea since it’s very old but I will definitely steal that wording and use it. Thank you 😊🙏 and thank you for watching 😊😊
@orterves
@orterves 3 күн бұрын
Without a doubt, the ReadOnlyCollection inheriting from IList is a mistake even though the interface is technically correct by throwing that exception. One thing I'd say in addition is, I hope no one thinks that technical validity means the correct approach to using IList.Add is to check if it's safe or otherwise handle the exception in the case of ReadOnlyCollection - we should still code with the assumption IList.Add is always safe to call, and if a client passes in a ReadOnly collection to that method, that's on them to fix.
@adambickford8720
@adambickford8720 25 күн бұрын
Before cheating off java's homework, make sure it's right
@ChristopherOkhravi
@ChristopherOkhravi 25 күн бұрын
Underrated comment 😆
@sergtachini
@sergtachini 25 күн бұрын
Java has the same, Collections.unmodifiableList or List.of
@redcrafterlppa303
@redcrafterlppa303 25 күн бұрын
The stupid thing is Java is even worse at this. They didn't even bother adding a readonly hierarchy. They simply added factory methods that create readonly collections of type (I)Collection
@streettrialsandstuff
@streettrialsandstuff 21 күн бұрын
Yeap, they went ass backwards, it should have been ICollection and IMutableCollection.
@andywong3095
@andywong3095 21 күн бұрын
J-things expert, talking about c# and Dot-things.
@greob
@greob 23 күн бұрын
Great lesson, thanks for sharing!
@Misteribel
@Misteribel 24 күн бұрын
Another good reason to move to F#. No LSP issues (unless you seek them out). All functional datatypes are immutable by default (which implies readonly). It's so much simpler and more concise, once you made the paradigm shift. Rarely try/catch or exceptions and aprogram you can reason about.
@_iPilot
@_iPilot 9 күн бұрын
Until you have infinite amount of resources and they are almost free. In the real world, all computations have their cost in CPU time, memory, and amount of data transferred over the network.
@Scorbutics
@Scorbutics 25 күн бұрын
Collections are always a pain in the ass to implement, but I totally agree with you ! Java has pretty much the same problems with their "Collections.unmodifiableXXX" (List / Set / Map / SortedMap...) and they are also using exceptions. Next step: how would you implement it ? I guess using composition, either containing another container inside the ReadOnlyCollection. Or by splitting collection interfaces into "permission accesses", ReadableCollection (get / iterate) WritableCollection (add / remove / clear) ? Or both ?
@guai9632
@guai9632 18 күн бұрын
MutableCollection : CollectionMutator, ReadOnlyCollection
@sagarbhosale3337
@sagarbhosale3337 24 күн бұрын
Very well explained. Requesting video on Liskov Substitution VS Interface segregation principle.
@ciberman
@ciberman 25 күн бұрын
Amazing video as always!
@fourZerglings
@fourZerglings Күн бұрын
There's a simpler example: arrays in .NET implement IList without support for Add or Remove
@logiclrd
@logiclrd 2 күн бұрын
I think they wanted the "common type" to be something that developers (their own developers) could use without thinking. What they should have done is IEnumerable
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 күн бұрын
I share your pain, though an even more fundamental violation is: IList list = new int[] { 1, 2, 3, 4, 5 }; list.Add(27);
@jvesoft
@jvesoft 9 күн бұрын
That's one of the things I like in Objective-C. It's main classes are immutable: NSArray, NSDictionary; they have subclasses NSMutableArray and NSMutableDictionary. Though I don't know .NET, I think that the hierarchy of interfaces would have been nicer if the base interface IEnumerable would have a subinterface ICollection (without add/remove), which would have two subinterfaces: IMutableCollection (with add/remove) and IList (with get(index), but without set(index, item)). The interface IMutableList would have two superinterfaces: IMutableCollection and IList and would add the set(index, item). No need for "unsupported operation" exception and you can immediately see what the class actually supports by its list of interfaces.
@AndreyRogozhnikov
@AndreyRogozhnikov 4 күн бұрын
Wow! Such a clear and logical delivery!
@ChristopherOkhravi
@ChristopherOkhravi 4 күн бұрын
🙏 Thank you for watching 😊🙏
@zalvazar9080
@zalvazar9080 25 күн бұрын
Great video keep them comming
@AerysBat
@AerysBat 3 күн бұрын
ReadOnlyCollection implements IList which is an interface for a mutable list. This means it throws errors if you call add() or remove(). MSFT added an “isReadOnly” flag in ICollection which allows add()/remove() to throw errors. It’s messy workaround for a LSP design error and weakens the compiler.
@perian1983
@perian1983 25 күн бұрын
Very nice video as always, it definitely was food for thoughts. Letting alone the discomfort I would have using an object of a class that could be (in theory) both mutable and immutable and letting alone the cognitive overhead in terms of counterintuitive naming, I could not help but thinking that this way of deciding the behavior of an object of a given class at runtime with an if-else block may be a little bit of an anti-pattern in a language that allows you to use types (at compile time) to address the issue. What I am wondering now is which reason(s) they may have had to make this particular design choice. Any idea?
@qj0n
@qj0n Күн бұрын
I think there are 2 ways of thinking about read-only collections. On the one hand, these are generalizations of read-write collections, which only support reading. On the other hand, these are collections which guarantee they won't change. I believe MS devs have change their mind about it couple of times and here's the result
@vinayshastri497
@vinayshastri497 24 күн бұрын
@ChristopherOkhravi Can you please explain what could be the possible reason of Implementing IList by ReadOnlyCollection base class. I didn't get this part
@modernkennnern
@modernkennnern 15 күн бұрын
IReadOnly** was added much later - after they had made that big blunder with `IsReadOnly`. It's also because (and this might change in .Net 10) IList and ICollection does not implement their IReadOnly counterpart.
@sebastianszafran5221
@sebastianszafran5221 5 күн бұрын
Brilliant video, I love it all! This plot twist near the end made me smile. However, I feel like it's missing the explanation of Microsoft's decision about this move. As you said, there are incredible intelligent people working on .NET design, so that I am sure there gotta be a good reason for that
@johnekare8376
@johnekare8376 25 күн бұрын
Great video!
@vchap01
@vchap01 25 күн бұрын
.NET concrete collections have always been a mess. Collection implements List, SortedList implements IDictionary and those are the ones just off the top of my head.
@dmmgualb
@dmmgualb 21 күн бұрын
Totally true. This happens not only with base classes/interfaces, but I have many times encountered such dead-ends in my own code when programming, and the solution is like that one (runtime and ugly). I really would like to see how to fix that.
@Nobody1707
@Nobody1707 9 күн бұрын
Coming from Swift, I'm surprised by that interface hierarchy. The equivalent protocol hierarchy in Swift is Sequence
@jvesoft
@jvesoft 9 күн бұрын
Wrote my comment (about Objective-C), before seeing yours about Swift. Yep, there are things, which Apple clearly did better. P.S.: Don't like Swift (I prefer Java nowadays), but it's definitely better than mixing C and Smalltalk. :)
@IroAppe
@IroAppe 6 күн бұрын
So basically, the LSP is kept, because ICollection was already broken, so ReadOnlyCollection can be just as broken. Oh the irony. It reminds me of the mathematical property that you can deduce anything from a false statement. Although this obviously has nothing in common with that, it makes sense - if you have chaos, you can deduce more chaos from it...
@leerothman2715
@leerothman2715 Күн бұрын
Love it. I am now changing my workshop session on LSP to include this. Basically the way that C# has got round it is by a hack and use and a boolean to the ICollection contract. If you need to check the result of this first then does this go against ‘tell don’t ask’? Not a SOLID principle obviously but a commonly used rule.
@pix3ldust360
@pix3ldust360 20 күн бұрын
Your explanation was engaging and thought-provoking. Made me wonder about the relationship between number of layers of abstraction and probability of violating the Liskov Substitution Principle. For "real-world" solutions of sufficiently complex problems, is this curve likely exponential, linear, or ...?
@jorgeromero4680
@jorgeromero4680 25 күн бұрын
i really like this video about these horrible decisions and the wise consideration to use types.
@DaoBeat
@DaoBeat 25 күн бұрын
Hello my friend and thank you for your new video. It’s very interesting. Would love to hear something interesting about IQueryable.
@user-dc9zo7ek5j
@user-dc9zo7ek5j 5 күн бұрын
Christopher, awesome presentation, with the gestures you remind me a little bit of Morshu :) I want to ask a question because even chat bots confure this one. Is there a circular reference between string and object's ToString? String inherits from object, but object cannot be compiled without string. What do you think?
@user-dc9zo7ek5j
@user-dc9zo7ek5j 5 күн бұрын
Regarding the issue in the video, I think that F# solved this issue and applied a much more pragmatic definition of interfaces. There wasn't a need for interfaces, rather the type must have a method that matches the one that is called in order to compile, without the need of interfaces, making implementations very specific and at the same time, very generic, but I might be wrong, haven't checked.
@samyakshah806
@samyakshah806 24 күн бұрын
Hey, just wanted to let you know that the link on recommended books, placed on your website, is not working
@Max-zn7md
@Max-zn7md 8 күн бұрын
Hi chris nice video, I have a question that might be a bit off with the topic of this video, the thing is that i started to read a book called "Dependency Injection: Principles, Practices and Patterns" in wich the author explains the different techniques we can use in order to inject properties into our clases (Constructor injection, Method injection and Property injection), he also explained some anti patterns related to dependency injection (Control freak, service locator, ambient context and constrained constructor), all off these stuff its explained on the chapter 4, 5 and 6 and i think you could create a great video explaining thoso topics
@davidmartensson273
@davidmartensson273 8 күн бұрын
I agree, they should have added ImmutableCollection between IEnumerable and ICollection and then add an IReadOnlyList, that would have created a clean hierarchy.
@Sproeikoei
@Sproeikoei 24 күн бұрын
Thank you I really enjoyed the video, especially the inreractive parts! I guess how I would try to solve it: we would want to introduce an iindexer interface and let IList implement that, then also let the ReadyOnlyCollection implement that interface to "solve" this? (With the information that is used in the video)
@sarvarthmonga5764
@sarvarthmonga5764 25 күн бұрын
Very nice
@ehsnils
@ehsnils 11 күн бұрын
At first glance I'd like to turn the IEnumerable, ICollection and IList upside down from what you did draw. Mostly because I see the IEnumerable as the root and that it then grows upward. Arrow directions can also be confusing - is the arrows the way the functionality evolves or the way the drill-down occurs. It comes down to the way you think, and it's easy to get locked into one pattern (information flow for example) and if someone else uses a different pattern then it takes time to adapt. At least you did set a baseline in the first 2 minutes. As for the inheritance issue I'd like to change so that the indexing that IList provides should be provided by IEnumerable and the IReadOnlyCollection should be between IEnumerable and ICollection.
@loam
@loam 21 күн бұрын
I saw one example of LSP violation myself in .Net source code (when of the methods of base class was not appropriate for the subclass and it was just throwing exception)
@drcl7429
@drcl7429 21 күн бұрын
I think you were right first time. I don't know why they made its return value to be void, could have just returned 0 or maybe -1 on add() then. At least Java uses a boolean.
@o_corisco
@o_corisco 16 күн бұрын
this could be an interesting case for type-families (ts ternary type, or rust traits). In fact, typescript has a utility type that does exactly that. i don't think it will be very long until c# gets functions at the type level.
@michaelutech4786
@michaelutech4786 8 күн бұрын
I remember having seen this in C# years ago, just not with ReadOnlyCollection, so this is not the only violation. This is actually sad, considering that in many regards, C# and .NET is designed very well (and I say that as someone who is not particular happy with the MS ecosystem). Even sadder, I believe I know exactly how this happened. It's a result of two attitudes. One is to not correct mistakes because corrections break existing code. The other is to get things done, also called the 80/20 principle or management making design decisions. The fact that this happened at such a fundamental level as the base data structure abstractions however is shameful.
13 күн бұрын
That’s crazy. I would also argue that we have an Interface segregation problem here. And yes, maybe technically it’s not a LSP violation, but it’s still bad code.
@reecedeyoung6595
@reecedeyoung6595 23 күн бұрын
When I hear LSP I think language server protocol
@NoAkcess
@NoAkcess 25 күн бұрын
So could the developer toggle ICollection.readonly to true or false dynamically so at times the collection is readonly? That would explain the exception at runtime.
@louisfrancisco2171
@louisfrancisco2171 9 күн бұрын
IsReadOnly is usually a read-only property. ReadOnlyCollection implementation always returns true. List implementation always returns false. A Collection, when instantiated with a list parameter, just returns the IsReadOnly value of that list. But you could implement it as read-write in your own class if you need to.
@sxs512
@sxs512 4 күн бұрын
Ideally you'd want to separate all those functionalities into separate interfaces and have collections implement the ones that make sense. This entire hierarchy is very much a complete mess caused by maintaining backwards compatibility and some language limitations.
@Akronymus_
@Akronymus_ 5 күн бұрын
Inheritance/interfaces are fantastic, the only big problem, for me, is the diamond problem. For example, what if you want to support passing IList to a function that expects a IReadOnlyCollection, because you want to ensure you don't to change the collection in the function that uses it. Under the current hirachy you simply can't do that, unless you go to just an IEnumerable. How would you solve that?
@TheFrewah
@TheFrewah 2 күн бұрын
Usually people end up with spaghetti code when they get things wrong. I do remember a former colleague that always wanted to use everything he knew about object oriented programming and his code was horrible. You could easily remove 50%
@verfran
@verfran 25 күн бұрын
Excellent. Sometimes I feel so stupid making this kind of mistakes. Now I realize, I shouldn't be too hard on me :)
@ChristopherOkhravi
@ChristopherOkhravi 25 күн бұрын
I feel the same way 😊
@tibrec8
@tibrec8 25 күн бұрын
This also founded on dart language by google . Theres List Type called : UnmodifiedList ... and if u call Add item on it it throws an exception same as .Net and i know it violating LSV Principal....but i also think dart is a new language why they make it like this
@istvanstefan9315
@istvanstefan9315 24 күн бұрын
i don't really get the point of creating a read-only version of any type. If you want to make an instance of a type read-only you could use the const keyword and mark the methods which don't change the state of the object also with const. Or is the const keyword not available in C# (I'm coming from C++ world)?
@CtrlGame
@CtrlGame 14 күн бұрын
c++ has a great way of dealing with readonly. they thought really well the const keyword for methods
@louisfrancisco2171
@louisfrancisco2171 9 күн бұрын
In C#, you can mark the methods of a struct with the readonly keyword.
@MaxwellHay
@MaxwellHay 22 күн бұрын
When it comes to principles like SOLID, you need to fully understand it and be pragmatic rather than just following it blindly
@ChristopherOkhravi
@ChristopherOkhravi 22 күн бұрын
Agreed. Except for LSP which I consider to be more like a hard rule. Violating LSP leads to enormously confusing code. The other principles however I completely agree with you are up for debate and you can make pragmatic decisions as to whether or not to care about them in a specific case. But LSP is a different story. It is very clearly defined and it is very clear what is a violation and violating it leads to very clear problems 😊 Thank you for watching and for sharing your thoughts 😊🙏
@JohnDoe4321
@JohnDoe4321 7 күн бұрын
IEnumerable, ICollection, and IList were added to .NET in version 2.0. IReadOnlyCollection was added to .NET in version 4.5. Is the inheritance hierarchy "wrong"? Yes. But doing it "right" would have broken the world. If you value ideological purity more highly than backwards compatibility, perhaps C# and .NET aren't for you. 😁
@phizc
@phizc 6 күн бұрын
Inserting an interface into an inheritance chain wouldn't break anything. I.e they could have made ICollection inherit from IReadOnlyCollection. The big problem though, is that the class ReadOnlyCollection was introduced in .NET Framework 2.0, long before the interface IReadOnlyCollection in 4.5. So at the time they only really had IEnumerable that it could sensibly implement. They decided to implement ICollection and IList too, and those can't be removed without breaking backwards compatibility. (I didn't bother with all the s. Too annoying to type on a phone.)
@numeritos1799
@numeritos1799 25 күн бұрын
I mean why/when would you require a read-only collection anyway? If you're going to only read through a collection, you don't really care if it's read-only or not, and limiting it to only read-only collections through the type-system would do more harm than good. I think it's just a hard problem to solve, if not impossible, would love to hear a better solution though if anyone has it. Just as a side note, Java solves this in a similar way: UnmodifiableCollection implements Collection.
@silberwolfSR71
@silberwolfSR71 25 күн бұрын
One common use case would be to communicate and enforce a contract: my library function takes a collection, does some computation using its elements and returns some result to you. In the contract of this function, I promise not to alter any collection you pass me. This is important, because you can now pass me a collection without having to worry (and verify) whether it has changed after I'm done with it. If we have interfaces defining read-only collections, I can both document and enforce this contract succinctly and reliably in one place. Conversely, as it currently stands in C# and Java, you basically have to both read the (prose) documentation of my function to find out this part of the contract and trust my library to actually adhere to it. This makes the contract at the same time weaker _and_ harder to discover, neither of which is a desirable quality in this context. In short, I may not care much if a collection you pass me also supports modification if I only need to read it, but you should care whether I can modify a collection you pass me or not. I'm curious what you imagine the harm done to be of defining a ReadableCollection interface that is extended by a MutableCollection interface which simply adds the add and remove methods. If my function must somehow mutate your collection in order to fulfill its goals, it can define a parameter of type MutableCollection, which also implicitly communicates to you that I intend to alter your collection. If you already have a MutableCollection but my function only requires a ReadableCollection (and defines its parameter accordingly), you can simply pass me your mutable collection with the reassurance that I still can't modify it, without any additional effort on your part. As to the hard problem to solve, maybe you can elaborate on what exactly that problem is?
@adambickford8720
@adambickford8720 25 күн бұрын
Thats how the entirety of functional programming works and increasingly considered the norm. Don't mutate your arguments.
@numeritos1799
@numeritos1799 25 күн бұрын
@@silberwolfSR71 Don't you see the problem with what you're saying? "...defining a ReadableCollection interface that is extended by a MutableCollection interface". If so, the LSP is "violated" once again, and in a WAY worse way, IMO.
@numeritos1799
@numeritos1799 25 күн бұрын
@@adambickford8720 Okay but C# isn't a functional language? The LSP doesn't refer to functional programming either. I don't really see your point. EDIT: just to clarify, I see the purpose of immutable collections, of course. But in a hierarchy structure like it's defined in C# (IEnumerable, ICollection, IList, etc), I don't see the point of having an LSP-compliant read-only collection.
@adambickford8720
@adambickford8720 25 күн бұрын
@@numeritos1799 What do you think linq is? Get educated before coping an attitude, mmkay?
@drapala97
@drapala97 24 күн бұрын
Any plans to continue the object oriented lectures?
@ChristopherOkhravi
@ChristopherOkhravi 24 күн бұрын
My sincere apologies but the answer is unfortunately no. I recorded everything but it didn't turn out as good as I wanted it to. I've got a few things in the works however so it is possible that I might redo it from scratch but that's far out into the future. Again, my sincere apologies.
@drapala97
@drapala97 24 күн бұрын
@@ChristopherOkhravi no need to apologize, your content is awesome
@sepiaflux123
@sepiaflux123 4 күн бұрын
Great Video
@franciscosilva7302
@franciscosilva7302 14 күн бұрын
Why isReadOnly means no LSP violation?
@McZsh
@McZsh 15 күн бұрын
Also, arrays are collections that cannot be added to, but can be changed by index. Readonly collections cannot be changed by index. The implementations of ICollection and IList are basically flipped. It would have been better concentrate the mutability methods and properties in the most top level type. IReadOnlyCollections stems from this historic mistake, with ReadOnlyCollection being a simple wrapper around every other collection type to make it work. You wouldn't use it in any declaration.
@user-yd9xy3rb4x
@user-yd9xy3rb4x 23 күн бұрын
Cool as usual. I wonder how one interface can inherit from another 😊
@ChristopherOkhravi
@ChristopherOkhravi 23 күн бұрын
The signatures are inherited. The subtype interface contains all signatures of the parent. Thanks for watching 😊😊
@xyyx1001
@xyyx1001 3 күн бұрын
I support your cause. Smelly code, oily tuna design, bad Microsoft, bad! Okay, over the top but eye opening video on how to paint yourself into a corner and everyone is susceptible.
@friedrichdergroe9664
@friedrichdergroe9664 19 күн бұрын
This could be resolved easily with Haskell's static typing. Pretty trivially, actually, so that violators fail at compile time. an isReadOnly() function is a bad idea. The read-only quality should be embodied by the typing directly.
@tesilab994
@tesilab994 10 күн бұрын
C++ has the “feature” of private inheritance where you can steal the implementation without promising to be a subtype. Also a lot of this mess is from mutability overload.
@one.pouria786
@one.pouria786 18 күн бұрын
Maybe if we had conditional types like typescript in C#, programmers would be able to deal with it more effectively
@Miszkaaaaa
@Miszkaaaaa 20 күн бұрын
I was repeating "Yes!" almost every 4 seconds after 15:37
@ahmedalirefaey3219
@ahmedalirefaey3219 22 күн бұрын
hold second i think Microsoft make this for business perspective to check in runtime if it read only collections or not (but in compile time will pass it ) it suppose to handle this runtime in derived business class
@Junior7setti
@Junior7setti 25 күн бұрын
Dont know the whole interface and methods there. But I think they could have ICollection be a sub type of IReadOnlyCollection instead, then from there fork between IList and IReadOnlyList which then is implemented by ReadOnlyCollection. I believe they had a very good reason to do the way they did instead. It is hard to please everyone when developing a large framework or library.
@ChristopherOkhravi
@ChristopherOkhravi 25 күн бұрын
I suppose we should please Barbara Liskov first? 😁 Thank you for sharing your thoughts. I would not be comfortable with having a mutable collection be a subtype of an immutable collection. Seems like this would lead to violating LSP by violating invariants in the supertype. But I may be mistaken. Thank you very much for watching and sharing your thoughts 😊🙏
@arjix8738
@arjix8738 25 күн бұрын
@@ChristopherOkhravi nah, Barbara Liskov can go cry about it jokes aside, it probably was made this way to not break existing code
@Scorbutics
@Scorbutics 25 күн бұрын
@@ChristopherOkhravi Sure, but you could rename it as "ReadableCollection", which would make more sense in this hierarchy location, because you remove the "only" part of the ReadonlyCollection
@barneylaurance1865
@barneylaurance1865 8 күн бұрын
@@Scorbutics You'd probably to rename it to just Collection then. Presumably all collections are readable.
@__christopher__
@__christopher__ 5 күн бұрын
@@ChristopherOkhravi Readonly is not the same as immutable. Immutable means it won't ever change. Readonly on the other hand only means *you* cannot change it. However I nevertheless agree that it would not make sense to derive a writable collection from a non-writable one. But that's just a matter of naming: Name the thing you cannot write to "Collection" and the thing you can write to as "WritableCollection": Then clearly a writable collection is a collection.
@fulconandroadcone9488
@fulconandroadcone9488 25 күн бұрын
Did I miss something or someone actually made read only collection and then implemented add / remove methods with exceptions?
@vytah
@vytah 22 күн бұрын
Yes. System.Collections.ObjectModel.ReadOnlyCollection implements IList and all modifying methods throw unconditionally.
@fulconandroadcone9488
@fulconandroadcone9488 22 күн бұрын
@@vytah I think I'll stick with JavaScript for the time being.
@thewelder3538
@thewelder3538 14 күн бұрын
I love your delivery and presentation; quite refreshing. Let's face it though, LSP is just a load of bollocks anyway. If you're writing code and giving a crap about stuff like LSP, the chances are you're writing crappy code.
@ChristopherOkhravi
@ChristopherOkhravi 14 күн бұрын
Thank you for your perspective. Would you care to elaborate what you mean? If you are violating LSP then you are essentially writing type unsafe code. So LSP is not really on the same “level” as other principles like say SRP or OCP. Imho LSP is much more important and much more clear. I would also hold that LSP is “discovered” more than “invented”. In the same way that mathematics is “discovered” rather than “invented”. But regarding most other principles I see what you mean 😊😊 Thank you again for sharing your perspective 😊🙏
@thewelder3538
@thewelder3538 14 күн бұрын
@@ChristopherOkhravi I'm more of a C++ coder than a C# one, I just happen to have ended up coding a large amount of C# recently. Now, I think of a list as being node based, as opposed to a vector which is more like an array. It's kind of weird that something contiguous in C# like List is called a list. Now I don't care what interfaces it implements, I just need to store a contiguous set of bytes. Nor do I care about interfaces at all when the concrete class will do just fine. Sure, if you have nothing better to think about than all these collections have the same contract with an interface, fine, but it's still just about storing some data. I've lost so much time removing things declared as IEnumerable because I needed something way more useful than an interface just defining that my collection is enumerable. Most of the things I use especially, have very defined sizes and having these abstract interfaces, just makes things more of a pain in the arse than it should be. Moreover, being able to substitute one collection for another just because they inherit from the same interface is all just a bit of nonsense in real-world terms. Also std::map which the C# equivalent is Dictionary is sorted by default. The C# equivalent of map must be weirdly implemented not to exhibit this behaviour. We have unordered_map for a key/value container where order is not maintained.
@__christopher__
@__christopher__ 5 күн бұрын
If you violate LSP, you are effectively lying about your class. You are promising "this can be used as a Whatever" when it actually can't be used that way.
@Aaron-wg6ft
@Aaron-wg6ft 6 күн бұрын
If ICollection has a read_only property, then what's the point of IReadOnlyCollection? And yes, it's a violation.
@mehtazsazid9398
@mehtazsazid9398 23 күн бұрын
Doesn't it break ISP? Read only collection is forced to implement add and write method.
@davidwilliss5555
@davidwilliss5555 20 күн бұрын
It would have made more sense for ICollection to inherit IReadOnlyCollection
@Wildstraw
@Wildstraw 24 күн бұрын
what is his name?
@chudchadanstud
@chudchadanstud 24 күн бұрын
Logically ICollection should only contain immutable methods and properties. If you need mutation just upcast or get the correct interface like IList. Even better there should be an interface call IWriteableCollection or IMutableCollection I am also not a big fan of the Add and Remove methods in ICollection. You can't just add or remove in a static array. An Array is a collection.
@carlseghers7822
@carlseghers7822 8 күн бұрын
Technically, if iEnumerable represents the idea of infinite number of elements, and iCollection the idea of a finite number of elements, then inheriting iCollection from iEnumerable is a violation of LSP, because code that deal with iEnumerable cannot make the infinite assumption if you pass in an iCollectible.. It's like why a iSquare cannot inherit from a iRectangle, because code dealing with rectangles would assume that height and width are independent, which is not the case with a square.. I think the inheritance of iCollectable from iEnumerator is ok because in reality nothing is infinite, but you cannot describe as property that iEnumerator assumes infinity and iCollection not..
@ChristopherOkhravi
@ChristopherOkhravi 8 күн бұрын
Good and interesting point. Thanks for sharing 😊🙏. IEnumerables are *possibly* infinite. So in my interpretation we should read this as that finality (and size) is undefined. So imho this doesn’t lead to a violation when we say that a type where finality IS defined implements the one where it isn’t. Thanks again for your detailed comment 😊
@AbhinavGunwant
@AbhinavGunwant 18 күн бұрын
Who else is dumb like me and saw the thumbnail and thought "Languagae Server Protocol"?
@CtrlGame
@CtrlGame 14 күн бұрын
If it where me, I would create a method to convert the ICollection to IReadOnlyCollection and vice versa without copying the items (In a c++ way with move semantics). The fact that the class is a child of the IList feels wrong to me. Just why? But, I still like c#. The interfaces are there. You can make your own IReadOnlyCollection and ICollection if you want to fix this by yourself.
@ErezAvidan1
@ErezAvidan1 25 күн бұрын
the whole problem starts and ends with inheritance, this time is not inheritance between classes but between interfaces
@ChristopherOkhravi
@ChristopherOkhravi 25 күн бұрын
The issue is not inheritance but subtyping. LSP applies as soon as there is subtyping. Thank you for watching and for sharing your thoughts 😊🙏
@alanmaia8880
@alanmaia8880 21 күн бұрын
Playing devils advocate here, they kinda have the type restriction via the interfaces which would be the most wise way of using these types. Without looking at the code is hard to say, but maybe ReadOnlyCollection just reuses the code of some other class but setting ReadOnly to true. Its still a weird design decision but I wouldn’t say it’s too bad given we have the interfaces properly done.
@johndenver8907
@johndenver8907 21 күн бұрын
If someone uses C# daily and has used an IReadOnlyCollection quite often I bet they've also never had a single ReadOnlyCollection. As in maybe you could new it up, but then adding values to it would just mean it's not actually read only so it's not meant to be used that way. it's an implementation detail that would be part of the guts of the framework. Yeah, it's weird, but I'm sure it's not the only thing you can find, and it truthfully has no negative affect at all.
@louisfrancisco2171
@louisfrancisco2171 9 күн бұрын
@@johndenver8907 You're supposed to pass the values to its constructor. You don't add anything to it afterwards.
@johndenver8907
@johndenver8907 9 күн бұрын
@@louisfrancisco2171 Oh man. Yes I assume that would be the case. But you would of course be using an interface and if you happen to have a ReadOnlyCollection I hope that you're at least passing it out as an IEnumerable or IReadOnlyCollection. I'm saying that if you program C# professionally you don't use a ReadOnlyCollection except for this strange edge case where you want to make sure that the interfaces you used to express your intent to make it readonly can't be cast to something mutable. I'm aware of the edge case, but we all still just use the interfaces and the ReadOnlyCollection is there for the edge case I spoke about. But thanks for telling me how to new it up. My advice is don't do that.
@uuuuuu352
@uuuuuu352 20 күн бұрын
I expected you show at the end of the video how your solution would be.
@robsosno
@robsosno 24 күн бұрын
In my opinion when IReadOnlyCollection was created then IsReadOnly property should be forbidden by the compiler. It could be assigned in runtime to allow old binaries still work but compiler would throw an error forcing change to IReadOnlyCollection. Then IReadOnlyList should be created inheriting from IReadOnlyCollection. And then ReadOnlyCollection class should inherit from IReadOnlyList, not from IList.
@dariofagotto4047
@dariofagotto4047 12 күн бұрын
Imagine how funny (and probably it is) if ICollection read only getter COULD change during the lifetime of the object, it wouldn't even be safe to check it before add...
@louisfrancisco2171
@louisfrancisco2171 9 күн бұрын
If you make a class implementing ICollection with a read-write IsReadOnly property, then it could change between checking its value and calling Add. You can always implement interfaces whichever way you want, even if it's silly.
@YuryPastushenko
@YuryPastushenko 21 күн бұрын
I just don't get why IList should be inherited from ICollection. Indexing shouldn't have anything with addin and removing anything. For example Array has indexing but doesn't support adding and removing. IMHO IList and ICollection should both inherit from IEnumerable.
@rastersoft
@rastersoft 8 күн бұрын
Mmm... so... the right way of doing that would have been IEnumerable
@barneylaurance1865
@barneylaurance1865 8 күн бұрын
No, list can't inherit from ReadOnlyList - that would violate the history constraint. A readOnlyCollection presumably should return the same data every time it's read. A subtype that doesn't behave like that violates the LSP. You could have MutableCollection inheriting from Collection, and ReadOnlyCollection as a sibling of MutableCollection also inheriting from Collection.
@FritsvanDoorn
@FritsvanDoorn 24 күн бұрын
This is clear. This I understand. Thank you. But I have a question. I followed your old videos. Now I wonder if IList violates the law of Demeter. Or am I wrong?
Covariance and Contravariance
13:31
Christopher Okhravi
Рет қаралды 11 М.
Liskov Substitution Principle
20:16
Christopher Okhravi
Рет қаралды 10 М.
Купили айфон для собачки #shorts #iribaby
00:31
New Gadgets! Bycycle 4.0 🚲 #shorts
00:14
BongBee Family
Рет қаралды 18 МЛН
🌊Насколько Глубокий Океан ? #shorts
00:42
King jr
Рет қаралды 1,7 МЛН
ГДЕ ЖЕ ЭЛИ???🐾🐾🐾
00:35
Chapitosiki
Рет қаралды 12 МЛН
This Is Why Managers Don't Trust Programmers...
28:04
Thriving Technologist
Рет қаралды 183 М.
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
Liskov: The Liskov Substitution Principle
4:23
Turing Awardee Clips
Рет қаралды 18 М.
The Magic of RISC-V Vector Processing
16:56
LaurieWired
Рет қаралды 190 М.
Only Use Inheritance If You Want Both of These
9:10
Christopher Okhravi
Рет қаралды 13 М.
Depend on Abstractions not Concretions (Framework)
11:56
Christopher Okhravi
Рет қаралды 15 М.
Factory Method Pattern - Design Patterns (ep 4)
27:21
Christopher Okhravi
Рет қаралды 534 М.
The Square-Rectangle Problem
9:59
Christopher Okhravi
Рет қаралды 9 М.
Object Oriented Programming is Good | Prime Reacts
31:30
ThePrimeTime
Рет қаралды 285 М.
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 186 М.
Купили айфон для собачки #shorts #iribaby
00:31