O'Reilly logo

Swift OS X Programming for Absolute Beginners by Wallace Wang

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter    21

Defensive Programming

Programmers tend to be optimistic because when they write code, they assume that it will work correctly. However, it’s often better to be more pessimistic when it comes to programming. Instead of assuming your code will work the first time, it’s safer to assume your code won’t work at all. This forces you to be extra careful when writing Swift code to make sure it does exactly what you want and doesn’t do anything unexpected.

No program is error-free. Rather than waste time hunting down problems, it’s far better to anticipate them and do your best to prevent them from creeping into your code in the first place. While it’s not possible to write error-free code 100 percent of the time, the more defensive you ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required