Questions tagged [least-astonishment]

Don't surprise the programmer by behaving/working differently than expected.

15 questions
3352
votes
34 answers

"Least Astonishment" and the Mutable Default Argument

Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function called with no parameter to always return a list with only…
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
34
votes
3 answers

Why is negative id or zero considered a bad practice?

Why is negative id or zero considered a bad practice when inserting a primary key in a database table? I think it could be useful in some cases, but people say that it is not recommended, despite the fact that they never say/know why. So, I was…
12
votes
5 answers

"Boolean" operations in Python (ie: the and/or operators)

This method searches for the first group of word characters (ie: [a-zA-Z0-9_]), returning the first matched group or None in case of failure. def test(str): m = re.search(r'(\w+)', str) if m: return m.group(1) return None The…
NullUserException
  • 83,810
  • 28
  • 209
  • 234
7
votes
2 answers

Reason for allowing Special Characters in Python Attributes

I somewhat accidentally discovered that you can set 'illegal' attributes to an object using setattr. By illegal, I mean attributes with names that can't be retrieve using the __getattr__ interface with traditional . operator references. They can…
Keozon
  • 998
  • 10
  • 25
7
votes
1 answer

Shouldn't using FieldInfo.SetValue to set a ValueType to null fail?

(related to PropertyInfo SetValue and nulls) If I have public class Thing { public int X; }, a Thing o, and a FieldInfo fi that points to the X field, why is it legal to call fi.SetValue(o, null)? The runtime sets the field X to zero, i.e.…
Sebastian Good
  • 6,310
  • 2
  • 33
  • 57
5
votes
1 answer

Default function values in multi-layer architecture

Wondering the best way to set defaults in a multi-layer application structure. Specifically, if a certain work flow requires a nested set of function calls, is the default specified on all the functions, or just on the top level function and passed…
Clay Wardell
  • 14,846
  • 13
  • 44
  • 65
3
votes
2 answers

Make ++o++ complain for types with user defined pre- and postfix increment operators

I'm looking for a way to prevent ++x++ from working for types with user defined prefix and postfix increment operators. For builtin types the result type of the postfix operator is not an lvalue but a prvalue expression and the compilers complain…
jesses
  • 559
  • 3
  • 15
2
votes
1 answer

Oddly Ruby behavior

I need to check if a variable is an array, and if not convert it into one before proceed with further processing. So, my code looks like this: class Test < Struct.new(:args) def eval p "1. #{args}" args = (args.instance_of?…
cheng81
  • 2,434
  • 2
  • 21
  • 18
2
votes
1 answer

Collection initializers inside object initializers with default values

I just stumbled upon the following issue: class Settings { // Let's set some default value: { 1 } public ICollection AllowedIds = new List() { 1 }; } static void Main(string[] args) { var s = new Settings { …
Heinzi
  • 167,459
  • 57
  • 363
  • 519
2
votes
1 answer

Returning `self` at ActiveRecord class method loses indirect reference

When defining a class method at an ActiveRecord, if I return self, the indirect reference is lost. I'm not sure if I am using the right vocabulary, as I am just learning Ruby on Rails, so here is an example: class User < ActiveRecord::Base …
Thiago Negri
  • 5,221
  • 2
  • 28
  • 39
1
vote
1 answer

Should I give the backing beans another name than the class?

Should I give the backing beans a new name in the @Named annotation, or should I use the same as the class for readability? Are there any guidelines on when to not or when to do this? I have a backing bean that provides a dropdown component with…
user626912
  • 2,546
  • 3
  • 24
  • 33
0
votes
0 answers

“Never alter or delete a user’s work without them knowing” - is there a name for this software design principle?

Is there a software design principle that says an application should never alter or delete a user’s work done in the application without them knowing? It’s very similar to the Principle of Least Astonishment, but specifically referring to modifying…
user3163495
  • 2,425
  • 2
  • 26
  • 43
0
votes
0 answers

Mutable vs immutable object behavior in python functions

the two pieces of code below produce different outputs. def f1(x= []): x.append(1) return x print(f1()) print(f1()) Output is: [1] [1, 1] And for the second function: def f2(x=0): x+=1 return x print(f2()) print(f2()) Output…
ste_kwr
  • 820
  • 1
  • 5
  • 21
0
votes
0 answers

Prevent conversion to factor when number of columns in a data.frame can be reduced to one

I have a procedure that can extract items from a data frame based on a list of conditions on the columns (see Extracting items from an R data frame using criteria given as a (column_name = value) list): Here are the data frame and condition list: >…
bli
  • 7,549
  • 7
  • 48
  • 94
0
votes
3 answers

Recurring dates on dates that do not exist

When giving the option for something to reoccur every certain amount of time how should I treat times that don't reoccur on every interval? For example what should happen to birthday reminders for February 29th? Or if I have a monthly appointment on…
Motti
  • 110,860
  • 49
  • 189
  • 262