代码异味--作者:Martin Fowler
2013-12-20 18:07
141 查看
A code smell is a surface indication that usually corresponds to a deeper problem in the system. The term was first coined by Kent Beck while helping me with my Refactoring book.
The quick definition above contains a couple of subtle points. Firstly a smell is by definition something that's quick to spot - or sniffable as
I've recently put it. A long method is a good example of this - just looking at the code and my nose twitches if I see more than a dozen lines of java.
The second is that smells don't always indicate a problem.Some long methods are just fine.
You have to look deeper to see if there is an underlying problem there - smells aren't inherently bad on their own - they are often an indicator of a problem rather than the problem themselves.
The best smells are something that's easy to spot and most of time lead you to really interesting problems.Data classes (classes with all data and no behavior) are good examples of this. You look at them and ask yourself what behavior should
be in this class. Then you start refactoring to move that behavior in there.Often simple questions and initial refactorings can be the vital step in turning anemic objects into something that really has class.
One of the nice things about smells is that it's easy for inexperienced people to spot them, even if they don't know enough to evaluate if there's a real problem or to correct them.I've heard of lead developers who will
pick a "smell of the week" and ask people to look for the smell and bring it up with the senior members of the team. Doing it one smell at a time is a good way of gradually teaching people on the team to be better programmers.
The quick definition above contains a couple of subtle points. Firstly a smell is by definition something that's quick to spot - or sniffable as
I've recently put it. A long method is a good example of this - just looking at the code and my nose twitches if I see more than a dozen lines of java.
The second is that smells don't always indicate a problem.Some long methods are just fine.
You have to look deeper to see if there is an underlying problem there - smells aren't inherently bad on their own - they are often an indicator of a problem rather than the problem themselves.
The best smells are something that's easy to spot and most of time lead you to really interesting problems.Data classes (classes with all data and no behavior) are good examples of this. You look at them and ask yourself what behavior should
be in this class. Then you start refactoring to move that behavior in there.Often simple questions and initial refactorings can be the vital step in turning anemic objects into something that really has class.
One of the nice things about smells is that it's easy for inexperienced people to spot them, even if they don't know enough to evaluate if there's a real problem or to correct them.I've heard of lead developers who will
pick a "smell of the week" and ask people to look for the smell and bring it up with the senior members of the team. Doing it one smell at a time is a good way of gradually teaching people on the team to be better programmers.
相关文章推荐
- 聊聊代码优化——感谢马丁花(Martin Fowler),感谢N多书的作者!
- James Grenning访谈录:关于测试驱动开发及代码异味
- 22种代码味道(Martin Fowler与Kent Beck)
- linux下 USB动态监测 hotplug事件监测 开源给网友,使用及修改请给作者留一份新的代码,互相提高
- idea 代码作者注释的修改地方
- 去除代码异味
- 代码异味---Code smell
- C#操作消息队列的代码开发者在线 Builder.com.cn 更新时间:2008-07-20作者: 来源:
- asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式 作者:Jerry
- 在Windows使用git工具将代码同步至github(作者:ying1989920)
- 清除代码异味
- 搜索时候发现一处网站,里面有很多作者的au3代码
- 设计模式是代码的异味
- 杰奇自定义首页的官方作者代码
- [查异常网]-20160401-清除代码异味
- 访谈 | 专访《AngularJS 2权威教程》作者Nate Murray:相比文档,代码更像图表
- 通过 ASP.NET 2.0 的数据增强功能来提高负载和减少代码 --作者:Dino Esposito
- git修改提交代码时记录的作者信息
- 编写Admired主题的作者太有爱了,居然在代码里吐槽
- WordPress文章作者的相关函数调用代码