Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Functions are cleaner than subclasses #116

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

Closed
wants to merge 8 commits into from

Conversation

DavidVujic
Copy link
Contributor

A drawback with a class extending the Array global, is that you are required to instantiate it using the 'new' keyword. Also, the 'diff' feature is limited to the subclass only.

@IAmAdamTaylor
Copy link

Agree with this due to the ability to create array literals. Arrays created by JSON.parse() also wouldn't get the new functionality.

Should the function name be diffArray() to express that specifically works with arrays?

Copy link
Owner

@ryanmcdermott ryanmcdermott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey David! Thank you for contributing your expertise. I've left some comments.

@@ -609,6 +609,35 @@ class SuperArray extends Array {
```
**[⬆ back to top](#table-of-contents)**

### Functions are often cleaner than subclasses ###
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this replace the SuperArray example?

Copy link
Contributor Author

@DavidVujic DavidVujic Jan 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would instead recommend a rewrite of the SuperArray example. Will update this PR with rewitten section.


A class will limit the usage of the diff feature to the subclass only. A better option is to write a stateless function, that accept two arrays and calculate the difference between them.

```javascript
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't keep with the format of Bad/Good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@DavidVujic
Copy link
Contributor Author

DavidVujic commented Jan 12, 2017

Thank you for the feedback @ryanmcdermott! I will update the pull request.

@DavidVujic
Copy link
Contributor Author

DavidVujic commented Jan 12, 2017

In this updated PR, I have added a new section using the Bad/Good format.

@DavidVujic DavidVujic changed the title Functions are often cleaner than subclasses Functions are cleaner than subclasses Jan 14, 2017
@vsemozhetbyt
Copy link
Contributor

@DavidVujic Sorry, the original code was changed in #124.

@DavidVujic
Copy link
Contributor Author

Great @vsemozhetbyt! I saw the changes, very nice improvement.

@vsemozhetbyt
Copy link
Contributor

Thank you) I mean, maybe it would be good for consistency to update these fragments in your examples as well.

@DavidVujic
Copy link
Contributor Author

Done!

Now hoping for a merge and/or more feedback for improvement.

@ryanmcdermott
Copy link
Owner

I think this is a really great idea for an addition! The only issue I have is that SuperArray holds state in that the primary state is the array data itself. Is there another simple example we can use? I don't want to contradict with the other use of SuperArray. I've had some consistency problems in this project that are almost fully resolved 😄

@DavidVujic
Copy link
Contributor Author

DavidVujic commented Jan 17, 2017

Thank you for the feedback @ryanmcdermott! I have given it some thought and think the "good" part of the SuperArray example should be a stateless function, because of some drawbacks that I think are major:

  • complex syntax: const arr = new SuperArray(); instead of the much cleaner shorthand way: const arr = [];
  • diff is a feature that shouldn't require any state.
  • only instances of SuperArray (or extensions of that class) can use the Array diff function.

Let me know what you think! If you don't like it, please tell me and I will close the pull request.

(I have updated the pull request. It is updated with a function as "good" instead of the class.)

@ryanmcdermott
Copy link
Owner

Hey David! Thanks for spending time revising this. I want to combine this PR with another: #139

The motivation is that I think we should say something about the nature of classes in the preamble of the Classes section. We can incorporate your thoughts into that introduction. I want to leave this example the way it is for now! If you have time would love to see your work as an intro in the Classes section!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants