Piotr Gabryanczyk’s Blog

Java, Refactoring, AOP, Spring, DDD, TDD, etc.

  • Blogroll

    • I have joined Anti-IF Campaign

Archive for the ‘refactoring’ Category

Anemic Domain Model – resources

Posted by Piotr Gabryanczyk on December 13, 2012

Fowler about the problem:
http://www.martinfowler.com/bliki/AnemicDomainModel.html

Hibernate:
http://wrschneider.blogspot.com/2005/01/avoiding-anemic-domain-models-with.html

Double Dispatch
http://stackoverflow.com/questions/227856/how-to-avoid-anemic-domain-models-and-maintain-separation-of-concerns
http://www.c2.com/cgi/wiki?DoubleDispatchExample

Advertisements

Posted in refactoring | 2 Comments »

Friday School of Refactoring – Extract Method – video

Posted by Piotr Gabryanczyk on December 6, 2006

People still don’t use refactoring!

Some time ago I realized how many developers still don’t use simple refactorings like Extract Method or Replace Temp with Query etc. Many of them don’t know how excellent the development tools are these days. Simple one-click/keystroke operation can make their code much prettier and simpler.
I even heared once the opinion that “tools can not be trusted” as they can break the code while refactoring.
I don’t think it is the case especially with IntelliJ and Eclipse. These editors are quite smart and if used well can almost write the code for you 🙂 I know, I know I went too far…

Friday school of refactoring

I decided that I have to do something to change it. I started posting a weekly refactoring guide to my team-mates. Every week I pick some example from our system and make it simpler, prettier, easier to maintain – I refactor it. You could see one of the examples in my post Refactoring – conditionals.

Video

But reading emails full of code seems to be a bit boring, so this week I made a movie.
It shows the following refactorings:

  1. Extract Method
  2. Inline Variable/Replace Temp with Query
  3. Rename Variable

Show it to your team mates!
We can change the world together 🙂

Disclaimer:

I know that movie has a bug – extract method takes one line at the top too much…
I am too lazy to fix it… and not too many people spotted it 🙂

Posted in Blogroll, java, refactoring, video | Leave a Comment »

Refactoring – conditionals

Posted by Piotr Gabryanczyk on November 17, 2006

I came across such a code:

 1  private boolean checkForSymmetricNulls(Object lhs, Object rhs) {
 2         return (lhs == null) ? (rhs == null) : (rhs != null);
 3     }
 4 
 5     private boolean isSymmetricallyNullOrEquals(Object lhs, Object rhs) {
 6         if (checkForSymmetricNulls(lhs, rhs)) {
 7             if (lhs != null) {
 8                 return lhs.equals(rhs);
 9             }
10             return true;
11         }
12         return false;
13     }

I was trying to figure out what this does for good 5 minutes and I realized that isSymmetricallyNullOrEquals and checkForSymmetricNulls could be replaced with one method:

 1  private boolean equal(Object lhs, Object rhs) {
 2         if (lhs == null || rhs == null) return lhs == rhs;
 3         return lhs.equals(rhs);
 4  }

I simplified the code slightly and also used the following refactoring:

http://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html

but the following seem to be relevant:

http://www.refactoring.com/catalog/decomposeConditional.html

http://www.refactoring.com/catalog/consolidateConditionalExpression.html

http://www.refactoring.com/catalog/consolidateDuplicateConditionalFragments.html

Enjoy!

Posted in java, refactoring | Leave a Comment »