How to shorten multiple equality checks in logical OR? - objective-c

Is there a way in C/ObjectiveC
to shorten this?
if (a == b || a == c || a == d)
{
}
so that
i would have something like this?
if (a == (b || c || d))
{
}
I know the latter is not correct but what I want something that resembles plain english i.e.
If "a" equals this or this or this...then do something. I find repeating the ==
operator a bit redundant.

If you're working with objective-c objects:
if([#[b,c,d] containsObject:a])
Otherwise, there's no way to simplify your first implementation.

No, you've written it correctly the first time.
Incidentally, shorter code is not necessarily better code. It's often harder to read.

Related

Kotlin's when with Pair - complicated contidions

I have complicated bussiness logic conditions which I want to resolve via when statement.
Let's say I have 3 enums Enum1, Enum2 and Enum3 and every has constans A,B,C,D... etc. And depending on value of Enum1 and Enum2 I need to determine list of Enum3 values. So I have function like this:
fun determineValues(enumPair: Pair<Enum1, Enum2>) : List<Enum3> {
return when(enumPair){
Enum1.A to Enum2.B -> listOf(Enum3.A, Enum3.B)
Enum1.B to Enum2.C -> listOf(Enum3.A, Enum3.C)
//..etc
}
}
So far so good. However I have also condition when let's say for Enum1.C no matter what is in Enum2 I have to return some Enum3 values.
I was trying to add to when conditions like this: Enum1.C to Any -> listOf(...), but it's not working. So I ended up with some ifs before when and it makes my code really less readable (I have a lot of business conditions like this)
So my question is: what is the simplest and cleanest way to do that?
This feature of doing pattern matching in when does not exist yet. Even what you are doing now is not really pattern matching - you're just creating some temporary Pair objects and comparing equality. See this for a discussion about this. For now, the most concise way to do this that I can think of is:
return when{
enumPair == Enum1.A to Enum2.B -> listOf(Enum3.A, Enum3.B)
enumPair == Enum1.B to Enum2.C -> listOf(Enum3.A, Enum3.C)
enumPair.first == Enum1.C -> ...
else -> ...
}

Conditional Action Call

I'm building an application using CRM 2013 Unified Service Desk (USD). Right now, I'm creating a conditional action call from a hosted control to another. So far I got it working with a single expression, like:
"[[Data1]]" == "Value1"
But I've gone further down the rabbit's hole and now I need to query two values with an OR operator:
"[[Data1]]" == "Value1" || "[[Data2]]" == "Value2"
While the first example works fine, using the || operator does not. I've already tried using some variations, like expr OR expr, but the evaluation fails.
There's no documentation on MSDN covering the conditional part of an action call, so I need help figuring this one out.
As you have discovered, USD translates your key's to their text versions and then passes it to the javascript converter.
so :
// where mykey1 = "Test" (string) and mykey2 = true (bool)
// Success condition
"[[mykey1]]" == "Test" || "[[mykey2]]" == "True"
if you were working with a lookup and you want to only run the action if the lookup is pointed at the business unit and the name of the business unit is defaultorg... you would use this:
// where [[$User.businessunitid]] reference to a business unit
"[[$User.businessunitid.logicalname]]" == "businessunit" && "[[$User.businessunitid.name]]" == "defaultorg"
Hope that helps.
So, after a little experimenting, I realized that the issue was not with the logical operators but with a boolean representation instead. true and false are capitalized in USD conditionals:
"[[Data1]]" == "Value1" || "[[Data2]]" == "True"
But the rest of the expression was valid, so you can combine and use logical ORs ("||") and logical ANDs ("&&") just as you'd do in C#.

Objective C : Ternary operator with multiple statements

As the title says i was wondering if there is a way to use ternary operators with multiple statements in Objective C.
I know it can be easily done in some other languages like javascript, php, C etc but i couldn't find a solution for Objective C.
I want to implement something like this:
a > b ? ( statement1, statement2, statement3 ) : ( statement1, statement2 );
Basically i just want to avoid a lot of if-else blocks to maintain better code readability.
Please also suggest if using ternary operators instead of if-else blocks can harm app performance to a noticeable extent.
The Conditional operator ?: is not a replacement for an if/else block. I'm sure you could tweak the logic to make it work, but that would only obscure the meaning more.
My question is, "what are you saving?"
a > b ? ( statement1, statement2, statement3 ) : ( statement1, statement2 );
if (a > b) { statement1; statement2; statement3; } else { statement1; statement2; }
The if/else block is a grand total of 7 characters longer.
An even bigger question is, "Can the logic be composed in a better way?"
Look to see if the flow can be done differently with fewer ifs.
Look for places to create sub-routines.
You can easily do this; just be aware that the ternary operator can only include expressions, including comma expressions. So your statements can only be expressions, assignments, method calls etc. but no if / return / while and so on. And the ternary operator wants a result, so the last expressions in each group must have the same type; you can just add (void) 0 at the end of each list.
That said, you are most definitely not making your code more readable. Everyone reading it will start swearing at you and doubt your mental sanity. So don't do it.
The solution for your question (#user3752049) is:
a > b ? ^{ statement1; statement2; statement3;}() : ^{statement1; statement2;}();
Thanks

Julia: Can conditional statements evaluate code on the same line?

I am reading through the Julia manual right now and ran into my first potential disappointment.
I like being able to code conditional statements tersely. In R I might write:
if (x==y) print("Hello")
In Julia however I think I might need do
if x==y
println("Hello")
end
Or perhaps x==y ? print("Hello") : print("") which is certainly silly.
Is there some formulation in Julia for allowing for single line conditional statements?
You can write if x == y println("Hello") end or, as has become somewhat idiomatic, you can use the short-circuiting behavior of the && operator and write x == y && println("Hello"). In a very similar fashion it is fairly common to check some condition and throw an error if it isn't met by writing something like this: size(A) == size(B) || error("size mismatch").

What advantages are there to using either AND or &&?

Currently, I'm using && and || instead of AND and OR because that's how I was taught. In most languages, however, both are valid syntax. Are there any advantages to one or the other in any language?
I did try to search for this question, but it's a bit hard. It doesn't interpret my input correctly.
You ask “Are there any advantages to one or the other in any language?” This is, of course, dependent on the programming language.
Languages that implement both and and && (and correspondingly or and ||) will do it one of two ways:
Both behave exactly the same way. In which case, there is no advantage provided by the language in using one over the other.
Each behaves differently. In which case, the advantage is that you can get different behaviour by using one or the other.
That all sounds a bit facetious, but it's really as specific as one can get without talking about a specific language. Your question explicitly wants to know about all languages, but it's a question that needs to be answered per language.
Perl has all four of {&& || and or} but they differ in their precedence. "and" and "or" have really low precedence so you can do things like "complex-function-call-here or die $!" and you won't accidentally have "or" slurp up something on its left side that you didn't want it to.
it depends on the language, but on PHP, I'd be careful about using && versus "and". The ones i often use are "&&" and "||"
http://us3.php.net/manual/en/language.operators.logical.php
$g = true && false; // $g will be assigned to (true && false) which is false
$h = true and false; // $h will be assigned to true
In some languages && will have a higher operator precedence than AND.
If both works fine, then I would say it's really personal preference, in most cases, they are compiled into same binary code like this : 11100010001000101001001010 [not real code, just an example].
&& = two keystrokes of the same key.
AND = three keystrokes of different keys.
I'm not sure what language you are using, but some languages differentiate between a normal boolean operator and a short-circuit operator. For example, the following are normal boolean operators in MATLAB:
C = or(A,B);
C = A | B; % Exactly the same as above
However, this is a short-circuit operator:
C = A || B;
The short-circuit syntax will evaluate the first argument and then, depending on the value, will potentially skip over evaluating the second argument. For example, if A is already true, B doesn't have to be evaluated for an OR operation, since the result is guaranteed to be true. This is helpful when B is replaced with a logical operation that involves some kind of expensive computation.
Here's a wikipedia link discussing short-circuit operators and their syntax for a few languages.
Unless there aren't any precedence issues, I'd say there are differences in readability. Consider the following:
if (&x == &y && &y == &z) {
// ..
}
#define AND &&
if (&x == &y AND &y == &z) {
// ..
}