Chapter 9. Reduce your code: Use less, do more with class detergent

 

Congratulations! You’ve finished all the beginner’s topics, and now you move on to intermediate topics. In this first intermediate-level chapter, you learn about classes and how they make your code cleaner, shorter, and easier to reuse.

This chapter covers

  • What is a class?
  • Why would I use a class, anyway?
  • What are the benefits of classes?
  • What are initializers?
  • How do classes inherit from their parent classes?
  • What is evolution?
  • What’s scope and how does it apply to your code?
  • What’s the difference between declaring, defining?

Milestone 9, Reduce your code, is where things get a little more advanced. Classes make your code more portable and easier to maintain.

Reduce and clean your code with classes

I’ll start by giving you a bird’s-eye view of what classes are and then show you how to code them.

Warning

The topic of classes isn’t simple. This chapter has been made easy to understand to help you learn. But some information on classes isn’t included or explained in-depth, either because I teach it to you later or you don’t need to know it yet.

What is a class?

Think of a class as a collection of related functions and variables, with special features, that are organized into a bundle. Let me give you an example.

To understand the concept of a class, think about what the word person means. We’re all different in lots of ways—skin color, height, age, hair color, gender, and more—but still, each one of us is a person.

Create a class

Like father, like son: Inheritance

All about scope

Calculator app

Check your app knowledge

App-Exercise: Metric Conversion

Extra app-exercise: Metric Conversion app, Part 2