Skip to content

Go "useless if/else" example is wrong #213

@josharian

Description

@josharian

One of the rewrites in https://github.com/getgrit/stdlib/blob/main/.grit/patterns/go/useless_if_else_body.md is wrong.

It rewrites

if ($conditon) { $body } else { $body }

to

if ($conditon) { $body }

Oops. (Also, condition is misspelled, and Go doesn't support using integers as booleans in conditions. But those are less important.)


The other rewrites is only sometimes sound. Rewriting

if ($conditon) { $body } else if ($conditon) { $body }

to

if ($conditon) { $body }

is unsafe if evaluating the condition has side-effects.

Given GritQL's goals, that might be ok. But it might be worth a sentence in the docs there?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions