Blog-Archiv

Sonntag, 12. Dezember 2021

Java Functional Programming and Readability

Unfortunately developers tend to be proud when producing code that looks complex. The functional extension that was added to Java 8 makes it easy to write source code that is shorter than a conventional object-oriented solution, but hard to read for people not used to functional programming.

So will it be a "write less, do more" innovation or new ammunition for code wars?

Example Application

We want to find out the most frequent time-offset (from Greenwich mid-time) from a list of TimeZone:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    public static void main(String[] args) {
        final List<TimeZone> zones = new ArrayList<>();
        for (String timeZoneId : TimeZone.getAvailableIDs())
            zones.add(TimeZone.getTimeZone(timeZoneId));
        
        final int mostFrequentRawOffset = mostFrequentRawOffset(zones);
        
        for (TimeZone zone : zones)
            if (mostFrequentRawOffset == zone.getRawOffset())
                System.out.println(zone.getID()+" (\""+zone.getDisplayName()+"\")");
    }

Line 2 - 4 collects all available time-zones into a list.
Line 6 calculates the time-offset (in millis) that contains the most time-zones by calling mostFrequentRawOffset().
Line 8 - 10 outputs all zones that have exactly this offset.

How can we implement the mostFrequentRawOffset() method?

Conventional Solution

For demonstration that this problem is not so easy to solve, here is a conventional solution using standard Java runtime classes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
    private static int mostFrequentRawOffset(List<TimeZone> zones)    {
        Map<Integer,Integer> rawOffset2Frequency = new Hashtable<>();
        for (TimeZone zone : zones)    {
            Integer rawOffset = zone.getRawOffset();
            Integer frequency = rawOffset2Frequency.get(rawOffset);
            if (frequency == null)
                frequency = 0;
            rawOffset2Frequency.put(rawOffset, frequency + 1);
        }
        int maxFrequency = -1;
        int mostFrequent = -1;
        for (Map.Entry<Integer,Integer> entry : rawOffset2Frequency.entrySet())    {
            int frequency = entry.getValue();
            if (frequency > maxFrequency)    {
                maxFrequency = frequency;
                mostFrequent = entry.getKey();
            }
        }
        return mostFrequent;
    }

In lines 2 - 9 we collect the time-zones into a map where key is the time-offset and value is the count of time-zones that have exactly this time-offset.
Lines 10 - 18 searches the maximum value in that map.
The most frequent offset (key) is then returned in line 19.

We could make this 2 lines shorter, so let's say it is a 18 line solution.

Functional Solution

Now here is an already optimized functional solution that does exactly the same:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    private static int mostFrequentRawOffset(List<TimeZone> zones)    {
        return zones
            .stream()
            .collect(
                Collectors.groupingBy(
                    TimeZone::getRawOffset,
                    Collectors.counting()
                )
            )
            .entrySet()
            .stream()
            .max(Map.Entry.comparingByValue())
            .map(Map.Entry::getKey)
            .orElse(-1);
    }

This is a 15 line solution. Not much but anyway shorter than the conventional solution above. The difference is that with the functional solution you need to know the behavior of all involved functions, while with the conventional solution you just need to know the behavior of Map, nothing else.

Because I do not want to explain every single line of this code, here is a variant that may be more readable, because it has intermediate results which expose the return types of the used stream-functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
    private static int mostFrequentRawOffset(List<TimeZone> zones)    {
        Map<Integer,Long> rawOffset2Frequency = zones
            .stream()
            .collect(
                Collectors.groupingBy(
                    TimeZone::getRawOffset,
                    Collectors.counting()
                )
            );
        Map.Entry<Integer,Long> maxEntry = rawOffset2Frequency
            .entrySet()
            .stream()
            .max(Map.Entry.comparingByValue())
            .orElse(null);
        return (maxEntry != null) ? maxEntry.getKey() : -1;
    }

