Why I Don’t Use Classes

栏目: IT技术 · 发布时间: 4年前

内容简介:Recently, one of my teammates noted that our project’s codebase doesn’t have a lot of classes. The observation wasn’t framed positively or negatively. It was just an observation about a particular code style of the team as a whole.I didn’t think much about

Recently, one of my teammates noted that our project’s codebase doesn’t have a lot of classes. The observation wasn’t framed positively or negatively. It was just an observation about a particular code style of the team as a whole.

I didn’t think much about it at the time. But looking back, this observation has lead to some good self-reflection on what I value when writing software. I didn’t write this post to persuade others to follow patterns that I tend to follow. But I’m hoping this post is a catalyst for your own self-reflection.

For context, my projects lately have been in full-stack Typescript, and what I value in writing software is highly influenced by the experiences in the Typescript ecosystem. I’m guessing there are cross-cutting concerns here that are applicable in other ecosystems, but I’m not going to claim that’s the case.

I also should note that I don’t actively avoid writing classes. I just lean toward other solutions, typically functional solutions over object-orientated solutions. Since I spend the majority of my time in the Typescript ecosystem, I have the luxury of choosing either paradigm to solve the current problem at hand.

Abstractions

I’m terrible at making good abstractions with classes. I’ll admit that the results have been mixed. But typically, using inheritance and polymorphism to model a problem space evolves into a mess. I think I know at least two sources of the mess.

Terrible Method Names

First, classes end up having terrible method names if those methods are overriding base class methods. I’ve been fooled by too many method names that end up doing more than what they said they would. A base class will often set the name for a method based on what it should do . But as other classes extend and override those methods to address their version of the problem space, they usually end up having some other responsibilities in addition to the original task.

For example, I’ve implemented classes that manage database tables, so it implements an abstract class that needs an “update” method. Well, some tables when updating need to do more than just “update” a record, such as insert a log into a side table. While that might be innocent enough at first, I’ve seen quite a few methods continue to grow in their sets of responsibilities.

Maybe I just have organization problems. But I’ve found much more success creating groups of functions that don’t belong in a class. And I can give them any name I want and split responsibilities into new functions as I please.

Classes Grow

Secondly, I’ve noticed that classes have a tendency to grow large. They will collect pieces of functionality that need to live in the context of the class, usually to access the internal state of that class. This dependency on the internal state makes it difficult to break the methods up into logical chunks.

This usually isn’t a huge pain point, but teams I’ve been on have preferred using groups of functions in modules that have state passed to them. It tends to be easier to break large modules into smaller ones if needed.

State Management

I’m not great at managing state in classes. State contained and controlled by a class has been the cause of interesting and undesirably behaviors. While using class-managed state is likely well suited for a suite of problems, I’ve seen a pattern of buggy implementations coming out of class-based solutions that held their own state.

One concrete example of this pattern is class-based React components. Time and time again, whenever I would try to use a class-based component, that component would eventually be the source of a bug if it tried to manage some internal state.

I thought that maybe state management was just inherently complex, and maybe it wasn’t the classes to blame. However, now that I’ve been using React’s hooks to manage state for functional components, I haven’t come across this sort of bugs in my UI code. It seems like removing the class construct and replacing it with functions that can be passed around between components have simplified state management on my projects.

Alternatives to Classes

As I’ve mentioned above, I like to use modules that expose groups of functions. These functions accept state and other dependencies. These modules tend to look like what my colleague Drew describes as the functional module pattern .

Maybe functions are more natural than classes in the Typescript ecosystem. There’s a chance that I would admire classes a bit more if I did more .NET development. Maybe I would benefit if I tried to use the object-orientated paradigm rather than the functional paradigm. I just haven’t found a compelling reason to do that on my project as of late.


以上所述就是小编给大家介绍的《Why I Don’t Use Classes》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

赢在设计

赢在设计

[美] 洛芙迪 (Lance Loveday)、[美] 尼豪斯 (Sandra Niehaus) / 刘淼、枊靖、王卓昊 / 人民邮电出版社 / 2010-8 / 55.00

企业总是面临在网站设计和改进方面进行投资的抉择。怎样才能让有限的资金发挥出最大的效益呢?网站设计不应只是把网站做得赏心悦目,它更应该是提高经济收益和获得竞争优势的战略利器。是时候让网站发挥其潜能,以业务指标为导向来做设计决策,为提升网站收益而设计了。 作者凭借多年为众多网站做咨询工作的经验,为我们揭示了赢在设计的奥秘。它针对目前网站设计中存在的典型问题,先从宏观上探讨解决问题的战略手段,围绕......一起来看看 《赢在设计》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具