惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

Coding | 酷 壳 - CoolShell

API设计原则 – Qt官网的设计实践总结 | 酷 壳 - CoolShell Linus:利用二级指针删除单向链表 | 酷 壳 - CoolShell 如此理解面向对象编程 | 酷 壳 - CoolShell 代码执行的效率 | 酷 壳 - CoolShell 千万不要把 bool 设计成函数参数 | 酷 壳 - CoolShell 重构代码的7个阶段 | 酷 壳 - CoolShell 一个空格引发的惨剧 | 酷 壳 - CoolShell 如何写出无法维护的代码 | 酷 壳 - CoolShell Amazon的书为什么卖到了$2000万 | 酷 壳 - CoolShell Linux 2.6.39-rc3的一个插曲 | 酷 壳 - CoolShell 一些软件设计的原则 | 酷 壳 - CoolShell 打印质数的各种算法 | 酷 壳 - CoolShell 编程时间分配图 | 酷 壳 - CoolShell 代码优化概要 | 酷 壳 - CoolShell
代码重构的一个示例 | 酷 壳 - CoolShell
陈皓 · 2010-09-25 · via Coding | 酷 壳 - CoolShell

还记得以前和大家提到过的《各种流行的编程风格》吗?有一些人问我那些编程风格具体是什么样子的。下面是一个代码重构的实例,让我们看看那个流行的编程风格是实践是什么样的。下面的这个实践不是虚构,如有雷同,请对号入座。

首先,我们有一个表达式如下所示:

s = 7;

很明显,这个表达式的变量名太没意义了,很不利于程序的可读性,所以,我们需要取一个有意义的变量名:

slots = 7;

很好,不过,那个常量7是hard-code或是一个Magic number,而且,这常量没有名字也不利于代码的可读性啊。再改:

SEVEN = 7;
...
slots = SEVEN;

靠!上面,是这是哪门子的改法?(不过,我保证这是真实发生的),常量名也要有意义一点嘛,再改:

SLOTS_PER_WIDGET = 7;
...
slots = SLOTS_PER_WIDGET;

这还差不多,不过,名字可能会重名啊,最好放到一个类中:

import widgetConstants;
...
slots = widgetConstants.SLOTS_PER_WIDGET;

现在看起来好很多了,不过,即然面向对象了,我们当然要学会使用Design Pattern,比如Factory啊,或是Singleton啊什么的:

widgetModelFactory = WidgetModelFactory.getInstance();
widgetModel = widgetModelFactory.getWidgetModel() ;
slots = widgetModel.getSlotsPerWidget();

当然,要是考虑到整体的类结构,上面的那些还不够,下面是我们最终的重构代码:(欢迎来到真实的Java世界)

context = Context.getCurrentContext();
serviceDirectoryFactory = ServiceDirectoryFactory.getServiceDirectory(context);
serviceDirectory = serviceDirectoryFactory.getServiceDirectory(context);
serviceDescriptor = ServiceDescriptorFactory.getDescriptor("widgetModelFactory");
widgetModelFactoryServiceLocator = serviceDirectory.getServiceLocator(serviceDescriptor,context);
widgetModelFactory = (WidgetModelFactory)widgetModelFactoryServiceLocator.findService(context);
widgetModel = widgetModelFactory.getWidgetModel(context);

slots = widgetModel.getSlotsPerWidget();

这就是我们的面像对象的编程模式,记得N年前在面试那家著名的以鼓吹敏捷方法论的公司时,在用程序实现一个程序题的时候,他们对我的程序很不屑一顾,原因有两个,其一、我没有使用TDD写UT Case,其二、我的程序里没有设计模式。(我才知道,编程原来是为了测试和设计模式,而不是为了原来的需求),今天,仅以此文献给钟爱于那些流行编码风格的程序员们。

其实,这段代码也是如下而已罢了。

slots = thisWidget.getSlotCount();

(全文完)

Loading...