Boolean Arguments

Objective: Show how boolean arguments can be used to enable/disable features of a function. Focus on readability.

Note: Some of the concepts below can be extended to arguments that take more than two arbitrary values.

Python

Variant 1:

def foo(*, something_is_on):
    if something_is_on:
        print("Something is ON")
    else:
        print("Something is OFF")

foo(something_is_on=True)
foo(something_is_on=False)

Keyword-only arguments are used to enforce readability. Suffix like _is_on may feel verbose but provides clarity.

Variant 2:

from enum import Enum

class Something(Enum):
    on = "ON"
    off = "OFF"
    
def foo(something: Something):
    if something == Something.on:
        print("Something is ON")
    else:
        print("Something is OFF")

foo(Something.on)
foo(Something.off)

Something enum must be imported together with foo if they happen to be defined externally.

Variant 3:

from typing import Literal

Something = Literal["on", "off"]

def foo(*, something: Something):
    if something == "on":
        print("Something is ON")
    else:
        print("Something is OFF")

foo(something="on")
foo(something="off")

No need of importing Something. Keyword-only arguments are used to enforce readability.

Rust

Using bool variable may not be a good practive. There are keyword arguments, so in invocation it wouldn't be visible what the flag is doing.

enum Something {
    On,
    Off,
}

fn foo(is_something: Something) {

    match is_something {
        Something::On => {
            println!("Something is ON");
        },
        _ => {
            println!("Something is OFF");
        },
    }
}

fn main() {
    foo(Something::On);
    foo(Something::Off);
}

Something enum must be imported together with foo if they happen to be defined externally.

Crystal

Variant 1:

def foo(*, something_is_on : Bool)
  if something_is_on
    puts "Something is ON"
  else
    puts "Something is OFF"
  end
end

foo(something_is_on: true)
foo(something_is_on: false)

Keyword-only arguments are used to enforce readability. Suffix like _is_on may feel verbose but provides clarity.

Variant 2:

enum Something
  On
  Off
end

def foo(*, something : Something)
  if something == Something::On
    puts "Something is ON"
  else
    puts "Something is OFF"
  end
end

foo(something: :On)
foo(something: :Off)

Keyword-only arguments are used to enforce readability. Suffix _is_on not needed and no need of importing Something.

Enum can be reused for other arguments.

Variant 3:

No need of importing Something. Readability is enforced despite of the fact that function is called with positional arguments.

enum Something
  SomethingOn
  SomethingOff
end

def foo(something : Something)
  # fully qualified name is needed here
  if something == Something::SomethingOn
    puts "Something is ON"
  else
    puts "Something is OFF"
  end
end

foo(:SomethingOn)
foo(:SomethingOff)

Implementation of the function and of the enum looks too verbose.

Also note that enums in crystal can be mapped only to integers (no booleans).