Core Java

The Mute Design Pattern

Have you been writing a lot of code following the Mute-Design-Pattern™ lately? E.g.

1
2
3
4
5
6
7
8
9
try {
    complex();
    logic();
    here();
}
catch (Exception ignore) {
    // Will never happen hehe
    System.exit(-1);
}

There’s an easier way with Java 8!

Just add this very useful tool to your Utilities or Helper class:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
public class Helper {
 
    // 18395 lines of other code here
 
    @FunctionalInterface
    interface CheckedRunnable {
        void run() throws Throwable;
    }
 
    public static void mute(CheckedRunnable r) {
        try {
            r.run();
        }
        catch (Throwable ignore) {
            // OK, better stay safe
            ignore.printStackTrace();
        }
    }
 
    // 37831 lines of other code here
}

Now you can wrap all your logic in this nice little wrapper:

1
2
3
4
5
mute(() -> {
    complex();
    logic();
    here();
});

Done!

Even better, in some cases, you can use method references

1
2
3
4
5
try (Connection con = ...;
     PreparedStatement stmt = ...) {
 
    mute(stmt::executeUpdate);
}
Reference: The Mute Design Pattern from our JCG partner Lukas Eder at the JAVA, SQL, AND JOOQ blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Lukas Eder

Lukas is a Java and SQL enthusiast developer. He created the Data Geekery GmbH. He is the creator of jOOQ, a comprehensive SQL library for Java, and he is blogging mostly about these three topics: Java, SQL and jOOQ.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button