Git Product home page Git Product logo

Comments (5)

wardbell avatar wardbell commented on July 24, 2024 1

My $0.02. The rule of thumb in Object Orientation ( dating to the 1980s) is "favor composition over inheritance". That advice tilts toward injected services for common functionality, perhaps with a light weight prototype to do some internal service orchestration.

Deep inheritance chains are a code smell, whether in static or dynamic languages, class or prototypal inheritance.

from angular-styleguide.

johnpapa avatar johnpapa commented on July 24, 2024

If the code is reusable, shouldn't that code be in a service of its own?

from angular-styleguide.

patrick-fls avatar patrick-fls commented on July 24, 2024

For services I guess you're right but for controllers? Or if I feel a controller should be inherited, I'm doing something wrong?

from angular-styleguide.

johnpapa avatar johnpapa commented on July 24, 2024

It's not wrong or right, it's more about what you are trying to accomplish and finding the right tool for that. So let's take as step back and think about what prototypal inheritance is good for. Often it helps when you have lots of instances of the same object and you want to share the functions. If you create 1000 customer objects and they all share some functions such as checkCredit, it would be nice not to use memory for 1000 functions. All 1000 object instances could share that checkCredit function. But are you going to create lots of controllers where this would be super helpful? Where is it valuable? 5, 10, 100?

But you might have code that every controller seems to have, that's when I drop that code into a service/factory. Could I use prototypal inheritance? Sure, but factories are just as good and simple to test.

I'm not trying to way you either way, just give you some food for thought. So go with your instincts on this.

from angular-styleguide.

ddanailov-nmdp avatar ddanailov-nmdp commented on July 24, 2024

@johnpapa - What do you think for this article: Object-oriented AngularJS Services ?

This article follow the guide suggestions ?

from angular-styleguide.

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.