Skip to Content
The Productive Programmer
book

The Productive Programmer

by Neal Ford
July 2008
Intermediate to advanced
222 pages
6h 29m
English
O'Reilly Media, Inc.
Content preview from The Productive Programmer

Chapter 8. Good Citizenship

image with no caption

Good citizenship may seem like an odd topic in a discussion about how to improve code, but good citizenship refers to objects that are both aware of their own state and considerate of the state of others around them. Although this may seem simple, developers violate this principle all the time by coding on autopilot. This chapter looks at several different violators of good citizenship and ways to become more responsible citizens.

Breaking Encapsulation

One of the core tenets of object-oriented programming is encapsulation: protecting internal fields from outside meddling. Yet, I’ve seen lots of developers void the intent of encapsulation because they code on autopilot.

Here is a scenario. You create a new class, you create a bunch of private member variables for it, you tell the IDE to spit out properties (either get/set methods in Java or properties in C#), and then you engage your brain. Creating public properties for every private field completely destroys the intent of the property mechanism. You might as well make all the member variables public because the properties aren’t helping you at all (and, in fact, they are just making your code more dense for no good reason).

For example, let’s say you have a Customer class that has several address fields (such as the typical addressLine, city, state, zip). If you create mutating properties for each of these ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Functional Thinking

Functional Thinking

Neal Ford
Perfecting Your Thinking Skills

Perfecting Your Thinking Skills

MIT Sloan Management Review
The Hidden Cost of AI-Assisted Creativity

The Hidden Cost of AI-Assisted Creativity

Léonard Boussioux, Anil Doshi, Oliver Hauser, Kartik Hosanagar

Publisher Resources

ISBN: 9780596519780Errata Page