Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Stop Calling Kotlin Flows Hot and Cold

Member-only storySam CooperFollowBetter Programming--ShareMaybe you’ve heard Kotlin programmers say that “channels are hot, flows are cold.”It’s a useful distinction between two working methods with an asynchronous data stream. But it’s also a limited analogy that doesn’t do a good job of capturing the nuance and flexibility of Kotlin’s Flow API.How can we improve the vague metaphor into something more concrete and actionable?🔥 We call channels “hot” because they’re stateful objects. A channel is a communication mechanism that lets you receive values from other computations. As the consumer, your interaction with the channel doesn’t necessarily control when that computation starts and stops.Think of it like the moving escalator on the subway. It’s operating before you start using it and will most likely continue after you leave.❄️ Flows are called “cold” because they don’t hold state. When you pass a flow around in your Kotlin code, the flow isn’t holding or producing any data. That’s because a Flow object isn’t an active instance of a data stream. Instead, each time you call collect you create a new, ephemeral instance of the flow’s computation that only exists inside that function call.If a channel is like the moving escalator in the subway station, a flow is more like the elevator. It only starts operating when you start interacting with it and stops again as soon as you leave.This distinction is fine when dealing with the most basic channels and flows. But there’s more than one way to create a Flow, and if you’ve spent much time working with them, you’re probably already picking holes in my oversimplified explanation.“What about hot flows?” you ask.We started with the simple statement that flows are cold. “Hot flow” is a newer term used to describe flows backed by some active computation…----Better ProgrammingProgrammer, linguist and writer.Sam CooperinBetter Programming--Sergei SavvovinBetter Programming--13Dmitry KruglovinBetter Programming--39Sam CooperinBetter Programming--2Jacob Bartlett--2Tippu Fisal Sheriff--Sevban Bayır--shivakumar--1Sam CooperinBetter Programming--6SUMIT KUMAR--1HelpStatusWritersBlogCareersPrivacyTermsAboutText to speechTeams



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Stop Calling Kotlin Flows Hot and Cold

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×