One of the Best Bits of Programming Advice I ever Got

本文探讨了在面向对象编程中,避免使用以'Er'结尾的对象名称的重要性,解释了这种命名习惯如何影响代码的封装性、减少 spaghetti 代码并促进更清晰的面向对象设计。通过实例分析了诸如 Manager、Controller 和 Organizer 等常见错误命名,并提出了改进建议。

from: http://objology.blogspot.com/2011/09/one-of-best-bits-of-programming-advice.html

 

Years ago (early 1992), I attached myself to this crazy skunkworks project that was using this weird language called Smalltalk. "Object Oriented" was in its infancy as a "hot" item. High paid consultants. Lots of people laying claim to what this new object religion was all about. This was 5 years before Alan Kay would make the statement "I invented the term 'Object Oriented Programming' and this {Java and C++ } is not what I had in mind."

Shortly after hooking up with this whacky group with the whacky language, still confused about what the difference was between an instance variable, a class variable, and a class instance variable, I found myself in a training course taught by Russ Pencin, of ParcPlace. Russ would say something that I didn't really appreciate at the time. Despite not understanding the point behind this sage advice, I endeavored to follow it. It would take years of experience and exposure to appreciate it's value. The advice?

Don't make objects that end with 'er'.


That's it. The OOP paradigm sprang to life amidst of a culture of what we called "procedural programming." Now days we don't talk so much about the comparison between the two paradigms. Probably in part because Object Oriented languages are now a dime a dozen. The OOP religion, in a multitude of flavors won out. Sadly, I often find myself echoing words I heard Adele Goldberg say around 2000: "Now days we have lots of Object Oriented Programming, but not so many Object Oriented Programmers". If there was one piece of advice I would pass on to the hordes of would be Object Oriented Programmers, it would be the sage advice offered by Russ: "Don't make objects that end with 'er'."

What's in a name anyway? Why is this worth getting excited about? What I've discovered over the years, is that the jst of OOP is that we bind behavior to data . As long as you haven't joined in the Functional Monks in their Monasteries of Statelessness, programs are made of behavior and data. In classic structured/programming, we concentrate on behavior (verbs), and then figure out what data (nouns) we need to make it all work. In other words, we bind data to behavior. But in OOP, we make the focus of programs be the nouns, the data, and then we figure out what kind of behavior we can bind to them, and hope that the problems we hope to solve get out of the emergent behaviors.

I recently posited to a colleague that in nearly every "er" object case, there was a better name for it. And that giving it a better name would tend to make the design more encapsulated, less spaghetti code, in short more object oriented. It's not a hard and fast rule, but there are a lot of cases where it can improve things.

Take some sort of "Loader" for example. The focus here is on the unit of work it does. It'll have lots of instance variables, lots of arguments, and pass lots of data around probably. Now instead replace that with a LoadRecord and a LoadStream. I'm reasonably confident you'll end up with something that is more akin to what the original Founding Fathers of OOP had in mind. We want to create objects that describe what they are, and then bind behavior to them, rather than focus on what they do, and then figure out what data they'll need to do that.

Some er's that I've learned to avoid over the years:

  • Managers - Every time I see one of these, I cringe. People will usually tell me what it does, long before they can tell me what it is. Is it a registry? Fine call it a registry. Is it a history or a log? Call it that. Is it a factory? Call it that.
  • Controllers - Only good controller object I've made in the last 20 years was an interface to a BallastVoltageController that represented a real world object. The fact that every single MVC implementation in the world has had a different role for Controller ought to tell us something about how well that idea fit.
  • Organizer (and many like them) - Focus is on what it does. This is a great example of how easy it is to turn many of these 'ers' into nouns. Call it an Organization. Now we're focusing on what it is.
  • Analyzer/Renderer/etc - Definitely examples of "worker" objects. What if they had been Analysis/Rendering/etc.
  • Builder/Loader/Reader/Writer/etc - Remove the focus from the objects being manipulated, and tend assume to much responsibility themselves.
There's lots of exceptions to such a rule of course.
  • There are lots of noun words that end in 'er'. Register. Border. Character. Number. If it's really a noun, fine.
  • There are many 'er' words that despite their focus on what they do, have become so commonplace, that we're best to just stick with them, at least in part. Parser. Compiler. Browser.
  • When you are trying to model a domain object that ends in 'er'. I'm fine with a Manager subclass of Personel, which is there to refine a type of personal that has management behavior to it.
Your mileage may vary, I'm sure there are those that disagree with this. Until you apply the mindset for a while though, you'll never really know. Give it a whirl on one of your projects/designs and see what happens.


