Wednesday, July 6, 2016

Andy Pettitte's Nasty Breaking Ball & the Boolean Data Type

True or false? Babe Ruth retired a New York Yankee.

True or false? The Minnesota Vikings drafted Dave Winfield though he never played football.

True or false? If Andy Pettitte has two strikes on a batter, he will throw a slider in the dirt for strike three.

Want the answers? False, Ruth retired a Boston Brave. True, Winfield was drafted in the 17th round by the Vikings. And yes, Andy wants you to chase that nasty slider in the dirt for strike three.

In Swift coding if something is either true or false, it is a data type called a Boolean or a Bool. Why do we call them Booleans/Bools? Because they are named after George Boole, a 19th century English mathematician, philosopher, and logician who specialized in differential equations, analysis, and symbolic logic and was particularly adept at going around and asking people true or false questions because he liked to trick people. OK, not true, but I had to bring it around somehow.

Big Daddy Boole

Why are Bools important?
Bools are important because they help us write functions that either have one outcome or another. There is no gray area with Bools. Something either is true or it is false. If true, one thing happens. If false, something else happens. 

When do we use Bools?
We use Bools when the outcome can only be one of two things: true or false. Is Bryce Harper up next? True or false. Is Dellin Betances warming up in the bullpen? True or false. What are Joe Maddon’s best post-game quotes? Not a true or false question. There could be several answers. Is Joe Maddon a good post-game interview? True or false (though it depends on how much you enjoy or don’t enjoy his post-game interviews which can vary from fan to fan). 


The Andy Pettitte Bool above is an example. Let’s take a look:



In this case there either are two strikes or there are not two strikes. If there are two strikes, andyThrowsANastyBreakingBall. If there are not two strikes, Andy throws something else. Can you argue that Andy can throw a fastball or curve with two strikes? Sure you can, but if the man needed a K, he threw the slider. Plus, this is my Swift playground dammit!




When I first started to code I felt overwhelmed with the number of options for functions and had a hard time writing them because I didn’t even know where to start. When I learned to simplify my thinking, especially in terms of Bools with the question: Are there only two outcomes (true or false) or are there more than two possible outcomes? I had more clarity on when to use Bools and how to structure functions.

So what happens when you have many different possible outcomes?

On Deck: Grady Little, Game 7 of the 2003 ALCS, and The Switch Statement

No comments:

Post a Comment