Comments (11)
I'd love to see an example of this concept without Classes...
from dmitripavlutin.com-comments.
Hi @panzerdp. Great Article as always :)
from dmitripavlutin.com-comments.
Although this example seems object-oriented, the principle and implementation reminds me of what I understand to be functional programming. Either way, very cool and great article!
from dmitripavlutin.com-comments.
Hi @panzerdp. Great Article as always :)
Thanks @taniot!
from dmitripavlutin.com-comments.
Awesome article! I really like your takes on software design, and I hope you will write more of them in the future :)
from dmitripavlutin.com-comments.
Awesome article! I really like your takes on software design, and I hope you will write more of them in the future :)
Thanks, @Jaanilj! Yes, I'm learning a lot about software design and will try to write more useful things in the future.
from dmitripavlutin.com-comments.
Good explanation. It seems the last version of the render method is a violation of the Single responsibility principle. Am I wrong?
from dmitripavlutin.com-comments.
Good explanation. It seems the last version of the render method is a violation of the Single responsibility principle. Am I wrong?
Nice catch @Red02Raccoon! Seems like the last version violates SRP because it renders the list, and also invokes the sorting.
from dmitripavlutin.com-comments.
It can also be understood as extracting reusable methods
from dmitripavlutin.com-comments.
Hi. I guess it's a wrong link "For example, if the ListRenderer will always sort the names alphabetically in ascending order: then stop at the design presented at the beginning of section 2. Do not add unnecessary complexity." => 2. Have a nice day :) Thanks
from dmitripavlutin.com-comments.
I see the last comment was made almost a year ago, but thought I'd add my 2 cents in case anybody is still interested.
@sosioo, here's one possible solution without classes:
const localeCompareAsc = (s1: string, s2: string) => s1.localeCompare(s2);
const localeCompareDesc = (s1: string, s2: string) => localeCompareAsc(s2, s1);
const renderListItem = (item: string) => ` <li>${item}</li>`;
const renderList = (items: string[]): string =>
["<ul>", ...items.map(renderListItem), "</ul>"].join("\n");
const ascending = false; // Perhaps obtained via system input
const comparator = ascending ? localeCompareAsc : localeCompareDesc;
const names = ["Catwoman", "Joker", "Batman"];
const renderedNames = renderList([...names].sort(comparator));
console.log(renderedNames);
// <ul>
// <li>Joker</li>
// <li>Catwoman</li>
// <li>Batman</li>
// </ul>
This also addresses @Red02Raccoon's point about single responsibility because the renderList
function only renders the list. Sorting is handled independently, before the list is passed to renderList
.
from dmitripavlutin.com-comments.
Related Issues (20)
- javascript-json-modules/ HOT 7
- javascript-import-meta/ HOT 3
- javascript-array-group/ HOT 16
- javascript-array-flatmap/ HOT 23
- vue-next-tick/ HOT 4
- props-destructure-vue-composition/ HOT 7
- ref-reactive-differences-vue/ HOT 5
- environment-variables-javascript/ HOT 3
- ref-in-vue/ HOT 1
- typescript-function-type/ HOT 5
- vue-v-model-form-inputs/ HOT 2
- react-forwardref/ HOT 5
- javascript-enum/ HOT 20
- react-comments/ HOT 8
- typescript-record/ HOT 10
- screen-window-page-sizes/ HOT 3
- javascript-pure-function/ HOT 1
- Great article
- orthogonal-react-components/ HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dmitripavlutin.com-comments.