What Have You Learned?

Good design is doable, isn’t it? It’s the little details that improve the overall picture, and the art is to spot the flaws in the class at hand.

You probably didn’t write a lot of the code you see on your screen most of the time. So you’ll probably have to put some effort into understanding a coder’s reasoning behind her design decisions. And you always have a lot of choices to make when you design your code. It’s your job to weigh the benefits and concerns that each solution imposes. You have to make an informed decision with trade-offs that affect properties of your code, like readability, testability, maintainability, and performance.

The latter is usually the least concerning, at least at the start. When programming Java, you’re usually writing verbose but understandable code—that’s in contrast to fiddling around with pointer arithmetic and bit vectors as you’ll often see in C and Assembly. This doesn’t mean that Java programs are slow. It’s just that code understandability matters more than performance at the start. You can always go back and optimize the code later when you’ve benchmarked it and found a bottleneck. And this is much easier if your code is understandable.

That’s what this chapter was all about: improving the understandability of your classes and methods by writing them in a way that makes them intuitive with fewer avenues for misuse. If everybody on the team follows these rules, then development gets easier.

Sometimes you have to split methods and classes into several parts, whereas in other cases you must combine state and behavior. Then there’s how you handle references. You shouldn’t leak references to internal objects, and it’s even better if your objects are fully immutable. This makes them easy to understand, because there can only be one state per object. Finally, a well-designed class is flexible and guides the user to the correct usage. You can achieve this by not returning null references and by using abstract instead of very specific types as input parameters.

The design of large software systems is tremendously hard but it has been explored a lot already. If you want to read more on this, we recommend the classic book on Design Patterns [GHJV95].

In the next chapter, we’ll explore a new feature that came with Java 8: functional programming using Java streams and lambda expressions. Streams give you enormous potential for easier data manipulation, faster computations, parallelism, and more stability. So read on!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.196.146