Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Martin Fowler

Chapter 1
Start Cleaning Up

Have you ever looked back at a piece of code you wrote several months ago and wondered what you were thinking? Could any of your colleagues understand it within a reasonable amount of time? Writing readable code is an essential skill for a programmer. In fact, it’s one of your most important communication skills—it’s your public relations agent and your calling card. It says everything people need to know about your skills as a programmer, and it quickly reveals whether you’re a novice or an expert.

The truth is in the code. Forget the developer’s intentions and the promises in the documentation. All a computer can do is what’s instructed in code. The compiler takes the Java code and produces byte code—a machine representation based on a variety of strictly enforced rules. If the code obeys those rules, it compiles. If not, your screen fills with error messages. Simple as that.

Compilers can process any valid piece of Java code, but you can’t say the same for humans. Writing code that’s easy for humans to read and understand is much harder than writing code that compiles. So that’s what we’re starting with in this chapter: writing expert code that’s easy to understand. We’ll show you some tricks that make your code more readable, such as why you should avoid unnecessary comparisons and negations and how to simplify Boolean expressions.

Conveniently, code that’s easy to understand also contains fewer bugs. By the time you finish this chapter, you’ll be able to write better code that contains fewer bugs. You’ll stay clear of NullPointerExceptions in conditional expressions and you’ll be aware of some infamous bug types, such as the switch fallthrough or the GOTO FAIL bug. You’ll also learn about general design principles, such as code symmetry. Let’s get started!

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

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