本系统采用Python编程语言中的Flask框架作为基础架构,实现了一个面向二手商品交易的网络平台。该平台具备完整的前端展示与后端管理功能,适合用作学术研究、课程作业或个人技术能力训练的实际案例。Flask作为一种简洁高效的Web开发框架,能够以模块化方式支持网站功能的快速搭建。在本系统中,Flask承担了核心服务端的角色,主要完成请求响应处理、数据运算及业务流程控制等任务。 开发工具选用PyCharm集成环境。这款由JetBrains推出的Python专用编辑器集成了智能代码提示、错误检测、程序调试与自动化测试等多种辅助功能,显著提升了软件编写与维护的效率。通过该环境,开发者可便捷地进行项目组织与问题排查。 数据存储部分采用MySQL关系型数据库管理系统,用于保存会员资料、产品信息及订单历史等内容。MySQL具备良好的稳定性和处理性能,常被各类网络服务所采用。在Flask体系内,一般会配合SQLAlchemy这一对象关系映射工具使用,使得开发者能够通过Python类对象直接管理数据实体,避免手动编写结构化查询语句。 缓存服务由Redis内存数据库提供支持。Redis是一种支持持久化存储的开放源代码内存键值存储系统,可作为高速缓存、临时数据库或消息代理使用。在本系统中,Redis可能用于暂存高频访问的商品内容、用户登录状态等动态信息,从而加快数据获取速度,降低主数据库的查询负载。 项目归档文件“Python_Flask_ershou-master”预计包含以下关键组成部分: 1. 应用主程序(app.py):包含Flask应用初始化代码及请求路径映射规则。 2. 数据模型定义(models.py):通过SQLAlchemy声明与数据库表对应的类结构。 3. 视图控制器(views.py):包含处理各类网络请求并生成回复的业务函数,涵盖账户管理、商品展示、订单处理等操作。 4. 页面模板目录(templates):存储用于动态生成网页的HTML模板文件。 5. 静态资源目录(static):存放层叠样式表、客户端脚本及图像等固定资源。 6. 依赖清单(requirements.txt):记录项目运行所需的所有第三方Python库及其版本号,便于环境重建。 7. 参数配置(config.py):集中设置数据库连接参数、缓存服务器地址等运行配置。 此外,项目还可能包含自动化测试用例、数据库结构迁移工具以及运行部署相关文档。通过构建此系统,开发者能够系统掌握Flask框架的实际运用,理解用户身份验证、访问控制、数据持久化、界面动态生成等网络应用关键技术,同时熟悉MySQL数据库运维与Redis缓存机制的应用方法。对于入门阶段的学习者而言,该系统可作为综合性的实践训练载体,有效促进Python网络编程技能的提升。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
在当代储能装置监控技术领域,精确测定锂离子电池的电荷存量(即荷电状态,SOC)是一项关键任务,它直接关系到电池运行的安全性、耐久性及整体效能。随着电动车辆产业的迅速扩张,业界对锂离子电池SOC测算的精确度与稳定性提出了更为严格的标准。为此,构建一套能够在多样化运行场景及温度条件下实现高精度SOC测算的技术方案具有显著的实际意义。 本文介绍一种结合Transformer架构与容积卡尔曼滤波(CKF)的混合式SOC测算系统。Transformer架构最初在语言处理领域获得突破性进展,其特有的注意力机制能够有效捕捉时间序列数据中的长期关联特征。在本应用中,该架构用于分析电池工作过程中采集的电压、电流与温度等时序数据,从而识别电池在不同放电区间的动态行为规律。 容积卡尔曼滤波作为一种适用于非线性系统的状态估计算法,在本系统中负责对Transformer提取的特征数据进行递归融合与实时推算,以持续更新电池的SOC值。该方法增强了系统在测量噪声干扰下的稳定性,确保了测算结果在不同环境条件下的可靠性。 本系统在多种标准驾驶循环(如BJDST、DST、FUDS、US06)及不同环境温度(0°C、25°C、45°C)下进行了验证测试,这些条件涵盖了电动车辆在实际使用中可能遇到的主要工况与气候范围。实验表明,该系统在低温、常温及高温环境中,面对差异化的负载变化,均能保持较高的测算准确性。 随附文档中提供了该系统的补充说明、实验数据及技术细节,核心代码与模型文件亦包含于对应目录中,可供进一步研究或工程部署使用。该融合架构不仅在方法层面具有创新性,同时展现了良好的工程适用性与测算精度,对推进电池管理技术的进步具有积极意义。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
代码转载自:https://pan.quark.cn/s/9e296fe8986c 实验题目为“复杂模型机的设计与实现”。 _1. 实验目的与要求:目的:1. 熟练掌握并达成较为复杂的计算机原理。 2. 本实验增加了16条机器指令,全面运用所学的计算机原理知识,借助扩展的机器指令设计并编写程序,然后在CPU中执行所编写的程序。 要求:依照练习一和练习二的要求完成相应的操作,并上机进行调试和运行。 2. 实验方案:……实验报告的标题设定为“广东工业大学计组实验报告复杂模型机的设计与实现六”,主要围绕计算机组成原理中的复杂模型机设计和实现展开。 实验的宗旨在于让学生深入理解和实际操作计算机原理,特别是通过增加16条机器指令,来全面运用所学知识设计程序,并在CPU中运行这些程序。 实验的具体要求包括:1. 掌握复杂的计算机工作原理,这要求学生不仅具备扎实的理论知识,还需要拥有将理论转化为实际操作的能力。 2. 实验中增加了16条机器指令,这涉及到计算机指令集的扩展和设计,可能包含算术运算、逻辑运算、数据传输和控制流程等指令。 3. 学生需要运用扩展的机器指令编写程序,并通过CPU进行运行和调试,这涉及到编程、汇编和CPU执行流程的理解。 4. 依照练习一和练习二的要求完成操作,这表明实验包含分阶段的练习任务,需要逐步完成并验证。 实验方案包括:1. 实验连线:保证硬件连接准确无误,这是任何电子实验的基础,对于计算机实验,这通常涵盖CPU、内存、输入/输出设备等组件的连接。 2. 实验程序:提供了范例程序,包括机器指令程序和微指令程序的微代码。 这部分内容展示了如何利用扩展的机器指令编写程序,以及对应的微指令实现,有助于理解计算机内部的低级操作。 在实验结果和数据处理部分,学生需要:1. 在程...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值