Java: Refactoring / Code Optimization

Please enlighten me:

Which one do you prefer? What for? [Readability? Memory issues? Some other problems?]

1.

String strSomething1 = someObject.getSomeProperties1(); strSomething1 = doSomeValidation(strSomething1); String strSomething2 = someObject.getSomeProperties2(); strSomething2 = doSomeValidation(strSomething2); String strSomeResult = strSomething1 + strSomething2; someObject.setSomeProperties(strSomeResult); 

2.

 someObject.setSomeProperties(doSomeValidation(someObject.getSomeProperties1()) + doSomeValidation(someObject.getSomeProperties2())); 

If you did it differently, what would it be? Why would you do this?

+4
source share
8 answers

I would probably be in between:

 String strSomething1 = doSomeValidation(someObject.getSomeProperties1()); String strSomething2 = doSomeValidation(someObject.getSomeProperties2()); someObject.setSomeProperties(strSomething1 + strSomething2); 

Option number 2 seems to be very much in one line. It is readable, but requires a little effort for analysis. In option No. 1, each line is very readable and understandable in intentions, but verbosity slows me down when I turn to it. I would try to balance brevity and clarity as indicated above, with each line being a simple “sentence” of code.

+12
source

I would go with:

 String strSomething1 = someObject.getSomeProperties1(); String strSomething2 = someObject.getSomeProperties2(); // clean-up spaces strSomething1 = removeTrailingSpaces(strSomething1); strSomething2 = removeTrailingSpaces(strSomething2); someObject.setSomeProperties(strSomething1 + strSomething2); 

My personal preference is to organize an action, not a sequence. I think it just reads better.

+17
source

I prefer the second one. You can make it just as easy to read with a bit of formatting, without declaring additional intermediate links.

 someObject.setSomeProperties( doSomeValidation( someObject.getSomeProperties1() ) + doSomeValidation( someObject.getSomeProperties2() )); 

The names of your methods provide all the necessary explanations.

+6
source

Option 2 for readability . I see no memory problems here if methods only do what their names indicate. However, I would still be distinguished by concatenations. Performance definitely keeps up with the increasing concatenation sequence due to the immutability of Java strings.

It's just interesting to know if you really wrote your own removeTrailingSpaces () method or is this just an example?

+4
source

I am trying to perform one operation per line. The main reason is as follows:

 setX(getX().getY()+getA().getB()) 

If you have NPE, which method returns null? Therefore, I like to have intermediate results in some variable that I see after the code fell into the strong hands of the debugger and did not reboot!

+3
source

for me, it depends on the context and the surrounding code.

[EDIT: it makes no sense, sorry] if it were in a method like setSomeObjectProperties (), I would prefer option 2, but maybe create a private method "getProperty (String name)" that removes trailing spaces if removed spaces is not an important operation [/ EDIT]

If property validation is an important step of your method, I would call the method "setValidatedProperties ()" and would prefer a variant of your first sentence:

 validatedProp1 = doValidation(someObject.getSomeProperty1()); validatedProp2 = doValidation(someObject.getSomeProperty2()); someObject.setSomeProperties(validatedProp1, validatedProp2); 

If validation is not something important for this method (for example, it makes no sense to return properties that are not validated), I would put the validation step in "getSomePropertyX ()"

+2
source

Personally, I prefer the second. It is less cluttered and I do not need to keep track of these temporary variables.

Can be easily changed with more complex expressions.

+1
source

I like both versions of Greg and Bill, I think that I would more naturally write code like Greg. One advantage with intermediate variables: it is easier to debug (in general).

+1
source

All Articles