The focus of this article is to compare the imperative style of solving a problem vs declarative style using a couple of examples.It assumes basic knowledge about Lambdas and Streams introduced in Java 8.
Example 1: I do not wish to give you the problem statement on purpose. Look at the code below and try to figure out the intention of the code.
I am sure you read that code very carefully and possibly a couple of times. What does it do? You might read it again ! Most developers have a short memory. You know that you might have to revisit this code at some point and hence you will probably add comments in the code above like sort by name , remove last comma etc.
From a list of football leagues, the code checks if the league is the English Premier league, selects all the players who play for the English premier league where their age is <= 25 years. It then sorts them by their names and then finally returns a comma separated list of their names.
So our problem statement: Given a list of football leagues, select all the players who play for the English Premier League and are <=25 years of age. Then sort their names and return them as comma separated. Let us break this problem statement about what we want to do:
- Select all the players who play in the Premier league
- Remove all the players who are above 25 years of age
- Sort the names of the players
- Get their name and make them comma separated
Let us code this now using declarative style.
Look at the code above and the problem statements from 1-4. Don’t you think that the code above reads more like the problem statement? Not only does it read like the problem statement, the code does not have any garbage variables, no for loops instructing how we want to iterate and select. We are focusing more on what we want rather how we want to do it. Hence it is also easier to make any changes to the code in case the requirements change which means the maintenance is easy.
We could have used a .sorted(Comparator.naturalOrder()) above and done the mapping to get the name first before sorting.
Example 2: Given a list of strings,we need to output the strings based on their length. If the input string is “I”, ”ate”, ”food”, ”slept”. The output should be
1 – I
Hmm, one thing is clear that we need a Hashmap. The key will be the length of string and the value will be the actual string. So what are we doing here
- Create a new hashmap
- Iterate through the list of strings
- Create a new list
- If the current string has length x and x is already in the map then get the list corresponding to the length and add the current string to that list, else
- Create a new list and add the length of string and the string length as key , value as actual string.
- Done !
In short, we are grouping strings by their length.
What are we doing here:
- Group all the strings by length
- Done !
Again, the code is expressive and it translates to the problem statement very clearly !
The Collectors.groupingBy method returns a Map of Integer, List<String> or K, V where
K- the type that we are grouping by
V – The elements that we are operating on ( in this case, String)
Note that we have not considered the case where strings can repeat. This can be solved easily by using a Set instead of a List which would be pretty easy in both imperative and declarative style.
Declarative style does make your code more readable and concise.Declarative code might not be the magical solution to all problems, if you find the declarative style getting too complicated, stick to the old imperative style of programming.