Useful utility methods in order to have easy-to-read and easy-to-understand code

Useful utility methods in order to have easy-to-read and easy-to-understand code

The compiler understands the code even if it is messy, unstructured, or does not follow any clean code principles. That is not the case with developers. For developers, it is very important the code be easy to read and easy to understand. To achieve good readability there is a lot of good practice and principles to follow.

In this article, I want to share with you some useful utility methods that will contribute a lot to having easy-to-read and easy-to-understand code.

The java.util.Objects class contains utility methods for operating on objects.

  • Objects.isNull(Object):boolean. Returns true if the provided reference is null otherwise returns false.
  • Objects.nonNull(Object):boolean. Returns true if the provided reference is non-null otherwise returns false.
  • Objects.requireNonNull(T):T. Checks that the specified object reference is not null and throws a NullPointerException if it is.

The java.util.Objects class contains further utility methods, I highly recommend checking them out here.


The org.apache.commons.lang3.ObjectUtils classcontains utility methods for operating on objects.

  • ObjectUtils.isEmpty(Object):boolean. Checks if an Object is emty or null. It supports the following types: CharSequence, Array, Collection, Map.
  • ObjectUtils.isNotEmpty(Object):boolean. Checks if an object is not empty and not null. It supports the following types: CharSequence, Array, Collection, Map.
  • ObjectUtils.defaultIfNull(T object, T default):T. Returns a default value if the object passed is null.

The org.apache.commons.lang3.ObjectUtils class contains further utility methods, I highly recommend checking them out here.


The org.apache.commons.lang3.BooleanUtils class contains utility methods regarding boolean operations.

  • BooleanUtils.isFalse(Boolean):boolean. Checks if a boolean value is false, handling null by returning false.
  • BooleanUtils.isNotFalse(Boolean):boolean. Checks if a boolean value is not false, handling null by returning true.
  • BooleanUtils.isTrue(Boolean):boolean. Checks if a boolean value is true, handling null by returning false.
  • BooleanUtils.isTrue(Boolean):boolean. Checks if a boolean value is not true, handling null by returning true.
  • BooleanUtils.negate(Boolean):Boolean. Negates the given boolean. If null is passed in, null will be returned.

The BooleanUtils class contains further utility methods, recommend checking them out here.


The org.apache.commons.lang3.StringUtils contains String related utility methods. This class contains much more methods than the previously mentioned ones.

  • StringUtils.isBlank(CharSequence):boolean. Checks is a CharSequence is empty, null or whitespace only.
  • StringUtils.isNotBlank(CharSequence):boolean. Checks is a CharSequence is not empty, not null or not whitespace only.
  • StringUtils.startsWith(CharSequence str, CharSequence prefix):boolean. Checks if a CharSequence starts with a specified prefix.
  • StringUtils.startsWith(CharSequence str, CharSequence suffix):boolean. Checks if a CharSequence ends with a specified suffix.
  • StringUtils.equalsIgnoreCase(CharSequence cs1, CharSequence cs2):boolean. Compares two CharSequences, returning true if they represent equal sequences of characters, ignoring case.

The StringUtils class contains further utility methods, recommend checking them out here.

Conclusion:

In this article, I listed only a few of the existing utility methods that can help you achieve clean and readable code. Through the listed methods you could see how much easier is to understand the code because the utility method names are human-readable.

I highly recommend you invest in learning how to clean code, because that will be beneficial for you, for the team, and for the client.

Lee McKeeman

Staff Software Engineer at Google * Good Person to Know * Autistic * Now on Substack @leemckeeman!

3 年

There are dozens of ways to skin a cat, and often a team needs to decide what their preferred ways are. Guava has MoreObjects.firstNonNull, there is Optional chaining: return Optional.of(employee).map(Employee::getDepartment).orElse(null) //Can use orElseGet with a supplier if default is expensive to compute Sometimes ternary works, and may be more “at home” final Department department = employee.getDepartment(); return (department == null ? defaultDepartment : department); Depending on the team, current dependency set, Java version, etc. any might be the preferred way. Edit to add: Picking any one way, and having the team agree and be consistent is frankly more important than which way. Even a suboptimal way that’s the same throughout your code, and easy enough to find and refactor or write a code mod for is better than having it done differently everywhere. Some code may be allocation sensitive, so Optional may not be great. Some code bases may be dependency sensitive, so writing your own small set of utilities may be better than Guava or Apache. It’s important to make a decision and get it vetted by the team than letting everyone do their own thing, though.

Alan Mellor

Software Craftsman, Educator, Author

3 年

My randomo opinion: I flip and flop on these. The if statement version is pretty clear if you bin the else and have if (not null){ return thing; } return default; I'd be more sold if it had a different name like Nulls.valueOrDefault( thing, default ); I'd like it as a non-static private as well. But I really like the fact you're paying this attention, because the battle for clean code starts with lines like these.

要查看或添加评论,请登录

Helmut Siegel的更多文章

  • How to inject all the beans with a specific type and qualifier using CDI?

    How to inject all the beans with a specific type and qualifier using CDI?

    Introduction CDI stands for Context and Dependency Injection which is a standard dependency injection framework…

  • ArrayList vs LinkedList

    ArrayList vs LinkedList

    ArrayList and LinkedList are both implementations of the java.util.

    2 条评论
  • Don't use Java Arrays!

    Don't use Java Arrays!

    An array is a container that holds a fixed number of values of a single type. Dealing daily with business code, we see…

    4 条评论

社区洞察

其他会员也浏览了