Kotlin elvis operator vs "... == true" boolean expression check preformance - performance

Assume we have nullable val a: Boolean?.
In code we want to assign value of a to another non-nullable variable val b: Boolean.
If a is true then we want b also to be true
If a is false or null the we want b to be false.
We can do this in 2 ways:
b = a ?: false
or
b = a == true
Which of the following approaches is better in terms of performance?

Related

SpEL - Test multiple conditions in findAll

I am writing a SpEL condition.
It should be a basic IF, THEN, ELSE condition.
Currently it is this way:
cus_FinalCostBoolean == true ? FormDetails.findAll{cus_ForceRecommended == true}.min{cus_LastNegotiationRound} : FormDetails.findAll{cus_ForceRecommended == false}.min{cus_LastNegotiationRound}
where:
cus_FinalCostBoolean is a boolean variable
cus_ForceRecommended is a boolean variable
cus_LastNegotiationRound is a decimal number
Currently what this condition does is the following:
IF cus_FinalCostBoolean IS TRUE
THEN calculate min(cus_LastNegotiationRound) among all the entries that have cus_ForceRecommended = TRUE
ELSE calculate min(cus_LastNegotiationRound) among all the entries that have cus_ForceRecommended = FALSE
I'd like editing it the following way:
IF cus_FinalCostBoolean IS TRUE
THEN calculate min(cus_LastNegotiationRound) among all the entries that have cus_ForceRecommended = TRUE and cus_TechnicalEvaluationPassed = 'OK'
ELSE calculate min(cus_LastNegotiationRound) among all the entries that have cus_ForceRecommended = FALSE and cus_LastNegotiationRound is not null
where cus_TechnicalEvaluationPassed is a text variable
I was trying to editing it this way:
cus_FinalCostBoolean == true ? FormDetails.findAll{cus_ForceRecommended == true && cus_TechnicalEvaluationPassed == 'OK'}.min{cus_LastNegotiationRound} : FormDetails.findAll{cus_ForceRecommended == false && cus_LastNegotiationRound != null}.min{cus_LastNegotiationRound}
But it seems it does not work.
Could you help me?
Thank you.
Best regards.

How to alter Boolean value from true to false and vice-versa

I have a requirement in Go of altering the bool value and store it.
I am receiving value == true but I need to alter it and store the altered value.
I can think of only storing the alerted to a var and pass it in the next statement.
Eg psuedo code:
chnagevalue := false
if value == false {
changevalue == true
}
what's the best way to do it in Go? Is there any pre-defined way to do it?
Use the logical NOT operator ! to change true to false and vice versa:
changedValue := !value
There's a short answer, written somewhere else :-)
Yours is almost good too:
changedValue := false
if !value {
changedValue == true
}
An if statement is always about something being true.
so in the above it reads : if a value equals false is true then {}.
of course your mind reads it the short way :
if a value equals false then {}.
the switch of course works the best:
changedValue := !changedValue
BTW: I would never use a fieldname like "changedValue" because...
every variable is a value of some kind, so there is no need of writing that.
"changed" should be enough.
or, when it is a boolean like this , even better:
"isChanged" or "hasChanged"

Stuck on Exercise 28 of Learn Ruby Hard Way - different returns in irb

Currently doing exercise 28 link here: http://learnrubythehardway.org/book/ex28.html
Under 'Common Student Questions', there's the following:
Question
Why does "test" && "test" return "test" or 1 && 1 return 1 instead of true?
Answer
Ruby and many languages like to return one of the operands to their boolean expressions rather than just true or false. This means that if you did false && 1 you get the first operand (false) but if you do true && 1 your get the second (1). Play with this a bit.
However, even after reading the answer above, I'm still having trouble understanding why
false && 1 returns 'false'
while
true && 1 returns '1'
in irb.
Foreword: in Ruby just false itself and nil are false. Everything else is evaluated to true.
false && 1
e1 and e2 have both to be true to make the entire expression evaluate to true. e1 is false so there is no need to evaluate e2. e1 (false) is returned.
true && 1
e1 is true so we need to evaluate even e2 and e2 is 1. 1 is returned.

Can I shortcut-check if a variable is `nil` and replace by a default value?

Is there a way to shortcut-check if a variable is nil, and give a default value if it is? For instance, replace
result = (var == nil ? defaultvalue : var)
with something like
result = selfifnotnil(var, default)
Of course I could write selfifnotnil function like the ternary above, but is there any built in option?
It's as simple as this (assuming that false and nil are treated the same)
result = var || defaultvalue
If false is a legitimate value (not a missing one), then you have to do that ternary.
result = var.nil? ? defaultvalue : var
Since nil is a falsely value. Therefore:
result = var || defaultvalue
Or if it is to check the var itself and assign default value. Also, if you are from another language, be careful which values are false in ruby.
result ||= default

RecordBTN.enabled == false swift

//result of call to '==' is unused
What does that mean and how can i fix it?
RecordBTN.enabled == false
The compiler is telling me type of expression is ambiguous without more content.
var recordSettings: [String: AnyObject] = [AVFormatIDKey : kAudioFormatAppleLossless, AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue, AVEncoderBitRateKey : 320000, AVNumberOfChannelsKey : 2, AVSampleRateKey : 44100.0 ]
In your statement RecordBTN.enabled == false, you are comparing RecordBTN.enabled to false, but you aren't doing anything with the result of the comparison (a Boolean value). You may want to store the result in a variable or use it in an if statement..
If you are trying to set RecordBTN.enabled to false, use the assignment operator (a single'='):
RecordBTN.enabled = false
The operators are well documented.

Resources