Skip to content
tezvyn:

All bites

The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.

4247 bites

Page 191

TypeScript & Web APIs1 min read

Understanding this in TypeScript

In TypeScript, this is determined by how a function is called, not where it is defined; arrow functions capture the enclosing this lexically, and TypeScript adds optional this parameters to type-check the expected context at compile time.

TypeScript & Web APIs2 min read

ES Modules in TypeScript: The Compile-Time Contract

TypeScript ES modules are a compile-time target, not a runtime guarantee. Configure this when targeting modern browsers or Node with native ESM. The footgun is omitting .js extensions in imports, which it requires in ESM output even for .ts source files.

TypeScript & Web APIs2 min read

Ambient Module Declarations: Compile-Time Trust Falls

Ambient module declarations are compile-time trust falls: you tell TypeScript the shape of code it cannot see. Use them for untyped npm packages, CSS imports, or CDN scripts.

Kotlin Variables: `val` for Constants, `var` for Variables
Android & Kotlin2 min read

Kotlin Variables: `val` for Constants, `var` for Variables

In Kotlin, val creates a read-only constant you assign once, like a fixed setting. var creates a mutable variable you can change later. Always prefer val unless you explicitly need to reassign a value to prevent accidental state changes.

Kotlin Null Safety: Catch Nulls at Compile Time
Android & Kotlin2 min read

Kotlin Null Safety: Catch Nulls at Compile Time

Kotlin's type system catches null pointer errors at compile time. Variables are non-nullable by default; you must opt-in to nulls with a ? (e.g., String?). The compiler then forces you to handle the null case. The main footgun is the !! operator.

Kotlin's Control Flow Expressions: `if` and `when`
Android & Kotlin2 min read

Kotlin's Control Flow Expressions: `if` and `when`

In Kotlin, if and when are expressions that return a value. This lets you assign their result directly to a variable, replacing Java's ternary operator. The footgun: when used as an expression, you must have an else branch to cover all cases.

Kotlin Functions: Named, Reusable Code Blocks
Android & Kotlin2 min read

Kotlin Functions: Named, Reusable Code Blocks

A function is a named recipe for your code. You give it ingredients (parameters) and it produces a result. They are used everywhere, from calculating values to handling button clicks.

Kotlin Inheritance: Open for Extension, Closed by Default
Android & Kotlin2 min read

Kotlin Inheritance: Open for Extension, Closed by Default

In Kotlin, classes are final by default. Think of inheritance as an opt-in feature you enable with the open keyword. Use it to create specialized versions of a base class, like a Student from a Person.

Kotlin Data Classes: Automatic Boilerplate for Data Holders
Android & Kotlin2 min read

Kotlin Data Classes: Automatic Boilerplate for Data Holders

A Kotlin data class automatically generates boilerplate like equals() and toString() for classes that just hold data. Use it for model objects or DTOs where value equality matters. The footgun: its copy() method is shallow, sharing mutable objects.

Android & Kotlin2 min read

Kotlin Collections: Think Transformations, Not Loops

Treat collection operations as a pipeline, not a for loop. Each function (map, filter) transforms data and passes it to the next stage, ideal for turning API data into UI models.

Kotlin Extension Functions: Add Methods Without Inheritance
Android & Kotlin2 min read

Kotlin Extension Functions: Add Methods Without Inheritance

Kotlin extension functions let you add new functionality to existing classes without inheritance, as if you were adding a new method. They're perfect for creating helpers for third-party library classes or framework types like String.

Kotlin Scope Functions: Cleaner Code, Clearer Choices
Android & Kotlin2 min read

Kotlin Scope Functions: Cleaner Code, Clearer Choices

Kotlin's scope functions (let, run, apply) create a temporary workspace for an object, avoiding repetitive variable names. Use them for object configuration or chaining calls.

Kotlin Sealed Classes: Enums for Types
Android & Kotlin2 min read

Kotlin Sealed Classes: Enums for Types

A sealed class is like an enum, but for types. It defines a closed set of subclasses, letting each one carry different data. It's perfect for modeling states like Loading, Success, and Error, ensuring you handle every case at compile time.

Kotlin Coroutines on Android
Android & Kotlin2 min read

Kotlin Coroutines on Android

Kotlin coroutines let Android code write asynchronous logic that reads like sequential code, suspending instead of blocking a thread. Structured concurrency ties each coroutine to a scope, so a destroyed ViewModel cancels its children automatically.

Coroutine Dispatchers: Telling Your Coroutines Which Thread to Use
Android & Kotlin2 min read

Coroutine Dispatchers: Telling Your Coroutines Which Thread to Use

A Dispatcher tells a coroutine which thread or thread pool to use for its work. Use Dispatchers.Default for CPU-intensive tasks. If none is specified, it inherits from its parent. The main footgun: Unconfined can resume on an unexpected thread.

CoroutineScope: The Parent of Your Coroutines
Android & Kotlin2 min read

CoroutineScope: The Parent of Your Coroutines

A CoroutineScope acts as a parent to a group of coroutines, managing their lifecycles. When the parent scope is cancelled, all its children are cancelled too. It's used with builders like launch to group related work, like in an Android ViewModel.

Kotlin Flow: Asynchronous Data Streams
Android & Kotlin2 min read

Kotlin Flow: Asynchronous Data Streams

A Kotlin Flow is like an async Sequence, emitting multiple values over time without blocking. It's used for live data from a database or streaming network responses. The footgun: Flows are cold; the code doesn't run until a collector calls .collect().

Structured Concurrency in Kotlin
Android & Kotlin2 min read

Structured Concurrency in Kotlin

Structured concurrency treats async operations like code blocks: a parent task's lifetime contains its children. In Kotlin, a CoroutineScope ensures if the parent is cancelled, all its child coroutines are too.

Kotlin's Job: A Handle to a Background Task
Android & Kotlin2 min read

Kotlin's Job: A Handle to a Background Task

A Job is a handle to a coroutine, letting you manage its lifecycle. You get one from launch to control background tasks like network calls. The key footgun: a child's *failure* (Exception) cancels its parent, but a child's *cancellation* does not.

Kotlin Coroutines: async/await for Parallel Results
Android & Kotlin2 min read

Kotlin Coroutines: async/await for Parallel Results

async starts a coroutine for a parallel result, returning a Deferred value. await pauses until that result is ready. Use it to run independent network calls concurrently. The footgun: using it for sequential tasks creates a needless race condition.