Next.js Discord

Discord Forum

Best practices - if () return

Unanswered
Great Skua posted this in #help-forum
Open in Discord
Great SkuaOP
Hey,
    if (!event) {
        return "Event not found."
    }

VS
    if (!event) return "Event not found."


Can you tell me which one is the better approach? And why?

2 Replies

both does the same thing. At the end, it comes down to code style. Many lint config forces you to add braces whereas others don't
Transvaal lion
It doesn't matter so much but if you have braces on your if clause, you can just add a line for an additional expression. For example, when you have to fire another function call when you don't have the event,
if (!event) {
  emitEventNoutFound()
  return "Event not found."
}

and you can check only one additional line on your commit. That's kind of clear to understand.

However, almost everybody uses AI, at least linter like biome so please do not take this thing so seriously.