kotlin

Snippets

Kotlin Contracts: Teach Your Compiler About Your Function’s Behavior

What Are Kotlin Contracts? If you’ve worked with Kotlin for a while, you’ve likely encountered functions from the standard library that seem to have almost magical powers. Take require() and check()—call them with a condition, and the compiler somehow knows that if execution continues past that line, your variable is no longer nullable. Or use […]

, , , ,
The best of both worlds: The type safety of a wrapper class with the zero-overhead performance of a primitive.
Snippets

Type-Safe Zero-Cost Wrappers with Kotlin Value Classes

The Problem: Primitive Obsession You’ve probably seen this pattern in production code: database IDs represented as Long, user emails as String, currency amounts as Double. On the surface, it’s simple and pragmatic. But it introduces a subtle bug vector—primitive obsession. Imagine you have a function that accepts a user ID and a post ID, both

, , , ,
Refactoring with AI
Articles

LLM Prompts for Code Refactoring: A Practical Guide

LLM Prompts for Code Refactoring: Structuring Your Requests When you’re facing a large refactoring task in your Android codebase, asking an LLM the right way can save you hours. Unlike simple coding questions, refactoring requires your AI assistant to understand the architectural context, the business logic you’re preserving, and the specific constraints of your project.

, , , ,
Code on a dark monitor screen
Tutorials

Kotlin Property Delegates You’re Probably Not Using: observable and vetoable

Why Property Delegates Often Go Unnoticed Most Kotlin developers know lazy — it’s everywhere. You see it in Android ViewModels, dependency injection setups, and library code. But the standard library ships two more property delegates that are surprisingly powerful and almost never discussed: Delegates.observable and Delegates.vetoable. If you’ve been wiring up manual setters or using

, , , , ,
Laptop with Kotlin code open
Tutorials

Lazy Computation in Kotlin: The sequence Builder and How It Can Save Your Memory

The Hidden Cost of Eager Collections Kotlin’s collection functions — map, filter, flatMap — are a joy to use. But they share one characteristic that can quietly hurt your app: they’re eager. Each call processes the entire source and allocates a brand new intermediate list. Chain three or four of them on a list of

, , , , ,
Tutorials

Stop Using mutableListOf + toList(): Meet Kotlin’s buildList, buildMap, and buildSet

A Familiar But Slightly Awkward Pattern If you’ve written Kotlin for a while, you’ve almost certainly written code like this: fun getActiveUsers(users: List): List { val result = mutableListOf() for (user in users) { if (user.isActive) result.add(user) if (user.isPremium) result.add(user.copy(label = “Premium”)) } return result.toList() // convert back to read-only } The pattern works, but

, , , , ,
Tutorials

Functional Error Handling in Kotlin: runCatching and the Result Type

The Problem With Try-Catch Everywhere Exception handling in Kotlin (and Java before it) has always had a composability problem. Once you introduce a try-catch block, you break the expression-oriented flow of your code. You can’t easily chain operations, return from them in one line, or pass the “success or failure” result to another function without

, , , , ,
Tutorials

Stop Using System.currentTimeMillis() for Benchmarking: Kotlin’s measureTimedValue and Duration API

The Old Way: Manual Time Measurement If you’ve ever benchmarked a function in Kotlin or Android, you’ve probably written something like this: val start = System.currentTimeMillis() val result = doExpensiveWork() val elapsed = System.currentTimeMillis() – start Log.d(“Perf”, “doExpensiveWork took ${elapsed}ms, result=$result”) It works, but it’s noisy. You need three lines just to time one call,

, , , , ,
Tutorials

Claude Code for Android Development: Setup, Best Practices & Gotchas

AI-assisted development has moved well beyond autocomplete. Claude Code — Anthropic’s agentic coding tool — can read your project, reason about your architecture, write Kotlin, generate tests, and even help you debug Gradle. But like any powerful tool, it rewards those who know how to use it well. This guide walks you through everything you

, , , , , , ,
Tutorials

Animating the Seasons: Building a Particle System with Android Canvas

One of the most satisfying things to code is a particle system. Snowflakes, falling leaves, fireflies — simple physics, beautiful results. Here’s how Seasons Live Wallpaper handles its snow, and how you can build something similar. The Particle Data Class data class Snowflake( var x: Float, var y: Float, val radius: Float, // visual size

, , , , ,
Kotlin collections and mutability
Articles, Tutorials

Kotlin Mutable and Immutable Collections

In this article I would like to emphasise over a few options that we have when working with data structure and more precisely with collections in a mutable and immutable fashion. So let’s start with a few of them and see their mutability state. But first, what makes a list mutable or immutable? Immutable Collections

, , , ,
Tutorials

Kotlin – for, while, if, when

Control flow: for, while, if, when This post is for beginners who want to start learning Kotlin language and already know Java. We will show simple examples of for, while, if, when in both languages, in parallel, for an easier understanding. We will use Log.e in order to print string messages, as from our point of view,

, , , , , , , , , , ,
Articles

Kotlin – How to pass a function as parameter

Recently, I’ve noticed that there are a few different ways to pass a function as parameter in Kotlin. In this post I will use 2 functions: sum and divide. We have to declare the sum function, in such way that it can receive the divide function as argument. The most common and often more convenient

, , , , , ,
Tutorials

Functions in Kotlin

In this post I will talk about 5 cool things that functions can do in Kotlin, better than methods in Java.   1. Forget about overloaded methods In Java, when you need the same method to have 1 parameter in one case and more parameters in another case, you would have to overload the method.

, , , , , ,
Scroll to Top