-
-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Oop exercise #353
Oop exercise #353
Conversation
Hey! Congratulations on your PR! 😎😎😎 Let's do some self-checks to fix most common issues and to make some improvements to the code before reviewers put their hands on the code. Go through the requirements/most common mistakes listed/linked below and fix the code as appropriate. If you have any questions to requirements/common mistakes feel free asking them here or in Students' chat. When you genuinely believe you are done put a comment stating that you have completed self-checks and fixed code accordingly. Also, be aware, that if you would silently ignore this recommendation, a mentor can think that you are still working on fixes. And your PR will not be reviewed. 😒 A Tiny JS World -- OOP exercise check listRelates to A Tiny JS World OOP exercise. Check-list - definition of doneMinimal requirements to meet:
Optional level up (not required to implement):
Bonus:
Helpful resources: Universal recommendations:
Also take a note of the requirements above and follow them in all your future projects. By the way, you may proceed to the next task before this one is reviewed and merged. Sincerely yours, |
Good morning. For the first view in self-check, everything seems to be alright. Humbly asking for review :) |
This issue has been automatically marked as stale because there were no activity during last 14 days. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. А. Чому так? Б. Що робити, якщо в піарі нема оновлень, оскільки не зрозуміло, що треба зробити? В. А якщо я все зробив(ла) і це ментор не рев'юває мої зміни?
Г. Хіба недостатньо того, що я додав(ла) коміт із змінами? Традиційна пропозиція: задай питання по вищенаписаному в студентському чаті. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tedzury good job!
Yet implementation needs to follow OOP and SOLID principles.
"species", | ||
"name", | ||
"gender", | ||
"saying", | ||
"friends", | ||
"legs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A base class should never handle child classes' properties. This breaks Encapsulation, Open-Closed and Separation of Concerns principles as any changes to child classes would require changes in one (or more) base class up the hierarchy.
Look into method overloading to have consistent interface and user experience where method implementation depends on class yet allows to re-use parent class' method for a similar purpose.
constructor(name, gender, saying, friends) { | ||
super(name, gender, saying, friends); | ||
this.species = "human"; | ||
this.legs = 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
legs
looks like a shared property. Should be generalized to a base class just like any other shared property.
Same refers to species
.
The criteria of generalization is property per se. It can bear different values for different class instances.
…them in mammal class
Good afternoon. Removed legs from base properties, added class Mammal and put legs to it. Not sure that I get you right with this: "Look into method overloading to have consistent interface and user experience where method implementation depends on class yet allows to re-use parent class' method for a similar purpose." I tried to implement that in such way, to prettify the printing output in needed order, added ternary operator into output func to modify the order of properties. Hope, it's what was needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tedzury quite a decent job.
Let's make it more OOP/SOLID compliant.
Method overloading and extended super
API are to the rescue.
"species", | ||
"gender", | ||
"legs", | ||
"hands", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every class should handle the props it owns immediately.
As a developer you do not want to touch base classes when adding a new one. This is what encapsualtion and open-closed principles from OOP/SOLID are about.
|
||
const tinyWorldInhabitants = [barbos, sonya, oleksii, victoria, anjela]; | ||
|
||
tinyWorldInhabitants.forEach((item) => print(item.output())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
item
is a generic term.
"saying", | ||
"friends", | ||
]) | ||
: (this.prettyOutput = this.propsArray); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nested ternaries considered a bad practice. Can we make this fragment more obvious? KISS
Good night. Slightly refactored output function. Removed propsArray, applied function overrading to output( ) method and modified it into child classes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tedzury well done.
Let's polish it using learnings from past tasks.
|
||
class Mammal extends Creature { | ||
constructor(species, name, gender, saying, friends, legs) { | ||
super(species, name, gender, saying, friends);git |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
git
here should have thrown an error. Didn't it?
} | ||
|
||
output() { | ||
return `${this.name}; ${this.species}; ${this.gender}; ${this.saying}; ${this.friendsToString()}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More than 2 items. Array#join
is still a thing.
Won't be a really good approach, but think of how destructuring could help you in more complicated cases:
const { propA, propC, propB } = this
Good day. Removed typo, apllied destructuring to properties in output( ) method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tedzury let's optimize hierarchy.
Introducing classes just for the purpose of setting default values is an overkill.
I am not even sure we need classes for men and women. Or shy dogs and cats do not deserve sheDog/Cat and heDog/Cat classes?
output() { | ||
const { name, species, gender, saying } = this; | ||
|
||
return `${name}; ${species}; ${gender}; ${saying}; ${this.friendsToString()}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's employ Array#join
. Change this line only.
Remember, anything can be an array element. [ func() ]
will add a result from func call as an array element.
class Mammal extends Creature { | ||
constructor(species, name, gender, saying, friends, legs) { | ||
super(species, name, gender, saying, friends); | ||
this.legs = legs; | ||
} | ||
output() { | ||
return `${super.output()}; ${this.legs}`; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mammal
is the only class that extends Creature
. Why do we need Mammal
?
KISS
class Animal extends Mammal { | ||
constructor(species, name, gender, saying, friends) { | ||
super(species, name, gender, saying, friends, 4); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Animal
extends Mammal
just to pass a specific value.
Solvable by setting default argument value.
KISS
Good night. Removed unneded animal class, man and woman. Just passing legs as default value, and gender to humans as an argument. Also implemented array.join in output method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tedzury nearly there
output() { | ||
const { name, species, gender, saying, friends } = this; | ||
|
||
return `${name}; ${species}; ${gender}; ${saying}; ${friends.join(", ")}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is still repetition ("; "
). Pack whatever you need into an array and Array#join
.
You will benefit a lot from recognizing use cases for Array
methods.
} | ||
} | ||
|
||
class Mammal extends Creature { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mammal
is the only class that extendsCreature
. Why do we needMammal
?
Any good reason for this?
Also, obviously there is no good reason to prefer 4 for legs. Decisions should be explainable.
Good morning. Left only base class and Human/Dog/Cat, legs are passed as default property in child classes. Implemented array with props to use array.join in the output method. Sorry, but I feel very confused, I had exactly the same way of array.join in output two commits ago, but I undestood you in such way, that I shouldn't use it, so tried everything else, except manipulations with array :( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tedzury great job done!
Make sure to put some learnings on your diary.
this.saying = saying; | ||
this.friends = friends; | ||
this.legs = legs; | ||
this.propsArray = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Data type name as a part of variable name is not necessary (think if you switch to Map
data type, you'd need to rename variable to reflect the change).
Plural noun is just sufficient.
Also props
is quite specific term and kinda technical (as in e.g. "component props"). Here we mean "properties of inhabitant", just a normal word that in real life is never abbreviated. Do not save keystrokes on names, IDE will help you to autocomplete even German-language-style variable names.
OOP exersice
Demo |
Code base
The code is submitted in a dedicated feature branch.
Only code files are submitted.
Please, review.