- 浏览: 78662 次
- 性别:
- 来自: 杭州
最近访客 更多访客>>
文章分类
最新评论
-
ieniac:
516456267 写道离线版非常好使!
查看当前本机浏览器FlashPlayer版本 -
516456267:
离线版非常好使!
查看当前本机浏览器FlashPlayer版本
Never, never, never use (unwrapped) String or long or int. Why? Those primitive types have no semantic meaning. They are hard to understand, hard to maintain, and hard to extend. I’ve been evangelizing this concept for some time, the essay “Object calisthenics” finally prompted be to write this post. Suppose we have an example of a cinema ticket booking service.
Update: If you just want to drop a comment telling me how revolting you find the idea, well, just don’t. I appreciate your comment, but sit back, think some time about it and move on coding. When you read someone else code with String id
and you wonder what on earth the id
is, come back and read this post.
Compare
public void bookTicket( String name, String firstName, String film, int count, String cinema);
with (and I know one would introduce an Order
object for real code):
public void bookTicket( Name name, FirstName firstName, Film film, Count count, Cinema cinema);
The second one is much easier to understand, especially when your IDE is one that tells you during autocompletion of a method call bookTicket(String arg0, String arg1, String arg2, int arg3, String arg4)
versus bookTicket(Name arg0, FirstName arg1, Film arg2, Count arg3, Cinema arg4)
. The second one is also much easier to read.
Compare
void book(String orderId);
with
void book(OrderId order);
In the first case the developer seeing the code wonders a.) where to get an orderId and b.) what an orderId really is, "1212"
, "ABC-123"
or "12-34-45-SCHWEINEBACKE"
. In the second case he can search for the usage of the OrderId
class, how it is used, read the javadoc and only pass validated and correct order ids into an application. You might think an orderId is just an orderId, easy to find. Legacy systems will change the id, the naming and semantics in often inconsistent ways. I’ve seen systems that name an order ID in serveral ways as “orderId”, “AuftragsId”, “id” and several other names and all meaning the same thing!
It’s easier to have a class with semantics than a domain-less String. Developers cannot as easily mess up. If you rely on static typing and use a startic type language (both object and reference) then maximize your benefits and create more classes. In the future an OrderId
class can also easily be changed to hold long instead of an int, hold validation or id generation logic. It’s much harder to extend the initial String
based version.
Implementation with a fluent interface
The classes should be implemented as simple Domain classes, sometimes as immutable value objects, which just wrap String
and attach some semantic meaning to the String
.
public class Name { public Name(String name) { ... } public static Name name(String name) { return new Name(name); } }
One would wonder if the solution is too noisy. Assuming
new Customer(new FirstName("Stephan"), new Name("Schmidt"));
is certainly noiser than a String argument:
new Customer("Stephan", "Schmidt");
The first is easier to understand though, and with static methods can be changed to
new Customer(firstName("Stephan"), name("Schmidt"));
This also solves the problems that with many arguments developers from reading the code don’t understand what each parameter means, especially in longer (refactor to parameter object!) parameter lists. This is another approach to Fluent Interface builders.
My last post on how to use generics with immutable objects could also be extended with value objects instead of primitives.
new Point(10,10);
new Point(x(10), y(10));
where x(10)
and y(10)
create Xpos and Ypos value objects.
Domain objects vs. primitivs in interview questions
One of the interview questions I like is to ask people about an interface for a price search. I usually give them
... searchByPrice(...)
and let candidates fill in the missing parts. Some will write
Vector searchByPrice(double start, double end)
which is bad code from several points of view (using double for money, no domain objects, untyped Vector).
Others with more domain based thinking write
List<Product> searchByPriceRange(Price start, Price end)
or even us the Range pattern by Fowler:
List<Product> searchByPriceRange(PriceRange priceToSearch)
The last solution is easy to extend and understand. Answering this question often starts an interesting discussion on interface design, maintainablity and domain modelling. Whatever you think about this interview question, don’t forget to once and for all: Do not use double for money.
Thanks for listening and don’t use String, int and long (or double for money).
Update:
If you find the usage of Classes instead of Strings repulsive, look at another example, zip code. Most people I’ve seen in lots of code use a primitive for zip code which creates a lot of problems when going i18n.
Customer { String name; String street; String city; String zip; }
(some will have used int
for the zip code but get faster into trouble than the String
users)
instead of
Customer { String name; Address address; } Address { ZipCode code; }
Still think Strings are a good idea in your code or “the simplest thing that could possibly work”?
o-Link : http://codemonkeyism.com/never-never-never-use-string-in-java-or-at-least-less-often/
发表评论
-
Sublime Text 2 Custom Layout
2012-10-15 15:21 7011. Edit Sublime Text 2\Data ... -
Linux Tips
2012-10-15 06:43 601-- How to config ip in Linux F ... -
Change FireBug DEFAULT font-family
2011-12-31 16:44 7681. Search *firebug*.xpi in Fire ... -
cygwin for e-texteditor
2011-08-17 11:03 668install cygwin for ete Se ... -
Config Remark
2011-06-19 15:06 8851. version control 2. out o ... -
IE associate Fix
2010-08-11 09:33 644注册动态链接库 for %%i in (c:\windows ... -
qq.foxmail.crack
2009-10-20 17:31 934javascript:var s=/mail.qq.com/; ... -
Editing/Authoring Actionscript3 Classes In IntelliJ IDEA
2009-08-25 17:54 2059I wanted to see whether it was ... -
Flex & RIA
2009-08-22 23:28 798Framework: a. Cairngorm [MVC] ... -
Tell, Don't Ask
2009-08-22 19:41 2436Alec Sharp, in the recent boo ... -
Extract flash from Excel or Word
2009-06-05 14:34 854打开含有Flash的EXCEL文档, 点击"视图& ... -
Google的九条创新原则
2009-06-04 11:57 738Innovation, not instant perfec ... -
Ten Quotients of Success
2009-05-29 10:50 1011成功是每一个人都梦想 ... -
How to Rate a Software Developer
2009-05-29 10:42 968How do you rate a software deve ...
相关推荐
- 如:What does your father do in the morning? He usually does morning exercises at 6 a.m. 5. "with" 的用法: - "with" 后接人称宾格表示“和...一起”。 - 例如:I often play football with my friends...
- 使用"I usually"或"I often"描述经常性的动作,如"I usually finish class at 2:30."(我通常在两点半下课) - 可以用"Sometimes"添加偶尔的行为,如"I sometimes go to the park with my sister."(我有时和...
本单元主要围绕频率副词“多久一次(How often)”展开,涉及大量的词汇、短语及其辨析,旨在提升学生对日常活动频率的表达能力。 一、核心词汇与短语 1. 序数词与次数表达: - 第一:first - 一次:once = one ...
- always=all the time, usually, often, sometimes=at times, hardly ever, never。 - 注意区分sometimes、sometime和some times的不同用法。 - hardly作为副词表示“几乎不”。 - how often提问动作频率: -...
- almost可以用于none no never nobody nothing等否定词之前,但nearly不可以。 6. 动词短语: - take place:发生(计划,必然),没有被动语态,强调事情有计划有安排地发生 - happen:发生(偶然),没有被动...
- 从不:never - 正餐:meal - 成员:member - 空闲的:free - 运动员:athlete - 梦想:dream - 画画:draw - 周末:weekend - 偶像:idol - 现代的:modern - 生物:biology - 地理:geography - ...
- 保持身体健康:keep in good health - 去购物:go shopping - 和...相同:the same as - 当然:of course 3. **翻译句子** - 周末你/他们经常做什么?:What do you/they usually do on weekends? - 你最...
- more/less, most/least 的用法来表示比较级和最高级。 4. 不规则动词的变化形式: - 如:be -> am/is/are -> was/were。 5. 代词和冠词的使用: - 特指和泛指的冠词 a/an, the 的用法。 - 人称代词、物主...
- 首先/最初:at first 2. **情感与情绪词汇**: - 射击/发射:shoot - 石头:stone - 虚弱的/无力的:weak - 提醒/使想起:remind - 愚蠢的/不明事理的:foolish - 物体/物品:object - 隐藏/隐蔽:hide ...
- 在公交车站:at the bus stop - 必须停下来:must stop - 跑得快:run fast - 去看医生:go to see a doctor - 班级规则:class rules - 玩球类游戏:play ball games - 大声地谈论:talk loudly - 在课堂...
Analytics-Journal-Never-2021-10-15-080348.ips.ca.synced
1. 频率副词的用法:频率副词如"never, hardly, seldom, sometimes, often, usually, always"等用来描述事件发生的频次。例如,"hardly ever"表示几乎从不,而"always"则表示总是。在填空练习中,我们看到例子:...
He often does homework until late at night and doesn't go to bed early. - 他有时在早上感觉困。He sometimes feels tired in the morning. - 王兵很了解刘涛。Wang Bing knows Liu Tao very well. 这些内容...
扩展:这些频度副词通常位于助动词、be动词或情态动词之后,行为动词之前,如"I usually eat breakfast at 7am."(我通常早上7点吃早餐)。 通过以上内容的学习,学生们将能更好地理解和运用与健康生活方式和英语...
- 反转句式:如"Never has a city been so completely destroyed before in history."(历史上从未有一个城市被如此彻底地摧毁过。) - 让步状语从句:如"Amazing as it may seem…"(虽然这看起来很令人惊奇…) ...
- 频度副词的用法:never, always, usually, often 和 sometimes 分别表示频率,通常置于行为动词前,be动词之后。sometimes 可以放在句首或句尾。 7. **巩固提高**: - 单项选择题测试了学生对频度副词的使用...
ApophisAndroid: Never-Die-Zombieroid :shooting_star: :female_sign::female_sign: SOPT 27日17日APP-JAM:Apophis :shooting_star: :pushpin: 部分会议 每天晚上7:30 :artist_palette: 看板 :wrench: 工具...
- 常见频度副词及其频率:always(100%)> usually(80%)> often(60%)> sometimes(20%~40%)> never(0%) - 使用位置:频度副词一般放在实义动词之前,情态动词或助动词之后。 6. **其他词汇**: - 疑问词...
- 了解并区分in、on、at、for在表示时间时的不同用法。 #### 二、关键词汇与句型 - **关键词汇**: - daily activities: 日常活动 - get up: 起床 - brush teeth: 刷牙 - take a shower: 洗澡 - get dressed...
- at least:至少 - on time:按时 - in time:及时 - from time to time:有时 - for the first time:首次 - a bit/a little:稍微,作状语时可互换,修饰名词时需加上of。 4. 不定代词: - someone/...