Open
Conversation
dmitryvhf
suggested changes
Jan 15, 2022
| rectangle.SetWidth(4); | ||
| rectangle.SetHeight(5); | ||
| var area = rectangle.GetArea(); // BAD: Will return 25 for Square. Should be 20. | ||
| var area = rectangle.GetArea(); // BAD: Will return 20 for Square. Should be 25. |
There was a problem hiding this comment.
Your are wrong. BAD example - correct.
- Square.SetHeight(5) set Width and Height to 5 (overwrite Width from 4).
- Rectangle,GetArea calculare Width * Height = 25
Good example fix i - different GetArea() with one SetLength
Author
There was a problem hiding this comment.
I just ran it and it gives 20, 20, 20 as result. The right approach would be to overrite the Rectangle::SetWidth() and Rectangle::SetHeight() in Square implementation.
There was a problem hiding this comment.
You right. Code result is 20. But i sure, this must be return 25 for BAD example.
But as fact - this LSP examples incorrect. This code is not executable. Alot errors, types, missed signatures and etc.
All problems into RenderLargeRectangles method. This cant be in correct code.
- This must be only render. But now this change sizes.
- In foreach object cast to Rectangle. Sure we cant set sizes for square.
Initialization of array must be into main block. This normaly for simple example.
Rectangle rectangle1 = new Rectangle();
rectangle1.SetWidth(2);
rectangle1.SetHeight(3);
Rectangle rectangle2 = new Rectangle();
rectangle2.SetWidth(4);
rectangle2.SetHeight(5);
Square square = new Square();
square.SetWidth(4);
square.SetHeight(5);
var rectangles = new[] { rectangle1, rectangle2, square };
RenderLargeRectangles(rectangles);
BAD:
- RenderLargeRectangles(Rectangle rectangles) -> Rectangle[] rectangles
- SetWidth, SetHeight must have return type is void or double.
- Render() signature is void. Drawable not used into examples.
GOOD:
- RenderLargeRectangles: parameter must be ShapeBase of type. And array too.
- RenderLargeRectangles: for set size we must use casting for each object type:
((Square)rectangle).SetLength(5);
and etc. errors.
Can you fix both examples?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix comment