TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Do you use inheritance when programming?

19 点作者 jameskerr将近 5 年前
I've been writing Ruby and JavaScript for 10 years and have rarely felt the need to use inheritance. The times I have used it, or seen it used, the code was less understandable. Do any of you use inheritance in your apps with success?

22 条评论

dyingkneepad将近 5 年前
The Linux Kernel codebase is <i>full</i> of inheritance, if you are open to understand how inheritance gets implemented in plain C.<p>A graphics driver for a certain vendor inherits from the DRM class, which inherits from PCI, which inherits from the Device class. Structs which embeds structs which embed structs, and you can access the base classes using the container_of() macro.<p>As for me, I particularly abuse inheritance in C++, although I a lot of cases I end up replacing it with template programming. To the luck of society, most of the code where I abuse templates and inheritance is not used by anybody else except for me.
评论 #23852418 未加载
gary_0将近 5 年前
Only when there&#x27;s a blatantly obvious reason to. The example that immediately comes to mind is GUI elements: there&#x27;s both a lot of code that many similar-but-different class instances share, and a strong &quot;is-a&quot; semantic. Even then, most of the hierarchy is only 1 level deep (Button -&gt; Element).<p>Flat is better than nested.
AnimalMuppet将近 5 年前
I was doing some genetic programming research. Inheritance was beautiful, because I could have an Instruction base class, and all the possible specific instructions were derived from that. The execution engine and reproduction stuff could just deal with Instructions, without knowing any specifics about any of them. Each specific instruction knew how to execute itself and how to copy itself. It made things <i>so simple</i>.<p>When there&#x27;s a good reason to use it, use it. If there&#x27;s not, don&#x27;t. Nobody&#x27;s forcing you to use it.
Gibbon1将近 5 年前
I most of the time writing crummy firmware in C. You can do inheritance in C and it always feels like an antipattern. In C# mostly never unless a library requires it. Especially since C# has generics without type erasure.<p>I think inheritance is a bad solution to the problem of wanting to call foo&#x27;s baz() method or access a property. Been way long since I poked at Ruby or Javascript but I thought those languages allow you to do that without inheritance. If so why would you even bother 99.9% of the time.
cordite将近 5 年前
Occasionally for edge cases in Java, but generally I go for composition with interfaces.<p>Never ever do I do this for tables &#x2F; entities or controllers. Things get fragile fast with that.
评论 #23828335 未加载
noema将近 5 年前
Inheritance can be clean and expressive when used diligently. If you wish to express a tight cohesion between classes with default functionality (especially when it pertains to instance variables), an abstract parent class can keep the shared logic in a single place. I run into this often when dealing with Android UI code which tends to involve many &quot;variations on a theme&quot; classes.
muzani将近 5 年前
All the time. The easiest example is that we have a search box on the top of every screen that makes the same API calls and has the same results (wiki-style app). The code also shares a lot of similar functionality that&#x27;s on almost every screen, like displaying a loading prompt. We put these in some base classes.<p>Larger codebases also use them extensively. I worked on a project which had multi-millions in funding. Basically it was video streaming functionality, dealt with DRM&#x2F;CA, rewind, optimization, all that done. We were on the UI team, meaning we build a different UI based for a specific client brand, utilizing the same code as in done in many countries.<p>With those, you don&#x27;t build a custom code from scratch. You just inherit and modify.<p>I only use them heavily in Java though, never JavaScript, even though we built that video streaming app in both. It&#x27;s just a different paradigm. I can live fine without inheritance though. It&#x27;s just a nice to have.
jameskerr将近 5 年前
This has been great feedback. To summarize, I heard people use inheritance because its the API of a chosen framework and in rare cases when it proves convenient like UI Elements.<p>I also remembered that unit test suites are an instance when I&#x27;ve enjoyed inheritance.<p>But it sounds like most people here do not use inheritance in the business logic of their apps&#x2F;systems. Composition and dependency injection do the job of sharing code without sacrificing comprehensibility.<p>Thanks everyone! This all matches my experience as well. I just wanted to make sure I wasn&#x27;t missing something life-changing!
guinness74将近 5 年前
Yes. The textbook examples really don&#x27;t do practical applications justice though, e.g.:<p><pre><code> shape ... circle extends shape </code></pre> I find it very useful to extend classes in an existing framework or system with a new functionality specific for my particular problem.<p>For a general example, sub-typing exceptions is useful.
评论 #23825451 未加载
mwhite将近 5 年前
One-level deep inheritance can be great for abstracting common functionality, and multi-level inheritance sometimes makes sense in very large applications. What you want to watch out for is multi-level inheritance with an inheritance hierarchy that parallels some hierarchy that is merely <i>incidental</i> to the code&#x27;s functionality, like the page navigation hierarchy.<p>I would say that having a multi-level inheritance hierarchy with greater than &quot;constant&quot; number of children per parent in one package is a code smell.
elamje将近 5 年前
I’ve recently been doing it. We use Dapper ORM. It’s super light weight and easiest to make classes that map 1-1 with database columns since Dapper does reflection to match up property names and column headers.<p>I’ve found the best way to add some extra view data to a class that is directly backed from a DB table is to inherit from the 1-1 class that Dapper uses, and just add the extra data in the child class.<p>That way dapper doesn’t get confused by properties not mapped to the database table, but I get to use the extra data properties in my view.
gitgud将近 5 年前
Inheritance of classes with behaviours has only ever brought me pain and suffering.<p>Though sometimes I&#x27;ll have an inheritance tree of interfaces, which is pretty easy to manage.<p>The best way I&#x27;ve found to share behaviour is dependency injection, it&#x27;s more flexible and consistent than shared methods in a parent class. But you need a DI framework to manage all the instantiation, which can add undesired complexity.
japhyr将近 5 年前
I rarely write my own class and then use that as a parent of a new class. But I very frequently write a class that inherits from a parent class when I use a framework.<p>When I teach inheritance to students, it’s not usually with the mindset that “You’ll use inheritance a lot in the original code you write.” The mindset I teach with is, “You’ll see inheritance often in the codebases you work with.”
eel将近 5 年前
Sometimes, but not frequently. I&#x27;ll write abstract classes and inherit from those occasionally.<p>The only times in recent memory where I have inherited a concrete class from another concrete class was to add functionality to an existing program without starting a massive refactor.
bzb3将近 5 年前
Very very rarely, and only when it is obvious. I could live without it, I&#x27;d say.
quickthrower2将近 5 年前
And the following question is do you honour LSP (Liskov substitution principle)<p>In other words can you freely swap out derived classes you didn’t know about when you wrote the base class.
AnastasiiaK将近 5 年前
To understand where and why to use inheritance, read about the design patterns first. But anyway it depends on your problem and language you use.
yen223将近 5 年前
Only when the framework I&#x27;m using demands it. The oft-cited wisdom of &quot;composition over inheritance&quot; rings true, in my experience.
评论 #23828604 未加载
dave_sid将近 5 年前
Yes. While submitting technical tests for backward job interviews. Other than that, nahhh not really. It’s a hangover from OOP 1.0
randtrain34将近 5 年前
Typescript and Golang interfaces extend each other a lot via inheritance, from my experience in various codebases.
maps7将近 5 年前
No I don&#x27;t. I can&#x27;t remember or think of an example where I used it.
codingslave将近 5 年前
Only if I am going to have like ten different children