<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Usba Usman</title>
    <description>The latest articles on DEV Community by Usba Usman (@usba_usman).</description>
    <link>https://dev.to/usba_usman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4199488%2Fb1383546-8312-4ef1-8626-cb93cf33de5b.jpg</url>
      <title>DEV Community: Usba Usman</title>
      <link>https://dev.to/usba_usman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/usba_usman"/>
    <language>en</language>
    <item>
      <title>The Kotlin Feature That Changes How You Think About Writing Safer Code</title>
      <dc:creator>Usba Usman</dc:creator>
      <pubDate>Thu, 22 Oct 2026 03:59:48 +0000</pubDate>
      <link>https://dev.to/usba_usman/the-kotlin-feature-that-changes-how-you-think-about-writing-safer-code-21ea</link>
      <guid>https://dev.to/usba_usman/the-kotlin-feature-that-changes-how-you-think-about-writing-safer-code-21ea</guid>
      <description>&lt;p&gt;Null safety looks like a small language feature, but understanding it can completely change how you handle missing data, avoid bugs, and write more reliable applications.&lt;/p&gt;

&lt;p&gt;There's a particular kind of programming bug that's incredibly frustrating.&lt;/p&gt;

&lt;p&gt;Your application works perfectly during testing. The buttons respond, the screens load, and everything seems fine.&lt;/p&gt;

&lt;p&gt;Then someone uses it with slightly different data.&lt;/p&gt;

&lt;p&gt;Maybe a user hasn't uploaded a profile picture. Maybe an API returns an empty field. Or perhaps a database doesn't contain the information your code expects.&lt;/p&gt;

&lt;p&gt;Suddenly, something crashes.&lt;/p&gt;

&lt;p&gt;The problem isn't necessarily complicated. Sometimes, your program simply assumed a value would always exist.&lt;/p&gt;

&lt;p&gt;This is one of the reasons I find Kotlin interesting.&lt;/p&gt;

&lt;p&gt;Instead of leaving developers to remember every possible null check, Kotlin makes missing values part of the language's type system.&lt;/p&gt;

&lt;p&gt;It sounds like a small improvement.&lt;/p&gt;

&lt;p&gt;But once you understand why it works, you start thinking differently about how software should behave.&lt;/p&gt;

&lt;p&gt;The Problem Isn't Null. It's Our Assumptions.&lt;/p&gt;

&lt;p&gt;Imagine you're building an application that displays a user's profile.&lt;/p&gt;

&lt;p&gt;Every user must have a name, but adding a nickname is optional.&lt;/p&gt;

&lt;p&gt;In many programming languages, it's easy to write code assuming both values will always exist.&lt;/p&gt;

&lt;p&gt;Something like:&lt;/p&gt;

&lt;p&gt;val nickname: String? = null&lt;br&gt;
println(nickname.length)&lt;/p&gt;

&lt;p&gt;If you're new to Kotlin, you might expect this to run and fail when it reaches the second line.&lt;/p&gt;

&lt;p&gt;It doesn't.&lt;/p&gt;

&lt;p&gt;Kotlin refuses to compile it.&lt;/p&gt;

&lt;p&gt;The question mark in String? means the variable is allowed to contain either a string or null.&lt;/p&gt;

&lt;p&gt;Because Kotlin knows the value might be missing, it won't let you access .length directly.&lt;/p&gt;

&lt;p&gt;Compare that with:&lt;/p&gt;

&lt;p&gt;val name: String = "Alex"&lt;br&gt;
println(name.length)&lt;/p&gt;

&lt;p&gt;This works because name is declared as a non-nullable string.&lt;/p&gt;

&lt;p&gt;Kotlin's compiler understands the difference.&lt;/p&gt;

&lt;p&gt;That's the first important lesson: your code should express what data is allowed to be missing, rather than hoping every value will be available.&lt;/p&gt;

&lt;p&gt;A Question Mark Can Replace a Lot of Defensive Code&lt;/p&gt;

&lt;p&gt;Let's return to our optional nickname.&lt;/p&gt;

&lt;p&gt;Suppose you want to display its length when it exists.&lt;/p&gt;

&lt;p&gt;You could write:&lt;/p&gt;

&lt;p&gt;val nickname: String? = "Alex"&lt;/p&gt;

