Skip to content
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

Replacing classes with modules and functions #46

Merged
merged 6 commits into from
Dec 9, 2022
Merged

Conversation

Cruikshanks
Copy link
Member

Classes are a template for creating objects.

That is the first sentence of the Mozilla documentation for Classes. It is what those of us from other languages understand classes to be for. For some of us though (we're looking at you @Cruikshanks !) classes are all things!

Because of that thinking, we've allowed Classes to creep in where they shouldn't really be used. Most noticeably, things like controllers, presenters and services, are formed of purely static methods. There is no real reason to wrap their functionality in a class. They could just be expressed as modules and export their relevant functions.

That's what those experienced with JavaScript are more likely to expect. By not doing things that way we're breaking the Principle of least astonishment.

We believe classes are suitable for our Models, as it follows the convention of Objection and we do create instances of them. Also, classes will be our go-to if we need to create objects in the future.

But before this repo gets too far down the road we're taking this opportunity to refactor things from classes into modules.

@Cruikshanks Cruikshanks added the housekeeping Refactoring, tidying up or other work which supports the project label Dec 8, 2022
@Cruikshanks Cruikshanks self-assigned this Dec 8, 2022
@Cruikshanks Cruikshanks added the do not merge Used for spikes and experiments label Dec 9, 2022
@Cruikshanks
Copy link
Member Author

We have #37 ready to be merged. Going to wait for that then will rebase this, make the changes and then make it ready for review.

> Classes are a template for creating objects.

That is the first sentance of the Mozilla documentation for **Classes**. It is what those of us from other languages understand classes to be for. Some of us though (we're looking at you @Cruikshanks) classes are _all things_!

Because of that thinking, we've allowed Classes to creap in where they shouldn't really be used. Most noticeably, things like controllers, presenters and services, which are formed of purely static methods. There is no real reason to wrap their functionality in a class. They could just be expressed as modules and export their relevant functions.

That's what those experienced with JavaScript are more likely to expect. By not doing things that way we're breaking the [Principle of least astonishment](https://en.wikipedia.org/wiki/Principle_of_least_astonishment).

We're believe classes are right for our Models, as it follows the convention of [Objection](https://vincit.github.io/objection.js/guide/models.html) and we do create instances of them. Also, if we have need to create objects in the future, classes will be our go-to.

But before this repo gets to far down the road we're taking this opportunity to refactor things from classes into modules.
Note - this forced us to review how we'd handled 'current date' in the BillingPeriodService. We found Sinon's [useFakeTimers()](https://sinonjs.org/releases/latest/fake-timers/) which is the more appropriate mechanism for controlling what `Date` returns during our tests.
@Cruikshanks Cruikshanks marked this pull request as ready for review December 9, 2022 10:49
@Cruikshanks Cruikshanks removed the do not merge Used for spikes and experiments label Dec 9, 2022
Copy link
Contributor

@Jozzey Jozzey left a comment

Choose a reason for hiding this comment

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

@Cruikshanks Cruikshanks merged commit 06d45e8 into main Dec 9, 2022
@Cruikshanks Cruikshanks deleted the going-classless branch December 9, 2022 11:30
Cruikshanks added a commit that referenced this pull request Dec 9, 2022
Follows on from [Replacing classes with modules and functions](#46) as we managed to miss some bits!

This second attempt hopefully covers alll the bits we missed on the first pass of refectoring things away from classes.
Cruikshanks added a commit that referenced this pull request Dec 9, 2022
This follows on from [Replacing classes with modules and functions](#46) as we managed to miss some bits!

This second attempt hopefully covers all the bits we missed on the first pass of refactoring things away from classes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
housekeeping Refactoring, tidying up or other work which supports the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants