Archive for category code

Refactoring large conditional method using method references

Some years ago I wrote junit-parameters, which is basically a custom JUnit test runner that make it possible to add parameters to JUnit 4 test methods.

Browsing its source code SonarLint pointed me a large conditional if/else method from the ParameterTypeConverterFactory class:

This method converts the method parameter to its specific type based on its Class object. As it is few lines long, it showed me a good opportunity to refactor this code a little bit with a more elegant solution. This project has unit tests, so I could start refactoring it in small steps and start over again whether I have made a mistake.

I started by defining a functional interface called ParameterConverter:

and I created an immutable map which maps each Class object to its associated ParameterConverter instance (making use of method references):

Then I refactored the original conditional method to get the ParameterConverter instance from the convertersByClass map and mapping to an Optional instance in case it didn’t exist.

After those refactorings, SonarLint stopped warning me. Below is the modified version of the original method with some helper methods:

The complete code can be found here.

What did you think about this refactoring? Have you ever had such a situation? Drop your comments here!

Tags: , , , , , , , , , , ,

3 Things Every Java Developer Should Stop Doing

My friends Andre and Leonnardo have sent me an interesting article about some bad habits every Java developer should stop doing in their code.

Basically the author picked up the following points and discussed each one of them, showing code examples why they aren’t good practices at all:

  1. Returning Null
  2. Defaulting to Functional Programming
  3. Creating Indiscriminate Getters and Setters

I agree with all these points as being bad practices in Java code. How about you? What do you think about it?

Tags: , , , , ,

35 programming habits that make your code smell

Interesting article about some bad programming practices, including topics like code organization, teamwork, testing and maintenance.

Tags: , , , ,

K Palindromes puzzle

Here we have the K Palindromes puzzle from Javalobby:

You probably already know what a palindrome is: a string to results in the same word, whether read left to right, or right to left. A K Palindrome is when a string can be tranformed into a palindrome by changing K characters at most. Regular palindromes have K=0.

Your challenge today is to write a method which takes a string and a value for k and returns true if it the string qualifies as a K palindrome.

Below is the code in Ruby. Our input string is omississimo and it’s a 0 palindrome String.

The output is true. Do you have another solution? Drop your comments here!

Tags: , , , , , , ,

Minimum difference puzzle in Ruby

Folks, here we are for another puzzle from Javalobby:

Given two arrays, sorted and exactly the same length, write a function that finds a pair of numbers – one from each of the arrays – such that the difference between both is as small as possible.

Suppose our input arrays are [1,2,3,4,5] and [6,7,8,9,10]. Below is my solution in Ruby, it’s a one liner code :-):

The output is [5, 6].

Have another solution? Leave your comments here!

Tags: , , , , , , ,

Balancing arrays puzzle in Ruby

Here we are for another puzzle from Javalobby:

Given an array of numbers, return the index at which the array can be balanced by all numbers on the left side add up the the sum of all numbers of the right side.

For example, an array with [1,5,6,7,9,10] can be balanced by splitting the array at position 4

Here is the solution in Ruby:

The input array [1,5,6,7,9,10] will split the array at position 4, as stated above.

Any other solutions? Drop comments here ๐Ÿ™‚

Tags: , , , , , , ,

Finding the second highest frequency

It’s been a long time since I posted for the last time, but I promise I’ll try to keep this blog up-to-date ๐Ÿ™‚

One of the purposes of this blog is to explore new technologies and recently I’ve been studying Ruby solving some puzzles posted by on JavaLobby.

The problem is about finding the second highest frequency of a character in a String. Below is the Ruby code, using the powerful concept of Ruby closures.

For the input String "aabbbc", the result is "a". I could done it in a more OOP style, but I chose a more concise solution ๐Ÿ™‚

If you have another version in Ruby, feel free to post a comment!

Tags: , , , , ,