&lt;p&gt;if (nickname != null) {&lt;br&gt;
    println(nickname.length)&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;There's nothing wrong with that approach.&lt;/p&gt;

&lt;p&gt;In fact, explicit conditions are often easier to understand when you're learning.&lt;/p&gt;

&lt;p&gt;But Kotlin provides a shorter alternative:&lt;/p&gt;

&lt;p&gt;println(nickname?.length)&lt;/p&gt;

&lt;p&gt;This is called a safe call.&lt;/p&gt;

&lt;p&gt;If nickname contains a string, Kotlin returns its length.&lt;/p&gt;

&lt;p&gt;If it's null, the expression returns null instead of attempting to access a property on a missing object.&lt;/p&gt;

&lt;p&gt;Now imagine you're working with information returned by an API.&lt;/p&gt;

&lt;p&gt;A user might have an address, and that address might have a city.&lt;/p&gt;

&lt;p&gt;You could access it like this:&lt;/p&gt;

&lt;p&gt;val city = user.address?.city&lt;/p&gt;

&lt;p&gt;If address is nullable and happens to be missing, the expression safely returns null.&lt;/p&gt;

&lt;p&gt;Of course, this doesn't solve every problem with incomplete data.&lt;/p&gt;

&lt;p&gt;But it removes a category of assumptions that developers would otherwise have to manage manually.&lt;/p&gt;

&lt;p&gt;Sometimes You Need a Fallback, Not Another Null&lt;/p&gt;

&lt;p&gt;Here's where Kotlin becomes especially practical.&lt;/p&gt;

&lt;p&gt;Returning null is safe, but it isn't always useful.&lt;/p&gt;

&lt;p&gt;Suppose you're displaying a greeting.&lt;/p&gt;

&lt;p&gt;If the user has a nickname, you want to use it. Otherwise, their regular name is perfectly fine.&lt;/p&gt;

&lt;p&gt;Kotlin has an operator for exactly this situation.&lt;/p&gt;

&lt;p&gt;val nickname: String? = null&lt;br&gt;
val name = "Alex"&lt;/p&gt;

&lt;p&gt;val displayName = nickname ?: name&lt;/p&gt;

&lt;p&gt;println("Welcome, $displayName!")&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;Welcome, Alex!&lt;/p&gt;

&lt;p&gt;The ?: symbol is called the Elvis operator.&lt;/p&gt;

&lt;p&gt;It means: use the value on the left if it isn't null; otherwise, use the value on the right.&lt;/p&gt;

&lt;p&gt;This is useful for things like optional descriptions, configuration settings, and default display values.&lt;/p&gt;

&lt;p&gt;But there's something worth understanding before using it everywhere.&lt;/p&gt;

&lt;p&gt;Not every missing value should be replaced with a default.&lt;/p&gt;

&lt;p&gt;If an API doesn't return the user's account balance, replacing that value with zero could display incorrect financial information.&lt;/p&gt;

&lt;p&gt;If a delivery address is missing, inventing a default address would be even worse.&lt;/p&gt;

&lt;p&gt;Sometimes the correct response is to show an error or ask for the missing information.&lt;/p&gt;

&lt;p&gt;Good programming isn't just about preventing crashes.&lt;/p&gt;

&lt;p&gt;It's about deciding what the application should do when reality doesn't match your expectations.&lt;/p&gt;

&lt;p&gt;Let the User Make Mistakes Without Breaking Everything&lt;/p&gt;

&lt;p&gt;Consider another everyday scenario.&lt;/p&gt;

&lt;p&gt;You're building a simple expense calculator.&lt;/p&gt;

&lt;p&gt;The user enters a quantity, and your program needs to convert it into a number.&lt;/p&gt;

&lt;p&gt;A beginner might write:&lt;/p&gt;

&lt;p&gt;val quantity = input.toInt()&lt;/p&gt;

&lt;p&gt;This works when the input is something like "5".&lt;/p&gt;

&lt;p&gt;But what happens when someone enters "five"?&lt;/p&gt;

&lt;p&gt;Kotlin throws a NumberFormatException.&lt;/p&gt;

&lt;p&gt;Instead, you can use:&lt;/p&gt;

&lt;p&gt;val quantity = input.toIntOrNull()&lt;/p&gt;

&lt;p&gt;Now invalid input produces null.&lt;/p&gt;

&lt;p&gt;You can handle that result deliberately:&lt;/p&gt;

&lt;p&gt;fun calculateTotal(input: String): String {&lt;br&gt;
    val quantity = input.toIntOrNull()&lt;br&gt;
        ?: return "Please enter a valid number"&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (quantity &amp;lt;= 0) {
    return "Quantity must be greater than zero"
}

val price = 100
return "Total: ${quantity * price}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Notice what's happening here.&lt;/p&gt;

&lt;p&gt;The program isn't silently ignoring bad input.&lt;/p&gt;

&lt;p&gt;It's explaining the problem to the user.&lt;/p&gt;

&lt;p&gt;For a small example, that might not seem significant. In an actual application, however, handling unexpected values properly can make the difference between a confusing failure and a useful experience.&lt;/p&gt;

&lt;p&gt;The Dangerous Shortcut Beginners Should Understand&lt;/p&gt;

&lt;p&gt;While learning Kotlin, you'll probably encounter code like this:&lt;/p&gt;

&lt;p&gt;val username: String? = null&lt;br&gt;
println(username!!.length)&lt;/p&gt;

&lt;p&gt;The double exclamation mark, !!, tells Kotlin to treat the value as non-null.&lt;/p&gt;

&lt;p&gt;If the value actually is null, the code throws a NullPointerException.&lt;/p&gt;

&lt;p&gt;In other words, you're overriding the compiler's protection.&lt;/p&gt;

&lt;p&gt;There are situations where developers use !! because they have a strong guarantee that a value exists.&lt;/p&gt;

&lt;p&gt;But it's worth being cautious with it.&lt;/p&gt;

&lt;p&gt;If you find yourself adding !! simply to make compiler errors disappear, there's probably a better way to handle the data.&lt;/p&gt;

&lt;p&gt;Try an explicit null check, a safe call, or a meaningful fallback instead.&lt;/p&gt;

&lt;p&gt;The point of learning Kotlin isn't to find shortcuts around its safety features.&lt;/p&gt;

&lt;p&gt;It's to understand why those protections exist.&lt;/p&gt;

&lt;p&gt;Safer Code Starts With Better Data Models&lt;/p&gt;

&lt;p&gt;One of the most useful habits you can develop is deciding which fields are genuinely optional before writing application logic.&lt;/p&gt;

&lt;p&gt;Consider this simple Kotlin data class:&lt;/p&gt;

&lt;p&gt;data class UserProfile(&lt;br&gt;
    val id: Int,&lt;br&gt;
    val name: String,&lt;br&gt;
    val nickname: String?,&lt;br&gt;
    val bio: String?&lt;br&gt;
)&lt;/p&gt;

&lt;p&gt;This model communicates several things immediately.&lt;/p&gt;

&lt;p&gt;Every profile must have an ID and name.&lt;/p&gt;

&lt;p&gt;A nickname and biography are optional.&lt;/p&gt;

&lt;p&gt;Anyone reading the code can understand those rules without searching through dozens of functions.&lt;/p&gt;

&lt;p&gt;And Kotlin will help enforce them.&lt;/p&gt;

&lt;p&gt;This matters as projects grow.&lt;/p&gt;

&lt;p&gt;A small application might contain only a few classes. A larger application can involve data coming from APIs, databases, user input, and external libraries.&lt;/p&gt;

&lt;p&gt;When your types accurately describe that data, other developers have fewer assumptions to make.&lt;/p&gt;

&lt;p&gt;There's one subtle point here: Kotlin's null safety isn't a replacement for validation.&lt;/p&gt;

&lt;p&gt;An empty string isn't the same as null.&lt;/p&gt;

&lt;p&gt;A negative quantity isn't necessarily valid just because it has the correct type.&lt;/p&gt;

&lt;p&gt;And when Kotlin interacts with Java code, some null-related risks still require special attention.&lt;/p&gt;

&lt;p&gt;The language helps you express your expectations, but you still have to decide whether incoming data makes sense.&lt;/p&gt;

&lt;p&gt;How I'd Recommend Learning This Properly&lt;/p&gt;

&lt;p&gt;Reading about a programming concept is useful.&lt;/p&gt;

&lt;p&gt;Actually using it makes the difference.&lt;/p&gt;

&lt;p&gt;If you're learning Kotlin, try building something small enough that you can deliberately experiment with missing values.&lt;/p&gt;

&lt;p&gt;A simple user profile application works well.&lt;/p&gt;

&lt;p&gt;Start by creating a UserProfile data class with a required name and optional nickname.&lt;/p&gt;

&lt;p&gt;Next, write a function that displays the nickname when available and uses the name otherwise.&lt;/p&gt;

&lt;p&gt;Then add a biography field and make your application display "No bio added yet" when appropriate.&lt;/p&gt;

&lt;p&gt;Finally, test your code with different combinations of missing information.&lt;/p&gt;

&lt;p&gt;You don't need Android Studio or an entire mobile application for this exercise. A simple Kotlin console program is enough.&lt;/p&gt;

&lt;p&gt;For anyone starting from the basics, Codevra has a structured Kotlin course that introduces variables, types, null safety, functions, classes, collections, and other topics through examples and exercises.&lt;/p&gt;

&lt;p&gt;It's useful to work through these concepts in order, but don't stop at reproducing the examples.&lt;/p&gt;

&lt;p&gt;Change their inputs. Remove values. Try invalid data. Predict what will happen before running the program.&lt;/p&gt;

&lt;p&gt;That's where the understanding starts to become practical.&lt;/p&gt;

&lt;p&gt;Writing Less Code Isn't Always the Goal&lt;/p&gt;

&lt;p&gt;Kotlin is often praised for its concise syntax.&lt;/p&gt;

&lt;p&gt;And yes, writing fewer lines can be satisfying.&lt;/p&gt;

&lt;p&gt;But fewer lines don't automatically mean better code.&lt;/p&gt;

&lt;p&gt;This:&lt;/p&gt;

&lt;p&gt;val result = value?.trim()?.takeIf {&lt;br&gt;
    it.isNotEmpty()&lt;br&gt;
} ?: "Unknown"&lt;/p&gt;

&lt;p&gt;might be perfectly reasonable once you're comfortable with Kotlin.&lt;/p&gt;

&lt;p&gt;For someone encountering safe calls, lambdas, and the Elvis operator for the first time, however, a longer version with explicit conditions might be easier to follow.&lt;/p&gt;

&lt;p&gt;Readability matters.&lt;/p&gt;

&lt;p&gt;There's no prize for fitting an entire function onto one line.&lt;/p&gt;

&lt;p&gt;Choose the version that communicates your intentions clearly, especially when you're working with other developers.&lt;/p&gt;

&lt;p&gt;You can always simplify your code later when you understand what it's doing.&lt;/p&gt;

&lt;p&gt;Final Thoughts&lt;/p&gt;

&lt;p&gt;One of the biggest changes that comes with programming experience is learning to question your assumptions.&lt;/p&gt;

&lt;p&gt;What happens if this field is missing?&lt;/p&gt;

&lt;p&gt;What if the user enters the wrong value?&lt;/p&gt;

&lt;p&gt;What if an external service returns something unexpected?&lt;/p&gt;

&lt;p&gt;What should the application do next?&lt;/p&gt;

&lt;p&gt;Kotlin's null safety encourages you to ask those questions before a problem reaches the user.&lt;/p&gt;

&lt;p&gt;It won't eliminate every bug, and it certainly won't replace careful testing.&lt;/p&gt;

&lt;p&gt;But it offers something valuable: a way to make important assumptions visible in your code.&lt;/p&gt;

&lt;p&gt;And once you start thinking about missing data as something to design for, rather than something to fix after an application crashes, you begin writing better software.&lt;/p&gt;

&lt;p&gt;Not necessarily more complicated software.&lt;/p&gt;

&lt;p&gt;Just software that's more prepared for the real world.&lt;/p&gt;

&lt;p&gt;`&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;If you want to explore Kotlin beyond the examples in this article, these resources are worth bookmarking:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.codevra.org/kotlin" rel="noopener noreferrer"&gt;Kotlin Tutorial for Beginners&lt;/a&gt; — A structured course covering Kotlin fundamentals, null safety, functions, classes, coroutines, and more, with examples, exercises, and quizzes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://kotlinlang.org/docs/null-safety.html" rel="noopener noreferrer"&gt;Official Kotlin Documentation — Null Safety&lt;/a&gt; — A detailed explanation of nullable types, safe calls, the Elvis operator, and other language features.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.android.com/codelabs/basic-android-kotlin-compose-nullability" rel="noopener noreferrer"&gt;Google Android Developers — Use Nullability in Kotlin&lt;/a&gt; — A hands-on introduction to working with nullable values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://kotlinlang.org/docs/basic-syntax.html" rel="noopener noreferrer"&gt;Kotlin Basic Syntax Guide&lt;/a&gt; — A useful reference for learning and revisiting Kotlin fundamentals.&lt;br&gt;
`&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>kotlin</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
