short-circuiting

Does Objective-C use short-circuit evaluation?

与世无争的帅哥 提交于 2019-11-27 08:53:07
I tried something along the lines of: if(myString != nil && myString.length) { ... } And got: -[NSNull length]: unrecognized selector sent to instance Does Objective-C not short-circuit after the first condition fails? Objective-C does support short-circuit evaluation, just like C. It seems that in your example myString is NSNull and not nil , therefore myString != nil is true. NSNull is a singleton and is used to represent nil where only objects are allowed, for example in an NSArray. Btw, normally, people write if (!myString && myString.length == 0) . Comparing to nil is quite ugly. Also, I

SQL Server Conditional Flow

泄露秘密 提交于 2019-11-27 08:42:29
If I write two SELECT statements in a IF EXISTS condition with a AND clause in between these select queries, does both queries get executed even if the first SELECT returns false? IF EXISTS (SELECT....) AND EXISTS(SELECT ....) BEGIN END Does the SQL Server Engine execute both the SQL Statement in this scenario? Thanks Krish I would rewrite the test as IF CASE WHEN EXISTS (SELECT ...) THEN CASE WHEN EXISTS (SELECT ...) THEN 1 END END = 1 This guarantees short circuiting as described here but does mean you need to select the cheapest one to evaluate up front rather than leaving it up to the

Conditional execution based on short-circuit logical operation

天涯浪子 提交于 2019-11-27 08:35:08
问题 As the evaluation of logical operators && and || are defined as "short circuit", I am assuming the following two pieces of code are equivalent: p = c || do_something(); and if (c) { p = true; } else { p = do_something(); } given p and c are bool , and do_something() is a function returning bool and possibly having side effects . According to the C standard, can one rely on the assumption the snippets are equivalent? In particular, having the first snippet, is it promised that if c is true,

Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

孤街浪徒 提交于 2019-11-27 08:08:46
问题 So for binary operators on booleans, Java has & , | , ^ , && and || . Let's summarize what they do briefly here: JLS 15.22.2 Boolean Logical Operators &, ^, and | JLS 15.23 Conditional-And Operator && JLS 15.24 Conditional-Or Operator || For & , the result value is true if both operand values are true ; otherwise, the result is false . For | , the result value is false if both operand values are false ; otherwise, the result is true . For ^ , the result value is true if the operand values are

Is there actually a reason why overloaded && and || don't short circuit?

廉价感情. 提交于 2019-11-27 06:21:28
The short circuiting behaviour of the operators && and || is an amazing tool for programmers. But why do they lose this behaviour when overloaded? I understand that operators are merely syntactic sugar for functions but the operators for bool have this behaviour, why should it be restricted to this single type? Is there any technical reasoning behind this? Eric Lippert All design processes result in compromises between mutually incompatible goals. Unfortunately, the design process for the overloaded && operator in C++ produced a confusing end result: that the very feature you want from && --

How to check for null in Twig?

旧街凉风 提交于 2019-11-27 05:54:58
What construct should I use to check whether a value is NULL in a Twig template? Depending on what exactly you need: is null checks whether the value is null : {% if var is null %} {# do something #} {% endif %} is defined checks whether the variable is defined: {% if var is not defined %} {# do something #} {% endif %} Additionally the is sameas test, which does a type strict comparison of two values, might be of interest for checking values other than null (like false ): {% if var is sameas(false) %} {# do something %} {% endif %} lax4mike How to set default values in twig: http://twig

Short circuit on |= and &= assignment operators in C#

一曲冷凌霜 提交于 2019-11-27 04:59:44
I know that || and && are defined as short-circuit operators in C#, and such behaviour is guaranteed by the language specification, but do |= and &= short-circuit too? For example: private bool IsEven(int n) { return n % 2 == 0; } private void Main() { var numbers = new int[] { 2, 4, 6, 8, 9, 10, 14, 16, 17, 18, 20 }; bool allEven = true; bool anyOdd = false; for (int i = 0; i < numbers.Length; i++) { allEven &= IsEven(numbers[i]); anyOdd |= !IsEven(numbers[i]); } } When the 9 entry is hit, allEven becomes false, meaning that all subsequent entries are irrelevant - the value of allEven is

Java ternary (immediate if) evaluation

假如想象 提交于 2019-11-27 04:46:06
I can't find the relevant portion of the spec to answer this. In a conditional operator statement in Java, are both the true and false arguments evaluated? So could the following throw a NullPointerException Integer test = null; test != null ? test.intValue() : 0; Michael Myers Since you wanted the spec, here it is (from §15.25 Conditional Operator ? : , the last sentence of the section): The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression. Michał Króliczek I know it is old post, but look at very similar case and then vote me :P

Short circuiting statement evaluation — is this guaranteed? [C#]

泪湿孤枕 提交于 2019-11-27 03:51:26
问题 Quick question here about short-circuiting statements in C#. With an if statement like this: if (MyObject.MyArray.Count == 0 || MyObject.MyArray[0].SomeValue == 0) { //.... } Is it guaranteed that evaluation will stop after the "MyArray.Count" portion, provided that portion is true? Otherwise I'll get a null exception in the second part. 回答1: Yes, this is guaranteed. C# Language Specification - 7.11 Conditional logical operators: The && and || operators are called the conditional logical

VBA Short-Circuit `And` Alternatives [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-11-27 02:04:15
This question already has an answer here: AndAlso/OrElse in VBA 7 answers VBA doesn't short-circuit VBA does not support short-circuiting - apparently because it only has bitwise And/Or/Not etc operations. From the VBA language specification : "Logical operators are simple data operators that perform bitwise computations on their operands." In this light, it makes sense that VBA was designed with true = &H1111 and false = &H0000 : this way logical statements can be evaluated as bitwise operations. The lack of short-circuiting can cause problems Performance: the ReallyExpensiveFunction() will