Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict value is not null coalesced from a false value #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arepnikov
Copy link
Contributor

@arepnikov arepnikov commented Dec 19, 2023

Resolves #28 (Strict value can't be null coalesced from a false value)

Background

Ruby's conditional assignment operator (||=) is typically used for assigning default values to variables:

def some_method(some_variable=nil)
  some_variable ||= "some-default-value"
  # ...
end

However, it has an issue with boolean values:

def some_method(some_boolean_variable=nil)
  some_boolean_variable ||= true
  # ...
end

If false is passed to some_method, the conditional assignment operator will treat false exactly the same as nil. As a result, boolean variables must be given default values differently from other types of values:

def some_method(some_boolean_variable=nil)
  some_boolean_variable = true if some_boolean_variable.nil?
  # ...
end

Proposed Standard: Boolean Attributes With Default Value

Messaging Handle has an boolean attribute strict, which has a default value:

def strict
  @strict ||= Defaults.strict
end
attr_writer :strict

This is a mistake, since the conditional assignment operator can't be used with boolean value. Instead, we propose the following:

def strict
  @strict = Defaults.strict if @strict.nil?
  @strict
end
attr_writer :strict

This proposed standard follows the existing norm for how default boolean values are assigned to local variables in methods.

@arepnikov arepnikov closed this Dec 19, 2023
@arepnikov arepnikov deleted the strict-value-cannot-be-null-coalesced-issues-28 branch December 19, 2023 17:46
@arepnikov arepnikov restored the strict-value-cannot-be-null-coalesced-issues-28 branch December 19, 2023 17:47
@arepnikov arepnikov reopened this Dec 19, 2023
@ntl
Copy link
Contributor

ntl commented Dec 22, 2023

In subst-attr, there's an example of a different form of nil-coalescing for booleans:

def some_method(some_boolean=nil)
  if some_boolean.nil?
    some_boolean = Defaults.some_boolean
  end
  ...
end

Reference: https://github.com/eventide-project/subst-attr/blob/master/lib/subst_attr/substitute.rb#L6-L8

Casing also has yet another form:

def some_method(some_boolean=nil)
  some_boolean = some_boolean.nil? ? false : some_boolean
  ...
end

Reference: https://github.com/eventide-project/casing/blob/master/lib/casing/camel/hash.rb#L5

@ntl
Copy link
Contributor

ntl commented Dec 29, 2023

The strict parameter is under consideration for removal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Strict value can't be null coalesced from a false value
2 participants