-
-
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
New index.js (tiny JS) for OOP Exercise #342
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, |
@OleksiyRudenko I'm ready to check |
@OleksiyRudenko Hello? tell me, are you all right? when will you be able to give feedback? Thank you. |
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.
@Levi123 need to make the code OOP compliant.
If two classes have the same property then they need a common base class that handles this shared property.
This will be OOP-compliant and will make the code DRY.
Also see comments below.
const allResidents = [woman, man, dog, cat, catWoman] | ||
|
||
function printAllResidents(allResidentsArray) { | ||
for (let i = 0; i < allResidentsArray.length; i++){ |
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.
Why not an Array
method to loop through the residents?
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.
Done
|
||
function printAllResidents(allResidentsArray) { | ||
for (let i = 0; i < allResidentsArray.length; i++){ | ||
const prop = Object.keys(allResidentsArray[i]); |
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.
From the checklist:
-
Object
methods likekeys
,values
,entries
as well asfor...in
shouldn't be used as these do not guarantee any particular order of keys/values
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.
From the checklist:
Object
methods likekeys
,values
,entries
as well asfor...in
shouldn't be used as these do not guarantee any particular order of keys/values
Done
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.
@Levi123 there are improvements, although in other parts the code became less clear.
Note that a comment can relate to multiple similar code fragments. You will need to extrapolate it by yourself.
Check the comments below. Double-check yourself against the checklist posted by bot and refer to the learning materials if needed.
class Human extends Entity { | ||
constructor(options){ | ||
super(options); | ||
this.species = 'Human'; |
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.
From the checklist
A parent class shouldn't know anything about its children's props. Child classes must have good reasons to assign or refer to any parent's props. There are no such reasons in this task. Method overloading is to the rescue when building an inhabitant's presentation string.
// } | ||
|
||
class Entity { | ||
constructor (options){ |
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.
Code is written for other people.
As a peer developer I am tasked to add a class that extends entity. In our team we are used to rely on code editors intellisense. Having current constructor specification it will only tell that constructor expects some "options". We would need to go through the code and read the method body to understand what is expected. As a team member you will also love when peer developers take care of you and write clear code.
At the same time if function specification is detailed there is no such need. There are methods to write self-explaining functions specifications: (1) regular list of parameters, (2) object destructuring syntax when parameters are supplied as objects. Use either of those.
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.
Code is written for other people. As a peer developer I am tasked to add a class that extends entity. In our team we are used to rely on code editors intellisense. Having current constructor specification it will only tell that constructor expects some "options". We would need to go through the code and read the method body to understand what is expected. As a team member you will also love when peer developers take care of you and write clear code.
At the same time if function specification is detailed there is no such need. There are methods to write self-explaining functions specifications: (1) regular list of parameters, (2) object destructuring syntax when parameters are supplied as objects. Use either of those.
Object destructuring, I understood correctly?
For example:
class Entity {
constructor ({name, species, gender, saying}){
this.name = name;
this.species = species;
this.gender = gender;
this.saying = saying;
}
}
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.
@Levi123 good job.
Let's leverage the power of OOP, so that you are able to use JS OOP mechanism to their full extent when a bigger project arrives.
arrayAllObjects.forEach((characters) => { | ||
toPrint(characters); |
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.
Does forEach
receive multiple characters or a single character on every iteration?
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.
@OleksiyRudenko single character on every iteration
const allProp = ['name', 'species', 'gender', 'saying', 'legs', 'hands', 'paws'] | ||
|
||
|
||
function toPrint (arrayAllResidents) { | ||
const propToPrint = allProp.filter((propOfObj) => arrayAllResidents[propOfObj] !== undefined) | ||
.map((propOfObj) => arrayAllResidents[propOfObj]).join('; '); | ||
print(propToPrint); | ||
} |
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.
This approach was good enough before OOP.
Following the OOP paradigm classes encapsulate properties and behaviour (properties handling).
Every class handles props it owns immediately.
Method overloading and explicit call to inherited method version via super
methods are to the rescue.
Task and original project docs offer links to the materials you will find helpful in these regards.
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.
This approach was good enough before OOP. Following the OOP paradigm classes encapsulate properties and behaviour (properties handling). Every class handles props it owns immediately. Method overloading and explicit call to inherited method version via
super
methods are to the rescue. Task and original project docs offer links to the materials you will find helpful in these regards.
done
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.
@Levi123 great job!
Let's polish it.
Naming is important. Take learnings for any future proejcts - educational or commercial - you will contribute to.
// class Animal { | ||
// constructor (species, name, gender){ | ||
// this.species = species; | ||
// this.name = name; | ||
// this.gender = gender; | ||
// } | ||
// } |
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.
Don't keep comments in production code.
Exception: comments that explain reasons behind implementation peculiarities, that cannot be understood from the code itself (e.g. we cache data because server is down often and we want users to have some data anyways)
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.
Done
// } | ||
// } | ||
|
||
class Entity { |
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.
entity
is a generic term. Business domain and task requirements are a good source of naming ideas. Name should describe what entity it represents at a given level of abstraction - dog, cat, human, mammal, herbivore - in the given business domain.
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.
Done
|
||
class Animal extends Entity { | ||
constructor(name, gender, saying){ | ||
super(name, 'Animal', gender, saying); |
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" is not a species.
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.
I changed the name of the main parent class entity to mammal. The animal class has a species - animals, and a person - human species I think this is not a mistake, but simply a manifestation of fantasy in this task. We do not receive a clear structure that should be followed.
|
||
class Woman extends Human { | ||
constructor(name, saying){ | ||
super(name, 'Woman', saying) |
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.
Men and women are the same species.
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.
Yes, a man and a woman get one species - a person. But it has a gender property to display gender, if that's what you're after.
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.
@Levi123 great job!
OOP exercise
Demo |
Code base
The code is submitted in a dedicated feature branch.
Only code files are submitted.
Please, review.