News for hackers. Stuff that matters.
Re:Ask HN: How often do you use inheritance? by probinso on Sunday May 14, 2017 @11:34PM (#14338973)
Inheritance is a great way to allow your newly defined class to interface with the language syntax or system environment features in a useful way. Doing something like inheriting from a dictionary in Python allows for you to extend a very versatile tool that already exists, that has been optimized for use in the language.

If you are going to bother inheriting from a class, then you should weigh the cost of your code complexity against it. It is definitely worth it sometimes

If you wrote both the class and sub-class, then you're likely over engineering the problem (unless you have a sufficiently large program you're working on)

Re:Ask HN: How often do you use inheritance? by tjalfi on Friday May 12, 2017 @10:45PM (#14329323)
I have used inheritance twice in the past two years. Here is the most recent example. The code is from an iOS application written in Swift. The task was to write a quiz application. I used a combination of a protocol and a base class to avoid duplicating the code to advance to the next question. All view controllers implement the Scorable protocol and the view controllers for individual questions all inherit from ScorableViewController.

// Scorable.swift

import Foundation

  protocol Scorable {
    var totalScore : Int {get set}
  }

// ScorableViewController.swift

import UIKit

class ScorableViewController: UIViewController, Scorable {

    //overrides here...
    
    //This class exists to share the following code.
    var totalScore : Int = 0
    var currentChoice : Selection? = nil
    
    final var choseOption : Bool {
        get {
            return currentChoice != nil
        }
    }
    
    final override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        precondition(choseOption, "must choose option before proceeding")
        precondition(segue.identifier != nil, "identifier is nil")
        precondition(!segue.identifier!.isEmpty, "identifier is empty")
        var dest = segue.destination as! Scorable
        dest.totalScore = Selection.add(score: totalScore, selection: currentChoice!)
    }
}
Re:Ask HN: How often do you use inheritance? by twobyfour on Saturday May 13, 2017 @08:39AM (#14330667)
Constantly. My company uses a popular framework that supports complex behavior defined largely in a declarative manner using inheritance; with method overrides for customizing business logic.

I also frequently use multiple inheritance and what are called "mix-in" classes in python to make more complex customizations reusable.

Re:Ask HN: How often do you use inheritance? by wayn3 on Sunday May 14, 2017 @03:39PM (#14337186)
MixIn is just multiple inheritance rebranded.
Re:Ask HN: How often do you use inheritance? by twobyfour on Monday May 15, 2017 @06:00AM (#14340374)
Yes. Mixins are a particularly handy and slightly less dangerous form of multiple inheritance than multiple inheritance from two fully-fledged and usable classes (which IME causes more problems than it solves). I didn't intend to imply they weren't multiple inheritance. Would it have been clearer to have said "with" instead of "and"?
Re:Ask HN: How often do you use inheritance? by tony on Friday May 12, 2017 @10:13PM (#14329256)
Inheritance is helpful when the domain is well-defined and the underlying problem is broadly similar enough to fill-in differences across what's being abstracted.

Some hand-picked examples of well-implemented inheritance in open source:

An example of where inheritance makes great sense is Django CBV (Class-based views), https://docs.djangoproject.com/en/1.11/topics/class-based-vi..., as well as Django REST Framework class views (http://www.django-rest-framework.org/api-guide/views/)

An example of where inheritance in the form of runtime metaprogramming makes sense is SQLAlchemy declarative and Django ORM.

An example where inheritance in the form of mixins make sense would be Django ORM, such as TimeStampedModel in django-extensions https://github.com/django-extensions/django-extensions/blob/....

An example where classes make sense in standard libraries would be (impure) interfaces such as csv.Dialect (https://github.com/python/cpython/blob/6aee6fb/Lib/csv.py#L2...)

An example of where inherited classes make sense in abstraction would be types (https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalch...) and dialects in SQLAlchemy (https://github.com/zzzeek/sqlalchemy/tree/master/lib/sqlalch...)

As a closing note, It works a lot better when the use of inheritance symbolically represents actual standards (SQL, HTTP, and/or standard libraries API's) that developers already are familiar with, rather than abstract concepts meaningful only to the writer.

Re:Ask HN: How often do you use inheritance? by foobarchu on Friday May 12, 2017 @09:20PM (#14329123)
Honestly, very rarely. Interfaces have been far more useful to me in my career, as far as major OO concepts go. If I'm extending, it's usually because someone brought in a new requirement after a products launch that requires very similar, but not quite the same, behavior as existing work. For example, we process data feed A already, and data feed B will be very similar. Rather than modifying the existing A processor, its often easier to lift out an abstract parent class and implement A and B as extensions of it. I almost never find myself extending concrete classes.
Re:Ask HN: How often do you use inheritance? by itamarst on Friday May 12, 2017 @03:29PM (#14327112)
It's usually a bad idea, and often you have no choice because of library design.
Re:Ask HN: How often do you use inheritance? by odonnellryan on Friday May 12, 2017 @04:42PM (#14327745)
Some APIs work really well with the pattern, but I've only used 2-3 that were "wow, that's really neat!"
Re:Ask HN: How often do you use inheritance? by Cieplak on Saturday May 13, 2017 @12:07AM (#14329488)
I use inheritance only if the model satisfies the Liskov Substitution Principle and if the language has strong OO primitives. Functional languages accomplish similar goals with different abstractions, e.g., type polymorphism, pattern matching, functional composition.
Re:Ask HN: How often do you use inheritance? by Cieplak on Saturday May 13, 2017 @12:13AM (#14329501)
As an example, working in the payments business, we often model credit cards and bank accounts as different subclasses (or types) of a common superclass, given that these objects share a lot of semantics, i.e., they both send and receive payments.
Re:Ask HN: How often do you use inheritance? by mabynogy on Friday May 12, 2017 @07:59PM (#14328860)
Never. Only plain objects in JS and structs in C++.

IMHO OOP is a trap.

Re:Ask HN: How often do you use inheritance? by cpcat on Friday May 12, 2017 @03:21PM (#14327048)
If you're an Android developer basically most of your work is extending existing classes.
Re:Ask HN: How often do you use inheritance? by coralreef on Sunday May 14, 2017 @09:46PM (#14338576)
In iOS development, all the time. I subclassed UI components like UITextView and other things so I could customize its appearance and add functionality.
Re:Ask HN: How often do you use inheritance? by fuball63 on Monday May 15, 2017 @08:44AM (#14341095)
In my web development career (with python) it is usually only simple mixin-like inheritance, mostly because I can't stand code redundancy.

In game development though, it is way easier to develop a full fledged class hierarchy. I think it is because OOP is supposed to model a physical world of objects, which is generally what you'll be simulating in games too.

Re:Ask HN: How often do you use inheritance? by wayn3 on Sunday May 14, 2017 @03:38PM (#14337181)
You use inheritance when a library provides an interface that you want to use. Other than that, you're probably better off not using it. Certainly not in the idiot Programming 101 "lets design a restaurant" OOP way.
Re:Ask HN: How often do you use inheritance? by anustretch on Friday May 12, 2017 @05:03PM (#14327917)
Over time you'll get a better sense of which methods or properties ought to be overloaded, and how/when to invoke super(). So it's best to think of polymorphism as residing in gradations, best applied infrequently.
Re:Ask HN: How often do you use inheritance? by gusmd on Sunday May 14, 2017 @12:05PM (#14336128)
It is kind of a necessity in some domains. For instance, I could cite Android applications in Java, or using Qt in C++.
Re:Ask HN: How often do you use inheritance? by AznHisoka on Sunday May 14, 2017 @08:20AM (#14335317)
Just once in the past 5 years. But I hear its very useful in dummy, trivial applications with no real life purpose.