One Off the Slack: Are Constant Keys OK?

Colton Idle asked:

I’ve seen this pattern cropping up in some compose repos from others… thoughts? At first glance I guess it’s kinda clever for trying to get something to launch only once, but it just seems bad

LaunchedEffect(key1 = "launch_once") {
    myViewModel.doSomething()
  }

ste pointed out that this is equivalent to LaunchedEffect(Unit), which Google’s Adam Powell echoed:

Any constant will do but Unit is more or less what we standardized on. true had some support on the team for a while

LaunchedEffect(Unit) is meant to look suspicious in the same way while (true) looks suspicious. The lack of a “real” key there is supposed to invite closer reading

we’ve had some discussion around whether this has been effective or if it’s just kinda noisy. The discussion it invites I think is sort of anecdata suggesting some positive results

and in the case of the example that started the thread, the key should probably be myViewModel 🙂


Read the original thread in the kotlinlang Slack workspace. Not a member? Join that Slack workspace here!