In line 3 we turn the list into a stream and send it into the collect() function that builds a map.
This map is built by the static Collectors.groupingBy(), which does exactly the same what we do in the conventional method in lines 2 - 9 (yes, we need to memorize all these functions).
Line 6 tells the lambda to use for the map's key, which is TimeZone::getRawOffset, line 7 tells the lambda for the map's value, which is the static Collectors.counting() function (yes, we need to memorize ...).
In line 10 we fetch the map's entry-set and stream it into a maximum-searcher, represented by the static Map.Entry.comparingByValue() function (yes, we need ...).
If the list is empty, orElse() will produce null in line 14.
Line 15 finally tests for null and returns -1 when so, else it returns the key of the found maximum entry.

This second functional solution differs from the first one above in that it does not map the result in line 10 to the getKey() method but calls orElse(null) instead in line 14. The one above fetches the key from the optional maximum by mapping to the MapEntry::getKey function in line 13. This won't fail if there is no value, in that case the orElse(-1) function will be executed.
→ That's the functional style to write an if-else!

All clear? When not, let it soak in, for a day or so, people like us have plenty of time ...

Resume

So we got a conventional 18-liner against a functional 15-liner.
Could you verify that they both do the same?

It is not a miracle that no functional language is among the 5 most popular programming languages of the world. The most popular (Python, Java, C++, JavaScript/ES6 etc.) are either object-oriented or near to OO. True, popularity is not a quality feature. But what is quality of source code? Shortness? Readability? Comprehensibility? Maintainability? What counts most?

Functional languages are science, having a steep and long learning curve. Functional programmers won't talk about readability: "Of course you can read it" (hope you also understand it). Is Java still Smalltalk for the masses, or are we going back to be an expert realm?




Sonntag, 28. November 2021

Refactoring the Code Duplication Example

This article refers to the code duplication example I presented in my preceding Blog.
I would propose two steps for getting rid of the code duplications in the example:

  1. Move the if-conditions away from AuthorExtractor into the caller by using the Java 8 functional interface Predicate

  2. Wrap the three different document-classes into just one class that provides a unified interface to them

Here are the methods in AuthorExtractor from which code duplications are to be removed:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    public List<String> withNamepartFromDocumentsYoungerThan(
            String namePart,
            Date date,
            List<CsvDocument> csvDocuments,
            List<ExcelDocument> excelDocuments,
            List<PdfDocument> pdfDocuments)
    {
        final List<String> authors = new ArrayList<String>();
        
        for (CsvDocument csvDocument : csvDocuments)    {
            final String authorFullName = csvDocument.getAuthorFullName();
            final Date createdAt = csvDocument.getCreatedAt();
            // START code duplication 1
            if (authorFullName.contains(namePart) && createdAt.after(date))    {
                authors.add(authorFullName);
            }
            // END code duplication 1
        }
        
        for (ExcelDocument excelDocument : excelDocuments)    {
            final String author = excelDocument.getAuthor();
            final Date creationDate = excelDocument.getCreationDate();
            // START code duplication 1
            if (author.contains(namePart) && creationDate.after(date))    {
                authors.add(author);
            }
            // END code duplication 1
        }
        
        for (PdfDocument pdfDocument : pdfDocuments)    {
            final String authorFirstName = pdfDocument.authorFirstName();
            final String authorLastName = pdfDocument.authorLastName();
            final String authorFirstAndLastName = authorFirstName+" "+authorLastName;
            final Date createdAt = pdfDocument.createdAt();
            // START code duplication 1
            if (authorFirstAndLastName.contains(namePart) && createdAt.after(date))    {
                authors.add(authorFirstAndLastName);
            }
            // END code duplication 1
        }
        
        return authors;
    }
    
    // START code duplication 2
    public List<String> withNamestartFromDocumentsOlderEqual(
            String nameStart,
            Date date,
            List<CsvDocument> csvDocuments,
            List<ExcelDocument> excelDocuments,
            List<PdfDocument> pdfDocuments)
    {
        final List<String> authors = new ArrayList<String>();
        
        for (CsvDocument csvDocument : csvDocuments)    {
            final String authorFullName = csvDocument.getAuthorFullName();
            final Date createdAt = csvDocument.getCreatedAt();
            // START code duplication 3
            if (authorFullName.startsWith(nameStart) && (createdAt.equals(date) || createdAt.before(date)))    {
                authors.add(authorFullName);
            }
            // END code duplication 3
        }
        
        for (ExcelDocument excelDocument : excelDocuments)    {
            final String author = excelDocument.getAuthor();
            final Date creationDate = excelDocument.getCreationDate();
            // START code duplication 3
            if (author.startsWith(nameStart) && (creationDate.equals(date) || creationDate.before(date)))    {
                authors.add(author);
            }
            // END code duplication 3
        }
        
        for (PdfDocument pdfDocument : pdfDocuments)    {
            final String authorFirstName = pdfDocument.authorFirstName();
            final String authorLastName = pdfDocument.authorLastName();
            final String authorFirstAndLastName = authorFirstName+" "+authorLastName;
            final Date createdAt = pdfDocument.createdAt();
            // START code duplication 3
            if (authorFirstAndLastName.startsWith(nameStart) && (createdAt.equals(date) || createdAt.before(date)))    {
                authors.add(authorFirstAndLastName);
            }
            // END code duplication 3
        }
        
        return authors;
    }
    // END code duplication 2

