Good code is short, simple, and symmetrical—the challenge is figuring out how to get there.

Sean Parent

Chapter 2
Level Up Your Code Style

In role-playing games, no matter whether they’re digital or tabletop, you usually play a character with a unique set of skills. As the game progresses, your character moves through the game world, interacts with its inhabitants, and gathers experience through quests. Eventually, your character will gain new skills and “level up” her skills to a new a stage of mastery, becoming faster, stronger, smarter, or whatever it was you were practicing.

Programming is similar to playing games, and it can be just as addictive. It’s real life, so you don’t control a character—you are in the game. You move through the digital world and gather experience. You discover distant places of programming languages with very different language paradigms. You meet fellow programmers who have different viewpoints on the most intricate aspects of style, such as whether to put a curly bracket in a new line. You practice your skills by programming, learning new frameworks or language concepts that you were unaware of, and sharpening your understanding of the ones you already know. Eventually, you rise to a new level of proficiency in programming.

This chapter is a level up from the more basic items we discussed in the previous chapter. We’ll go through a number of more advanced concepts of the Java language, as well as aspects of code style.

You’ll learn how to deal with magic numbers—either by replacing them with constants or, even better, with enums—and you’ll see why it makes sense to use newlines for grouping code. On top of that, we’ll show you why you should rely on the Java API as often as possible, and we’ll go over several typical pitfalls you should avoid when you do so. You’ll see how to best iterate over data structures without triggering unwanted exceptions or causing performance problems. Lastly, we’ll provide you with a handy method for formatting strings in your code. So let’s get to the next level!

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

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