Is it bad to explicitly compare against boolean constants e.g. if (b == false) in Java?

It’s not necessarily bad, it’s just superfluous. Also, the actual variable name weights a lot. I would prefer for example if (userIsAllowedToLogin) over if (b) or even worse if (flag). As to the performance concern, the compiler optimizes it away at any way. As to the authoritative sources, I can’t find something explicitly in the … Read more

Getting a list of indices where pandas boolean series is True

Using Boolean Indexing >>> s = pd.Series([True, False, True, True, False, False, False, True]) >>> s[s].index Int64Index([0, 2, 3, 7], dtype=”int64″) If need a np.array object, get the .values >>> s[s].index.values array([0, 2, 3, 7]) Using np.nonzero >>> np.nonzero(s) (array([0, 2, 3, 7]),) Using np.flatnonzero >>> np.flatnonzero(s) array([0, 2, 3, 7]) Using np.where >>> np.where(s)[0] … Read more

How do I submit a boolean parameter in Rails?

UPDATE: Rails 5: ActiveRecord::Type::Boolean.new.deserialize(‘0′) UPDATE: Rails 4.2 has public API for this: ActiveRecord::Type::Boolean.new.type_cast_from_user(“0”) # false PREVIOUS ANSWER: ActiveRecord maintains a list of representations for true/false in https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/column.rb 2.0.0-p247 :005 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean(“ON”) 2.0.0-p247 :006 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean(“F”) This is not part of Rails’ public API, so I wrapped it into a helper method: class ApplicationController < ActionController::Base … Read more

PHP: bool vs boolean type hinting

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration Warning Aliases for the above scalar types are not supported. Instead, they are treated as class or interface names. For example, using boolean as a parameter or return type will require an argument or return value that is an instanceof the class or interface boolean, rather than of type bool: <?php function test(boolean $param) {} test(true); ?> … Read more

Get random boolean in Java

I recommend using Random.nextBoolean() That being said, Math.random() < 0.5 as you have used works too. Here’s the behavior on my machine: $ cat myProgram.java public class myProgram{ public static boolean getRandomBoolean() { return Math.random() < 0.5; //I tried another approaches here, still the same result } public static void main(String[] args) { System.out.println(getRandomBoolean()); } … Read more