Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Latest commit

 

History

History
55 lines (39 loc) · 729 Bytes

prefer-conditional-expressions.md

File metadata and controls

55 lines (39 loc) · 729 Bytes

Prefer conditional expressions

Has auto-fix

Rule id

prefer-conditional-expressions

Description

Recommends to use a conditional expression instead of assigning to the same thing or return statement in each branch of an if statement.

Example

Bad:

  int a = 0;

  // LINT
  if (a > 0) {
    a = 1;
  } else {
    a = 2;
  }

  // LINT
  if (a > 0) a = 1;
  else a = 2;

  int function() {
    // LINT
    if (a == 1) {
        return 0;
    } else {
        return 1;
    }

    // LINT
    if (a == 2) return 0;
    else return 1;
  }

Good:

  int a = 0;

  a = a > 0 ? 1 : 2;

  int function() {
    return a == 2 ? 0 : 1;
  }