Programming Kotlin Applications. Бретт Мак-Лахлин
Читать онлайн книгу.of these nuances to Kotlin are keeping type safety intact and making it much harder to break Kotlin at runtime.
Believe it or not, there is a lot more to Kotlin's type safety: things you'll learn in future chapters. Kotlin has a lot of specific rules around null
values, checking for null
, and the idea of null safety: keeping your code free from null pointer exceptions.
NOTE If you're not familiar with null pointers, or have never seen a NullPointerException
in, for example, Java, then count yourself lucky. If you have, though, they're often the source of very difficult bugs. Kotlin attempts to add some extra work at the coding layer in favor of getting rid of those annoying and hard-to-find bugs. It's a trade-off, but one that works quite well for Kotlin.
WRITING CODE IS RARELY LINEAR
If you take a step back and consider what you've done over these last two chapters, it's both cool and potentially a little confusing. The cool part is even if you've never written a line of Kotlin before, you've now got a functioning class that exercises some of Kotlin's key and relatively unique features: objects, construction, and overriding superclasses (more on that later), as well as the somewhat unusual requirements Kotlin enforces around property value assignments.
What could be a bit confusing, though, is how you got to the terse code in Listing 2.10. You added properties, removed them, added a couple of custom mutators, removed those, and ultimately deleted nearly as much code as you added. Wouldn't it have been easier to just jump right to Listing 2.10?
Well, it might have, but it wouldn't have been that helpful in getting you to see how Kotlin works. Arguably just as important, it wouldn't be realistic. Seasoned developers are constantly going through this same process: trying things out, then adding something new, having to go back and change old code to accommodate new code, tweaking, and removing as much as is added. It's common to refactor—to restructure code to make it less redundant, more efficient, more organized, and generally just better.
That's really the point if you're learning Kotlin, too: to get more than just a syntactical understanding. You want to be a good developer? Keep adding things, removing things, and taking a fresh look to make sure your code is the right code.
Конец ознакомительного фрагмента.
Текст предоставлен ООО «ЛитРес».
Прочитайте эту книгу целиком, купив полную легальную версию на ЛитРес.
Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.