Move Search Condition to Caller

When comparing the withNamepartFromDocumentsYoungerThan() method with withNamestartFromDocumentsOlderEqual() it is obvious that the if-conditions (line 14, 24, 36 - 59, 69, 81) are the only difference between them. When this can be externalized, we should end up with just one method.

This refactored method can't have the condition in its name any more, because the caller will determine the condition, so we call it findAll() now:

import java.util.function.Predicate;

public class AuthorExtractor
{
    /** Predicates passed to find() will receive objects of this class. */
    public static class ConditionParameter
    {
        public final String author;
        public final Date creationDate;
        
        public ConditionParameter(String author, Date creationDate)    {
            this.author = author;
            this.creationDate = creationDate;
        }
    }
    
    public List<String> findAll(
            Predicate<ConditionParameter> condition,
            List<CsvDocument> csvDocuments,
            List<ExcelDocument> excelDocuments,
            List<PdfDocument> pdfDocuments)    {

        .....
    }
}

Externalizing the condition means that we need to provide a parameter type to the Predicate class. This has been implemented by the immutable class ConditionParameter. Mind that the public members do not violate encapsulation as they are final.

Here is what the caller in CodeDuplication.main() must do now:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class CodeDuplication
{
    public static void main(String[] args) throws Exception {
        final List<CsvDocument> csvDocuments = new ArrayList<>();
        csvDocuments.add(new CsvDocument());
        final List<ExcelDocument> excelDocuments = new ArrayList<>();
        excelDocuments.add(new ExcelDocument());
        final List<PdfDocument> pdfDocuments = new ArrayList<>();
        pdfDocuments.add(new PdfDocument());
        
        final Date date1 = Date.from(Instant.now().minus(Period.of(0, 0, 2)));    // 2 days ago
        final List<String> authors1 = new AuthorExtractor().findAll(
                (p) -> p.author.contains("a") && p.creationDate.after(date1),
                csvDocuments,
                excelDocuments,
                pdfDocuments);
        System.out.println(authors1);
        
        final Date date2 = Date.from(Instant.now());
        final List<String> authors2 = new AuthorExtractor().findAll(
                (p) -> p.author.contains("A") && (p.creationDate.equals(date2) || p.creationDate.before(date2)),
                csvDocuments,
                excelDocuments,
                pdfDocuments);
        System.out.println(authors2);
    }
}

In line 13 and 21 are the lambdas that implement the Predicate conditions needed for the new findAll() method. And there is room for many more conditions, although restricted to dealing with author and creationDate.

