Try   HackMD

Complex Condition Code Review

Complex If Condition Detected - Or Avoid negative condition [Dangarous]

  • Use var to make the code more readable.
  • Business codes require more readability than performance.
  • Don't use negative condition.
    • Rather using IsNotBlocked use must
      • IsBlocked and then use a function where it returns
      • IsActive () -> !IsBlocked
    • DON'T over complicate codes!
    • Don't negate a negative condition

Images

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Don't negate a negative condition

Bad ❌

var isNotBlocked = false // to check if active if isNotBlocked { // active // in your brain, // you need to think twice } if !isNotBlocked { // not active, meaning blocked // isNotBlocked == active and // inverting it isBlocked // in your brain, // you need to think twice // There is a very good chance of making mistake // Reducing mistake will improve code maintaince }

Good ✔✅ : Always be positive

var isActive = false var isBlocked = !IsActive // to check if active if isActive { // active } // to check if IsBlocked if isBlocked { // blocked }

An Example

FROM : Bad ❌

if command != constants.EmptyString && (strings.HasPrefix(field, constants.Dash) || field == consts.IptablesNegateMatch) { ... }

TO : Good ✔✅ - Always be positive

hasValidCommand := command != constants.EmptyString hasDashPrefix := isCommandEmpty && strings.HasPrefix(field, constants.Dash) hasNegateMatchOnField := field == consts.IptablesNegateMatch hasDashPrefixOrNegateMatch := hasDashPrefix || hasNegateMatchOnField if hasDashPrefixOrNegateMatch { ... }

Refernece (Condition)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →