Treacle

Java Toxication!
posts(28) comments(15) trackbacks(0)
  • 语源科技BlogJava
  • 联系
  • RSS 2.0 Feed 聚合
  • 管理

公告

Betake Java
Gape after Ruby!

Contact

Mail me!

Search

Google
Technorati search

Technorati Profile Get Conversations about BlogJava - TreaclePing Technorati

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类(30)

  •  Eclipse(2)
  •  English(5)
  •  Java(6)
  •  Portal(1)
  •  ROR(4)
  •  Self(12)

随笔档案(28)

  • 2008年7月 (2)
  • 2005年9月 (3)
  • 2005年8月 (2)
  • 2005年7月 (2)
  • 2005年6月 (19)

文章分类(4)

  •  Java
  •  Others(3)
  •  ROR(1)

文章档案(2)

  • 2005年6月 (2)

相册

  • MySelf
  • 诗词

收藏夹(2)

  •  Java
  •  Net(1)
  •  Portal(1)
  •  UI

Friends

  •  Salomon's Java Castle

最新评论

  • 1. re: 一款好用的图形转换软件
  • 杂用啊?
  • --李力
  • 2. re: 许久未来了
  • 同感............
  • --久城
  • 3. re: 一款好用的图形转换软件
  • XZXZXZ
  • --213
  • 4. re: AjaxFaces 1.0 released
  • 网页打不开阿,博主能发给我吗,谢谢
  • --gembin
  • 5. re: Ruby概述
  • 惊讶!怎么回复表单中正确地显示了我的所有信息?
  • --靖郭郭

阅读排行榜

  • 1. 一款好用的图形转换软件(1906)
  • 2. 用GOOGLE你瞬间成为黑客(1846)
  • 3. 网络经典命令行(1517)
  • 4. Language Oriented Programming 面向语言编程(1421)
  • 5. Use Ruby send email(1337)

评论排行榜

  • 1. 一款好用的图形转换软件(5)
  • 2. 问dudu一个问题(3)
  • 3. Get total lines of files(2)
  • 4. 许久未来了(1)
  • 5. Ruby概述(1)

View Post

Code Cleaning

Whipping inherited code into shape

When I receive code, and before adding anything new to it, I always spend a little time (1 hour, 1 day) on "preparing" the code for the later modifications. This preparation is made in 2 steps:

  • code cleaning
  • code clarifying

Once I've done that, I understand the code/project better, and I can start adding tests, and functionalities.

Note: all the actions below are possible thanks to modern tools, and 100% safe refactorings => no need to write tests first.

Code cleaning : First, I run a quick and fully automated analysis of the code, with just a few rules:

  • remove dead code (classes, methods).
  • remove unused method parameters, variables
  • remove useless variable initialisation
  • remove useless Casting
  • tighten visibility (public => private)
  • move tests code to a separate code tree
  • modernize (Java1.4 => Java5)

Second: amongst the hundreds of inspections rules offered by IDEA, a few dozens will spot oddities and probable bugs in the code (like modifying the contents of a collection passed as parameter, etc..). In those "hot" locations, I add a "todo: check", or "todo: modernize" tag, for later review.

Code clarifying : I examine the code structure, starting with the 2-mile high view of the project, and going down. Along the way, I would perform :

  • rename : (package, class, method, parameter
  • move : package, classes, methods, ...
  • extract/inline method
  • introduce variable

Note: As this has to be fast, I only "cure" the worst problems. I tag the others for later reviews: "to optimize", "to betterize", "to fix", "danger", etc..


Some advice from Rod:

Help! API Overload

Today, there are many API and technology choices for most problems in J2EE.

Even Sun now seems to be at the point where pulling it all together is so complex that we're seeing significant duplication of effort. For example, JDO and EJB 2.0 entity beans with CMP seem to overlap significantly.

Ultimately, we all pay for duplication of effort in increased effort and decreased quality. At least we can do our utmost to control it within our organization. I believe that code reuse impossible, and we should do our best to achieve it.

There are many ways to avoid duplication of effort and leverage existing code. I suggest the following practices as a starting point:

  • Adopt existing frameworks where possible. For example, use a standard logging framework and an existing framework for web applications. However, don't force developers to use organization-wide standard frameworks if it seems that they're not proving a good fit to the problem in hand. Where multiple alternative frameworks exist, survey the options. Don't automatically assume that the first product you look at, or the most popular, will best meet your needs.

  • Have zero tolerance for code duplication. This indicates the need for generalization: try to avoid code duplication in the first place, but refactor it out of the way as soon as it appears.

  • Ensure good communication amongst developers. For example, have developers give presentations on modules they've recently completed, so that other developers know what common needs are emerging or have already been met. Encourage developers to encourage other developers to use the infrastructure components they've implemented.

  • Develop and maintain some simple infrastructure packages that implement functionality that's widely used. Document them well and ensure that all developers are aware of them.

  • Adopt standard architectural patterns, even where it's not possible to share code. It's much easier to avoid duplication of effort when working with familiar patterns.

  • Use code reviews. This not only helps to boost quality, but also spurs communication within a team.

Technorati Tags: Java Ping Technorati

posted on 2005-06-06 13:40 Blake HAN 阅读(431) 评论(0)  编辑  收藏 所属分类: Java

新用户注册  刷新评论列表  

只有注册用户登录后才能发表评论。


网站导航:
博客园  博客园最新博文  博问  管理
相关文章:
  • Language Oriented Programming 面向语言编程
  • AjaxFaces 1.0 released
  • Core Java Interview Questions
  • JDK5.0时代的"XDoclet" for Hibernate
  • Code Cleaning
 
 
Powered by:
语源科技BlogJava
Copyright © Blake HAN