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

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

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”))))

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)