Brain friendly programming in Java

We have many principles for writing better more maintainable software, but still end up with a tangled mess.

Most of us have heard about the Single Responsibility Principle (SRP), Robert C Martin expressed this principle as “A class should have only one reason to change”.

He later clarified this by stating “Gather together the things that change for the same reasons. Separate those things that change for different reasons”.

We also have the Single Layer of Abstraction Principle (SLAP) which is expressed as “Don’t mix levels of abstractions”.

Dan Terhorst-North presented his Unix Philosophy as “does one thing well”.

The goal is to create more maintainable software, not to fulfill the principle. It’s still possible to create a mess even though we follow all the SOLID principles. So what do these principles mean, and how can I as a developer use them to create better software?

The Science Behind Brain-Friendly Programming Methods

In “The Programmer’s Brain“, Dr. Felienne Hermans talks about how our brain works working with code.  It’s a highly recommended read.

One of the key takeaways for me was the fact that our working memory can only keep track of 3 – 5 chunks of information at the same time. How big a chunk is depends on how much experience we have with the current task.

It can be individual statements, lines, or entire design patterns.

We can make our code easier to understand, to lower the cognitive load of the reader, by limiting the number of chunks of information we have to keep track of.

Ok, so how do we do that then?

Cognitive Load: Minimizing Mental Strain for Optimal Programming Performance

This is when the presentation “Simple Made Easy” from Rich Hickey, comes in.

He talks about how we complect our code.

Complect is an old word that means to braid together, to weave together. By completing our code we make it harder to understand by requiring the reader to keep more chunks of information in memory. We are increasing the cognitive load for the reader.

He suggests that we should separate our code based on the following questions:

  • What needs to be done?
  • Who wants to do it?
  • How should it be done?
  • When should it be done?
  • Where should it be done?
  • Why should it be done?

By mixing these, complecting them, we create code that is harder to understand because it requires the reader to keep more chunks in memory, which creates a higher cognitive load.

By keeping these separate, we get code that is easier to change and reuse, for instance, we can then change how something is done without affecting who wants it done.

Another thing we can do to make our code easier to understand is to take advantage of new features in the language we are using.

We often hear that we should update to the latest version of our language for performance and security reasons. But there are also improvements in the language that can make our code easier to understand.

Let’s take Java as an example.

Leveraging New Java Language Features to Lower the Cognitive Load of the Programmer

Java, being one of the most popular programming languages in the world, is constantly evolving to meet the changing demands of software development. With each iteration, Java introduces new language features that not only enhance the performance and scalability of applications but also aim to make the lives of programmers easier.

I’ll list a few of the new Java language features that, when used, can lower the cognitive load of programmers, allowing us to write more efficient and maintainable code.

Concise Code with Lambda Expressions

Lambda expressions, introduced in Java 8, allow developers to write more concise code by expressing instances of anonymous functions. By using lambda expressions, programmers can reduce boilerplate code and focus on the core logic of their applications. This feature is a good match for handling collections, stream processing, and event handling. For example, instead of writing lengthy loops to iterate over a list, lambda expressions can be used to achieve the same result in a more compact and readable manner.

Improved Null Safety with Optional

Java 8 introduced the Optional class, which helps handle null values more effectively. By using Optional, programmers can avoid NullPointerExceptions and write safer code. Optional encourages developers to handle null values explicitly, leading to more robust and error-free applications. This feature reduces the cognitive load of programmers by providing a clear and standardized way to deal with null values, making the code more predictable and easier to reason about.

Enhanced Pattern Matching

Java 14 introduced pattern matching for instanceof and this has been extended and improved on in newer versions since then. This feature simplifies the code by combining type checking and type casting into a single operation. Programmers can write cleaner and more concise code when working with complex data structures by using pattern matching. Pattern matching reduces the cognitive load of programmers by eliminating the need for if-else blocks and switch statements, leading to more readable and maintainable code.

Simplified Asynchronous Programming with CompletableFuture

Asynchronous programming is essential for building responsive and scalable applications. Java 8 introduced the CompletableFuture class, which makes asynchronous programming simpler by providing a convenient way to work with future values. Programmers can chain asynchronous operations and handle their results more intuitively using CompletableFuture. This feature reduces the cognitive load of developers by abstracting the complexity of asynchronous programming and allowing them to focus on the business logic of their applications.

The evolution of Java language features has brought significant improvements to the way programmers write and maintain code. By leveraging features such as lambda expressions, Optional, pattern matching, and CompletableFuture, developers can lower the cognitive load associated with programming tasks. These features enable programmers to write more concise, robust, and maintainable code, ultimately leading to increased productivity and efficiency in software development. 

As Java continues to evolve, developers can expect more innovations to enhance their programming experience further and make Java an even more powerful and developer-friendly language.

Conclusion

By understanding how the brain processes information, developers can create programming languages and environments that are more intuitive, efficient, and less cognitively demanding for users. This approach can improve the overall user experience, increase productivity, and promote greater accessibility for individuals of all cognitive abilities.

Embracing brain-friendly programming will not only benefit individual programmers and users but has the potential to revolutionize the way we interact with technology and shape the future of computing.

One thought on “Brain friendly programming in Java

Leave a comment