Now that the condition in AuthorExtractor doesn't require code duplications any more, we can implement the new findAll() method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    public List<String> findAll(
            Predicate<ConditionParameter> condition,
            List<CsvDocument> csvDocuments,
            List<ExcelDocument> excelDocuments,
            List<PdfDocument> pdfDocuments)
    {
        final List<String> authors = new ArrayList<String>();
        
        for (CsvDocument csvDocument : csvDocuments)    {
            final String authorFullName = csvDocument.getAuthorFullName();
            final Date createdAt = csvDocument.getCreatedAt();
            if (condition.test(new ConditionParameter(authorFullName, createdAt)))    {
                authors.add(authorFullName);
            }
        }
        
        for (ExcelDocument excelDocument : excelDocuments)    {
            final String author = excelDocument.getAuthor();
            final Date creationDate = excelDocument.getCreationDate();
            if (condition.test(new ConditionParameter(author, creationDate)))    {
                authors.add(author);
            }
        }
        
        for (PdfDocument pdfDocument : pdfDocuments)    {
            final String authorFirstName = pdfDocument.authorFirstName();
            final String authorLastName = pdfDocument.authorLastName();
            final String authorFirstAndLastName = authorFirstName+" "+authorLastName;
            final Date createdAt = pdfDocument.createdAt();
            if (condition.test(new ConditionParameter(authorFirstAndLastName, createdAt)))    {
                authors.add(authorFirstAndLastName);
            }
        }
        
        return authors;
    }

Due to externalizing the search-condition we could remove the copied search-method withNamestartFromDocumentsOlderEqual(). Step one is done.

Still there are three loops, should be just one. So let's continue with step two.

Wrap Different Document Classes into just One

The idea is to write one wrapper class for the different document-types that provides all search criterions (like author and creation-date) generically. The class ConditionParameter already encapsulates these criterions, so we will reuse it in the wrapper.

In this step we will not touch the calls in CodeDuplication.main() any more. We can put all document wrappers privately into AuthorExtractor.

    private static class Document
    {
        public final ConditionParameter authorAndCreationDate;
        
        Document(CsvDocument csvDocument) {
            this.authorAndCreationDate = new ConditionParameter(
                    csvDocument.getAuthorFullName(),
                    csvDocument.getCreatedAt());
        }
        Document(PdfDocument pdfDocument) {
            this.authorAndCreationDate = new ConditionParameter(
                    pdfDocument.authorFirstName()+" "+pdfDocument.authorLastName(),
                    pdfDocument.createdAt());
        }
        Document(ExcelDocument excelDocument) {
            this.authorAndCreationDate = new ConditionParameter(
                    excelDocument.getAuthor(),
                    excelDocument.getCreationDate());
        }
    }

This helper class will wrap exactly one document. For each of the three document-types there is a dedicated constructor (so if you add another type, you will need another constructor here). After construction, all necessary information for the search will have been fetched from the original document to the wrapper.

One more wrapper encpsulates all three lists. Again, if another document-list gets added, also this class needs maintenance:

    private static class Documents implements Iterable<Document>
    {
        private final Iterator<CsvDocument> csvDocuments;
        private final Iterator<ExcelDocument> excelDocuments;
        private final Iterator<PdfDocument> pdfDocuments;
        
        Documents(
                List<CsvDocument> csvDocuments,
                List<ExcelDocument> excelDocuments,
                List<PdfDocument> pdfDocuments)    {
            this.csvDocuments = csvDocuments.iterator();
            this.excelDocuments = excelDocuments.iterator();
            this.pdfDocuments = pdfDocuments.iterator();
        }
        
        @Override
        public Iterator<Document> iterator() {
            return new Iterator<AuthorExtractor3.Document>()
            {
                @Override
                public boolean hasNext() {
                    return csvDocuments.hasNext() || 
                            excelDocuments.hasNext() || 
                            pdfDocuments.hasNext();
                }
                @Override
                public Document next() {
                    return csvDocuments.hasNext() ? new Document(csvDocuments.next())
                        : excelDocuments.hasNext() ? new Document(excelDocuments.next())
                            : pdfDocuments.hasNext() ? new Document(pdfDocuments.next())
                                : null;
                }
            };
        }
    }

