if-statement
How can I simplify this set of if statements? (Or, what’s making it feel so awkward?)
One reason it looks like a lot of code is that it is very repetitive. Use variables to store the parts that are repeated, and that will help with the readability: private List<Foo> parseResponse(Response<ByteString> response) { Status status = response.status(); int code = status.code(); boolean payloadAbsent = !response.payload().isPresent(); if (code != Status.OK.code() || payloadAbsent) { … Read more
Most efficient way to compare a variable to multiple values?
Here’s a way in C++11, using std::initializer_list: #include <algorithm> #include <initializer_list> template <typename T> bool is_in(const T& v, std::initializer_list<T> lst) { return std::find(std::begin(lst), std::end(lst), v) != std::end(lst); } with that, you can do: if (is_in(num, {1, 2, 3})) { DO STUFF } It is not very efficient though when not used with built-in types. int … Read more
How to make this Block of python code short and efficient
There’s a trade-off between short and efficient. The Short way is if all(n % i == 0 for i in range(2, 21)): The Efficient way is to notice that things like n % 20 == 0 also mean that n % f == 0 where f is any factor of 20. For example, you can … Read more
VBA takes wrong branch at If-statement – severe compiler bug?
This bug is not present on 32-bit, but it seems to be present in 64-bit VBA-capable applications (I’ve tried Excel, Word, and AutoCAD). Since the question already covers what happens if the Object does not get terminated or if there is no Class_Terminate event, the following examples all use an Object that will surely go … Read more
Why is an empty string literal treated as true?
A condition is considered “true” if it evaluates to anything other than 0*. “” is a const char array containing a single \0 character. To evaluate this as a condition, the compiler “decays” the array to const char*. Since the const char[1] is not located at address 0, the pointer is nonzero and the condition … Read more
Why does this usage of C++17 if constexpr fail?
This is not possible outside the template! From cppreference.com Outside a template, a discarded statement is fully checked. if constexpr is not a substitute for the #if preprocessing directive: void f() { if constexpr(false) { int i = 0; int *p = i; // Error even though in discarded statement } } Any idea how … Read more
Why javascript treats 0 equal to empty string? [duplicate]
Quoting the doc (MDN): Equal (==) If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a … Read more
comma operator in if condition
http://en.wikipedia.org/wiki/Comma_operator: In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type). In your first if: if (a, b) a is evaluated first and discarded, b … Read more
Multiple IF statements between number ranges
It’s a little tricky because of the nested IFs but here is my answer (confirmed in Google Spreadsheets): =IF(AND(A2>=0, A2<500), “Less than 500”, IF(AND(A2>=500, A2<1000), “Between 500 and 1000”, IF(AND(A2>=1000, A2<1500), “Between 1000 and 1500”, IF(AND(A2>=1500, A2<2000), “Between 1500 and 2000”, “Undefined”))))