How do I implement a null coalescing operator in SQLAlchemy?

For a simple example of SQLAlchemy’s coalesce function, this may help: Handling null values in a SQLAlchemy query – equivalent of isnull, nullif or coalesce. Here are a couple of key lines of code from that post: from sqlalchemy.sql.functions import coalesce my_config = session.query(Config).order_by(coalesce(Config.last_processed_at, datetime.date.min)).first()

Double question marks (‘??’) vs if when assigning same var

Don’t worry about the performance, it will be negligible. If you are curious about it, write some code to test the performance using Stopwatch and see. I suspect you’ll need to do a few million iterations to start seeing differences though. You can also never assume about the implementation of things, they are liable to … Read more

Is there a more elegant way to add nullable ints?

var nums = new int?[] {1, null, 3}; var total = nums.Sum(); This relies on the IEnumerable<Nullable<Int32>>overload of the Enumerable.Sum Method, which behaves as you would expect. If you have a default-value that is not equal to zero, you can do: var total = nums.Sum(i => i.GetValueOrDefault(myDefaultValue)); or the shorthand: var total = nums.Sum(i => … Read more

PHP short-ternary (“Elvis”) operator vs null coalescing operator

Elvis ?: returns the first argument if it contains a “true-ish” value (see which values are considered loosely equal to true in the first line of the Loose comparisons with == table). Or the second argument otherwise $result = $var ?: ‘default’; // is a shorthand for $result = $var ? $var : ‘default’; Null … Read more