14.6. Designing Classes for the Real World Is Hard

Designing a class, just like designing a paper form, seems deceptively straightforward. Forms and classes are, by their nature, simplifications of the real-world objects they represent. The question is, how should we simplify these objects? For example, if we’re creating a Customer class, the customer should have a firstName and lastName attribute, right? But actually creating classes to model real-world objects can be tricky. In most Western countries, a person’s last name is their family name, but in China, the fam- ily name is first. If we don’t want to exclude more than one billion potential customers, how should we change our Customer class? Should we change firstName and lastName to givenName and familyName ? But some cultures don’t use family names. For example, former UN Secretary General U Thant, who is Burmese, has no family name: Thant is his given name and U is an initialization of his father’s given name. We might want to record the cus- tomer’s age, but an age attribute would soon become out of date; instead, it’s best to calculate the age each time you need it using a birthdate attribute.

The real world is complicated, and designing forms and classes to cap- ture this complexity in a uniform structure on which our programs can operate is difficult. Phone number formats vary between countries. ZIP codes don’t apply to addresses outside the United States. Setting a maximum num- ber of characters for city names could be a problem for the German hamlet of Schmedeswurtherwesterdeich. In Australia and New Zealand, your legally recognized gender can be X. A platypus is a mammal that lays eggs. A peanut is not a nut. A hotdog might or might not be a sandwich, depending on who you ask. As a programmer writing programs for use in the real world, you’ll have to navigate this complexity.

To learn more about this topic, I recommend the PyCon 2015 talk “Schemas for the Real World” by Carina C. Zona at https://youtu.be/PYYfVqtcWQY/ and the North Bay Python 2018 talk “Hi! My name is . . .” by James Bennett at https://youtu.be/NIebelIpdYk/. There are also popular “Falsehoods Programmers Believe” blog posts, such as “Falsehoods Programmers Believe About Names” and “Falsehoods Programmers Believe About Time Zones.” These blog posts also cover topics like maps, email addresses, and many more kinds of data that programmers often poorly represent. You’ll find a collection of links to these articles at https://github.com/kdeldycke/awesome-falsehood/. Additionally, you’ll find a good example of a poorly executed method of capturing real-world complexity in CGP Grey’s video, “Social Security Cards Explained,” at https:// youtu.be/Erp8IAUouus/.