Git Product home page Git Product logo

Comments (11)

sosioo avatar sosioo commented on August 26, 2024 1

I'd love to see an example of this concept without Classes...

from dmitripavlutin.com-comments.

taniot avatar taniot commented on August 26, 2024

Hi @panzerdp. Great Article as always :)

from dmitripavlutin.com-comments.

hedwardd avatar hedwardd commented on August 26, 2024

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.

panzerdp avatar panzerdp commented on August 26, 2024

Hi @panzerdp. Great Article as always :)

Thanks @taniot!

from dmitripavlutin.com-comments.

Jaanilj avatar Jaanilj commented on August 26, 2024

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.

panzerdp avatar panzerdp commented on August 26, 2024

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.

Red02Raccoon avatar Red02Raccoon commented on August 26, 2024

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.

panzerdp avatar panzerdp commented on August 26, 2024

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.

codthing avatar codthing commented on August 26, 2024

It can also be understood as extracting reusable methods

from dmitripavlutin.com-comments.

polyakh avatar polyakh commented on August 26, 2024

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.

chuckwondo avatar chuckwondo commented on August 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.