This implements the Java interface Iterable to be used in a for-loop. The Iterable implementation is done in an anonymous class that delegates to the three lists. It returns a wrapper Document so that the caller doesn't need to care any more for the type of the document.

Now we can implement the findAll() method in a very simple way:

    public List<String> findAll(
            Predicate<ConditionParameter> condition,
            List<CsvDocument> csvDocuments,
            List<ExcelDocument> excelDocuments,
            List<PdfDocument> pdfDocuments)
    {
        final List<String> authors = new ArrayList<String>();
        
        for (Document document : new Documents(csvDocuments, excelDocuments, pdfDocuments))
            if (condition.test(document.authorAndCreationDate))
                authors.add(document.authorAndCreationDate.author);
        
        return authors;
    }

The loop now uses the Iterable interface of the generic class Documents. The condition inside the loop calls the given Predicate parameter, and when that condition succeeds, the document is added to the list.
That's what we need. Easy to read and understand, short and concise.

Here is the full refactored source code of AuthorExtractor:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.function.Predicate;

public class AuthorExtractor
{
    /** Predicate lambdas passed to find() will receive objects of this class. */
    public static class ConditionParameter
    {
        public final String author;
        public final Date creationDate;
        
        public ConditionParameter(String author, Date creationDate)    {
            this.author = author;
            this.creationDate = creationDate;
        }
    }
    
    public List<String> findAll(
            Predicate<ConditionParameter> condition,
            List<CsvDocument> csvDocuments,
            List<ExcelDocument> excelDocuments,
            List<PdfDocument> pdfDocuments)
    {
        final List<String> authors = new ArrayList<String>();
        
        for (Document document : new Documents(csvDocuments, excelDocuments, pdfDocuments))
            if (condition.test(document.authorAndCreationDate))
                authors.add(document.authorAndCreationDate.author);
        
        return authors;
    }
    
    
    private static class Document
    {
        public final ConditionParameter authorAndCreationDate;
        
        Document(CsvDocument csvDocument) {
            this.authorAndCreationDate = new ConditionParameter(
                    csvDocument.getAuthorFullName(),
                    csvDocument.getCreatedAt());
        }
        Document(PdfDocument pdfDocument) {
            this.authorAndCreationDate = new ConditionParameter(
                    pdfDocument.authorFirstName()+" "+pdfDocument.authorLastName(),
                    pdfDocument.createdAt());
        }
        Document(ExcelDocument excelDocument) {
            this.authorAndCreationDate = new ConditionParameter(
                    excelDocument.getAuthor(),
                    excelDocument.getCreationDate());
        }
    }
    
    private static class Documents implements Iterable<Document>
    {
        private final Iterator<CsvDocument> csvDocuments;
        private final Iterator<ExcelDocument> excelDocuments;
        private final Iterator<PdfDocument> pdfDocuments;
        
        Documents(
                List<CsvDocument> csvDocuments,
                List<ExcelDocument> excelDocuments,
                List<PdfDocument> pdfDocuments)    {
            this.csvDocuments = csvDocuments.iterator();
            this.excelDocuments = excelDocuments.iterator();
            this.pdfDocuments = pdfDocuments.iterator();
        }
        
        @Override
        public Iterator<Document> iterator() {
            return new Iterator<AuthorExtractor.Document>()
            {
                @Override
                public boolean hasNext() {
                    return csvDocuments.hasNext() || 
                            excelDocuments.hasNext() || 
                            pdfDocuments.hasNext();
                }
                @Override
                public Document next() {
                    return csvDocuments.hasNext() ? new Document(csvDocuments.next())
                        : excelDocuments.hasNext() ? new Document(excelDocuments.next())
                            : pdfDocuments.hasNext() ? new Document(pdfDocuments.next())
                                : null;
                }
            };
        }
    }

}

Resume

Refactoring code duplications is not an easy task. Depending on the reason for the duplication you will need to find concepts on method- and class-level. Introducing interfaces will always be a safe way, although functional expressions may be much shorter. The effort for fixing code duplications can be saved when writing professional code right from start.