Refactoring – Stay Quick and Beautiful

 

This post has 206 words. Reading it will take approximately 1 minute.

VN:F [1.9.17_1161]
Rating: 0.0/5 (0 votes cast)

Sometimes you have to refactor something.  It may seem like it works, but the code is horrible-looking and inefficient.

Consider for example this gem:

No documentation, missing @Override annotation, quadratic blow-up of something that should be linear, unmotivated cloning of an immutable class, over-shadowing of field variables, leading to a clumsy syntax, and a code-structure that does not reveal the output…

I rewrote it to this:

We of course add JavaDoc referring to the defining class (ensuring a link in the generated documentation and a hint to IDEs they can just copy the old description), add an @Override annotation to prevent spelling mistakes, use a StringBuilder (a thread-unsafe and faster version of the old StringBuffer class) to reduce the execution time to linear in the number of parameters, abstain from cloning Strings (which are immutable), don’t use field-names for variable names, and reorder to adhere to the order of the output.

We also don’t reimplement toString for the collection parameters, as most collections have decent default implementations which means we don’t have to introduce a boolean variable in order to correctly add commas (or doing something as ugly as the original code, making a string comparison each time…

 

Tags:

 
 
 

2 Comments

  • spand
    VA:F [1.9.17_1161]
    Rating: 0 (from 0 votes)

    Or simply:
    return name + ‘(‘ + parameters + ‘)’;

     
 

Leave a reply

required

required

optional


On my mind…

  • For a status report, I had to figure out how much code I'm maintaining. Turns out it's roughly 390000 lines.
    4 days ago
  • This is becoming a bad habit: had to run to catch the last direct train to Eindhoven.
    4 days ago
 
 

Recent Comments

  • Michael: Forgot to add: in my code in the post, what I'm trying to do is just to get all …
     
  • Michael: Yeah, unless you know you are processing an integer, you can only get an object …
     
  • Danny: Hmmm... is the first solution you explain that much different from the original?…