Skip to Content.
Sympa Menu

patterns-discussion - Re: [patterns-discussion] Design pattern recommendation

patterns-discussion AT lists.cs.illinois.edu

Subject: General talk about software patterns

List archive

Re: [patterns-discussion] Design pattern recommendation


Chronological Thread 
  • From: Jesús Alonso <kenchoweb AT hotmail.com>
  • To: <patterns-discussion AT cs.uiuc.edu>
  • Subject: Re: [patterns-discussion] Design pattern recommendation
  • Date: Thu, 15 Nov 2007 04:21:18 +0100
  • Importance: Normal
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/patterns-discussion>
  • List-id: General talk about software patterns <patterns-discussion.cs.uiuc.edu>

Hehe, that looks like Lisp!

I can't think of a good pattern that would be applicable here at this moment. However, I would suggest to do some little steps in order to enhance the code.

First, I would try to break that single condition statement into nested conditions. This is as bad as having such a long statement, but has the benefit that, if you group the nested conditional statements by function or source class, you might be able to identify higher-level conditions that might be refactored. For instance, consider this simple statement:
if (!file.exists() || (file.exists() && !file.hasWritePermission()))
  return false;
can be re-structured as:
if (!file.exists())
  return false;
else
  if (!file.hasWritePermission())
    return false;
There it's easier to notice that you might move that to another function, name it file.isWrittable()

Of course, the first thing to consider when one sees such long statements is to break them into smaller ones and keep small intermediate values to keep things clearer. Those smaller statements are often candidates to be refactored into different methods. In your case, this might be an approach:
bool temp1, temp2, temp3;
temp1 = cCall.hasLockingCondition() || getUtility().usesConfig() ;
temp2 = getUtility().externalWriteUtility() || (!call.hasParameters());
temp3 = call.isNothingReturned() && (!call.shouldBuildObject()) && (!call.isWriteRequired());
if (temp1 && temp2 && temp3)
  ...
I can't help with the names as I don't really know the meaning and context of the methods, and of course some Boole's algebra would help a lot re-structuring that, but it's really late here and I don't feel like thinking that.

In any case, hope this helps you figure a solution out. Good luck!

Regards,
  Jesús


Date: Wed, 14 Nov 2007 18:36:07 -0800
From: win_ash12 AT yahoo.com
To: patterns-discussion AT cs.uiuc.edu
Subject: [patterns-discussion] Design pattern recommendation

Hello to all the four who replied (I guess a Gang of Four did reply to my message!!)
Thanks for all the suggestions. I will certainly look at all of them closely. Though it looks I may need the State pattern. I will also buy that book that was recommended.

In any case here is the code thats hard to read

Basically thats the code that I am trying to refactor ( I have relabeled the methods )

if (call.isNothingReturned() && (cCall.hasLockingCondition() || getUtility().usesConfig() )
                && (!call.shouldBuildObject()) && (getUtility().externalWriteUtility() || (!call.hasParameters())) && (!call.isWriteRequired()))


Get easy, one-click access to your favorites. Make Yahoo! your homepage.


Express yourself instantly with MSN Messenger! MSN Messenger


Archive powered by MHonArc 2.6.16.

Top of Page