| | |
Need refactoring opinions
![]() |
•
•
Join Date: Jan 2007
Posts: 3
Reputation:
Solved Threads: 0
Hi,
I have to refactor the method kingRange under, the goal being code readability :
Here is where I am so far :
My question about it is : do you think that some improvement can still be done using Fowler refactorings (see http://www.refactoring.com, in particular removing duplicate code, merging methods, ...) without sacrificing the code readability and understandability ?
I don't want a solution, just your opinion, and if you want some clues on how to improve, I want to find things by myself.
Thanks.
I have to refactor the method kingRange under, the goal being code readability :
java Syntax (Toggle Plain Text)
/* For information, here are the class fields needed here : private String setUp; public final static Collection validPieces = Arrays.asList(new String[] { "R", "N", "B", "K", "Q" }); public static final int LEFT = 1; public static final int RIGHT = 2; */ public Range kingRange() { Range result; if ( (setUp.indexOf("R",setUp.indexOf("R")+1) + 1) > (setUp.indexOf("R") + 1) ) { result = new Range((setUp.indexOf("R") + 1) + 1, (setUp.indexOf("R",setUp.indexOf("R")+1) + 1) - 1); } else { int firstEmptyPositionReadingLeftToRight = 0; boolean find = false; for (int i = 1; i < 8; i++) { if (!find && !validPieces.contains(setUp.substring(i-1,i))){ firstEmptyPositionReadingLeftToRight = i; find = true; } } int firstEmptyPositionReadingRightToLeft = 0; find = false; for (int i = 8; i > 1; i--) { if (!find && !validPieces.contains(setUp.substring(i-1,i))){ firstEmptyPositionReadingRightToLeft = i; find = true; } } int left = 0; int right = 0; if ((setUp.indexOf("R") + 1) == firstEmptyPositionReadingLeftToRight - 1) { left = firstEmptyPositionReadingLeftToRight; } else { left = firstEmptyPositionReadingLeftToRight + 1; } if ((setUp.indexOf("R") + 1) == firstEmptyPositionReadingRightToLeft + 1) { right = firstEmptyPositionReadingRightToLeft; } else { right = firstEmptyPositionReadingRightToLeft - 1; } result = new Range(left, right); } return result; }
Here is where I am so far :
java Syntax (Toggle Plain Text)
public Range kingRange() { if ( (setUp.indexOf("R",setUp.indexOf("R")+1) + 1) > (setUp.indexOf("R") + 1)) { return new Range((setUp.indexOf("R") + 1) + 1, (setUp.indexOf("R",setUp.indexOf("R")+1) + 1) - 1); } return new Range(rangeLeft(), rangeRight()); } private int rangeRight() { int firstEmptyPositionReadingRightToLeft = firstEmptyPositionReadingRightToLeft(); if ((setUp.indexOf("R") + 1) == firstEmptyPositionReadingRightToLeft + 1) { return firstEmptyPositionReadingRightToLeft; } return firstEmptyPositionReadingRightToLeft - 1; } private int rangeLeft() { int firstEmptyPositionReadingLeftToRight = firstEmptyPositionReadingLeftToRight(); if ((setUp.indexOf("R") + 1) == firstEmptyPositionReadingLeftToRight - 1) { return firstEmptyPositionReadingLeftToRight; } return firstEmptyPositionReadingLeftToRight + 1; } private int firstEmptyPositionReadingRightToLeft() { for (int i = 8; i > 1; i--) { if (!validPieces.contains(setUp.substring(i - 1, i))) { return i; } } return 0; } private int firstEmptyPositionReadingLeftToRight() { for (int i = 1; i < 8; i++) { if (!validPieces.contains(setUp.substring(i - 1, i))) { return i; } } return 0; }
My question about it is : do you think that some improvement can still be done using Fowler refactorings (see http://www.refactoring.com, in particular removing duplicate code, merging methods, ...) without sacrificing the code readability and understandability ?
I don't want a solution, just your opinion, and if you want some clues on how to improve, I want to find things by myself.
Thanks.
Last edited by mevtho; Feb 24th, 2008 at 9:51 pm. Reason: Try to make code more readable
I think you have done enough refactoring, also put some comments in the code so that it becomes more understandable.
Notice that the only difference in those pairs of methods is the direction of advance, +1 or -1. If you see that much duplication between methods, chances are you can make them one method. Consider how it might work if you defined and passed the direction to search as a parameter to a single method.
Java Syntax (Toggle Plain Text)
final static int LEFT = -1; final static int RIGHT = 1;
![]() |
Similar Threads
- Opinions? javascript/php/etc and programming standards (JavaScript / DHTML / AJAX)
Other Threads in the Java Forum
- Previous Thread: Split text and store it in arraylist [ problem with code]
- Next Thread: couldn't load main class
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking bidirectional binary binarytree birt bluetooth chat chatprogramusingobjects class client code columns component database derby design eclipse encryption error errors expand fractal game givemetehcodez graphics gui guidancer homework html ide if_statement image inheritance integer intellij interface j2me java javadesktopapplications javaprojects jlabel jme jni jpanel jtextfield julia linux list map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source problem program programming project property recursion reference ria scanner search server set sms sort sourcelabs splash sql sqlite static stop string support swing testautomation threads tree ui unicode validation windows






