This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

Metadata

ID: ruby-best-practices/no-else-with-unless

Language: Ruby

Severity: Info

Category: Best Practices

Description

The rule “Do not use unless with else” is a coding guideline that promotes clarity and readability in Ruby code. The unless keyword in Ruby is used as the inverse of if, meaning it executes the code block only if the condition is false. When an else statement is added to an unless statement, it can make the logic harder to follow and understand, especially for those new to the language or the codebase.

Why is this important? Readability and clarity are essential for maintaining a healthy codebase. Code is read more often than it is written, and it is crucial that it remains clear to all developers in a team. An unless-else construct can be confusing because it effectively double-negates the logic, making it harder to understand at a glance.

To avoid violating this rule, use an if-else construct instead. It conveys the same logic in a more straightforward manner. The if-else construct is also more common across different programming languages, making it easier for developers with different backgrounds to understand. For example, instead of writing unless condition; else; end, write if !condition; else; end.

Non-Compliant Code Examples

unless success?
  puts 'failure'
else
  # foo
  puts 'success'
end

Compliant Code Examples

if success?
  puts 'failure'
else
  #foo
  puts 'success'
end
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis