Who Should Read This Book

This book is for people who are learning to program in Java at a beginner or intermediate level. It’s also a classroom resource for teachers who coach new developers in their journey to become programmers. Here, we’re giving you tips and tricks based on more recent Java 8[1] syntax for resource handling, functional programming, or testing.

You should read this book after you’ve learned the basic Java syntax—after you’re able to write small programs with conditions and loops and you know the basics of object-oriented programming. You should be able to write code that compiles, runs, and solves small tasks like FizzBuzz (see Are You Ready? Try the Self-Assessment). You should be able to implement simple algorithms, and you should know how to use basic data structures like a list, queue, stack, or map. And obviously, you should be having fun while doing all that!

If you feel a deep satisfaction when you solve a complicated problem, then that’s an excellent start. But of course, you also know that there’s still a lot to learn. When you reflect on your skills and you have to confess that you don’t have a lot of experience in programming in Java (or even programming in general), then you can get the maximum benefit out of this book. This means that you probably haven’t yet developed a sense for clean code and the best practices an experienced developer applies.

It’s about time to change this!

Of course, if you already know more advanced books on code quality, readability, maintainability, and clean code in Java, such as Effective Java [Blo18] and Clean Code [Mar08], then you’ve already come a long way. Nevertheless, you can still find something new here and there.

Teaching Clean Code Using This Book

If you teach programming to newcomers as a senior developer at a company, you’re certainly aware of the best practices we’ll go over in this book. You might even disagree with some, depending on what you are working on. Still, this book can make your life easier when training a junior developer—just use it as a reference. When you spot problems in your apprentice’s code, point her to the corresponding item in this book. Your student can read a concise and simple example for the problem you’re hinting at, as well as how to get to the solution of the problem. At the very least, this saves you time because you won’t have to write an explanation yourself.

Our experience in developing teaching concepts for Java learners in their second to third year can also be useful for teaching in academia. This book is the product of our combined knowledge of over fifteen years of teaching Java to undergraduate college students in an advanced course with focus on code quality. It might not teach Java from the ground up, but it can accompany basically any course that involves programming tasks. In particular, you can use the book as a reference when you asses student code, as we’ve described in a workshop paper: Teaching Clean Code [DMHL18].

Are You Ready? Try the Self-Assessment

If you’re a new programmer, we suggest that you do a short self-assessment to see if you’re ready for the material in this book: the FizzBuzz Test (see the Fizz Buzz Test[2] or Using FizzBuzz to Find Developers who Grok Coding[3]). Some employers use this test in job interviews to determine if an applicant can program at all. The task goes like this:

Write a Java program that prints the numbers from 1 to 100 to the console. But for multiples of three, print Fizz instead of the number and for multiples of five, print Buzz. For numbers that are multiples of both three and five, print FizzBuzz.

To make the test more interesting, we’ll extend it a bit here by making sure that you can apply object orientation and use classes and interfaces as well. You should implement the FizzBuzz algorithm in a class called ConsoleBasedFizzBuzz, which implements a FizzBuzz interface. This interface provides a method that takes the first and last numbers to print as arguments. In the main method of a separate Main class, you should use the FizzBuzz interface with its ConsoleBasedFizzBuzz implementation to count from 1 up to the value passed from the console. Here, you’ll see the outlined structure in a short template.

 interface​ FizzBuzz {
 void​ print(​int​ from, ​int​ to);
 }
 
 
 class​ ConsoleBasedFizzBuzz ​implements​ FizzBuzz {
 // TODO implement FizzBuzz interface
 }
 
 
 class​ Main {
 // TODO use a main method
 // TODO print fizz buzz from 1 to max
 // TODO max is passed from the console
 }

You should be able to finish this exercise in about 15 minutes. One of the links we listed also contains solutions to the FizzBuzz challenge that you can compare to your own. If you can do it, then you’re ready to get the most out of this book. If not, don’t worry! Keep reading anyway. It might take you a little longer, and you might have a harder time understanding a comparison here and there. But if you practice programming by solving small exercises like the ones in this book, you’ll get on track quickly.

Many good resources are available online for practicing your programming skills and getting feedback on your code. Have a look at codewars.com[4] or cyber-dojo.[5] These pages let you train your programming skills in various levels of difficulty. If you have a mathematical background, you’ll find solving the problems of Project Euler[6] quite appealing.

If, on the other hand, you find the FizzBuzz test terribly easy, and your solution compiles and runs within seconds, be aware that you might already know some of the practices that we outline in this book. You can still get something out of it, of course. We’ve made all the comparisons self-contained. So feel free to jump around and skip the parts that you already know.

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

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