Every programmer agrees naming classes is highly important for code readability. Proper naming decrease the time needed to understand the code base. Many developers will also agree that class naming isn’t easy. There are numerous queries around the best practices which come not only from the beginners.
The aim of this article is to put in one place answers for the most popular questions around Java class name conventions and community standards. I’ll cover technical Java language restrictions, common conventions, and popular class naming best practices.
So much by way of introduction. Let’s get down into it.
Continue reading “Java class naming conventions, rules, and best practice”
The object equals contract indicates that when two objects are equal, their hash codes must also be the same. It’s a general agreement for all Java objects used in hash-based collections. Its main purpose is to optimize performance when working e.g. with HashMap or HashSet.
You may hear that when you implement the equals() method for your class, you should also implement the hashCode() method. That’s the practical approach for fulfilling the equals contract.
If you want to know more details why the contract is so important, keep on reading.
Continue reading “What is Java object equals contract?”
Is Spring controller/service/singleton thread-safe?
It’s a commonly asked question by Spring newcomers and probably a must-have warm-up question on job interviews. As usual in programming, the answer is: it depends. The main factor which determines thread safety of a component is its scope.
Let’s get down to it and see what Spring’s scopes have to offer in multithreaded programming.
Continue reading “Spring bean thread safety guide”
Spring Boot does a lot of configuration automatically for us but sooner or later you’ll have to do some custom work. In this post, you will learn how to hook into the application bootstrap lifecycle and execute code on Spring Boot startup.
So let’s see what the framework has to offer.
Continue reading “Running code on Spring Boot startup”
When you design a method in a Java class, some parameters may be optional for its execution. No matter it is inside a DTO, a fat model domain object, or a simple stateless service class, optional method parameters are common.
From this article you will learn how to handle optional parameters in Java. We’ll focus on regular method, class constructors with optional fields, and quickly look at bad practices of the discussed topic. We’ll stop for a moment to look at Java 8 Optional and assess if it fits our needs.
Let’s get started.
Continue reading “Java optional parameters in practice”
With a small amount of work, you can speed up your application and improve the overall user experience. In this article, you will learn how to use the built-in HTTP response cache mechanism for Spring controller’s results.
Continue reading “HTTP cache with Spring examples”
Writing parameterized tests in JUnit 4 was pretty cumbersome. JUnit 5 introduced several useful improvements to the framework and running the same test with different arguments is much simpler than in the previous version. However, there is one small issue with passing null values in such arguments.
In this post, I’m going to show you how to pass null in @CvsSource and @ValueSource for @ParametrziedTest in JUnit 5.
Continue reading “@ParameterizedTest with null values in @CvsSource”
Whether you like it or not, software development is a collaborative activity. Integration work has always been demonized and treated as necessary evil. There are several approaches which try to solve the challenge of effective integration. The feature toggle belongs to that group. In this article, you’ll see in practice how feature toggles, also known as feature flags, can be used in your Spring Boot application.
Continue reading “Feature toggle in Spring Boot 2”
It’s been almost two years since Java 8 was officially released and many excellent articles about new enhancements and related best practices have been written through that time. Surprisingly, one of the more controversial topics amongst all the added features is the Optional class. The type is a container, which can be either empty or contain a non-null value. Such construction reminds the user of an Optional object that the situation when there’s nothing inside must be handled appropriately. Although the definition of the type on Javadoc is quite descriptive, when it comes to identifying valid use cases it’s getting more problematic.
Continue reading “Java 8 Optional best practices and wrong usage”
Sending an email from the backend application part is a quite common use case in the world of enterprise applications. Although HTML content isn’t standardized message format, numerous mail clients support at least a subset of the markup language. In this post you will learn how to send an HTML email using Spring Boot standard modules and prepare an HTML content for a message using Thymeleaf template engine.
Continue reading “Sending HTML email with Spring Boot and Thymeleaf”