All Questions

1882
votes
30answers
102935 views

"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 to always ...
93
votes
7answers
20821 views

PHP global in functions

What is the utility of the global keyword? Are there any reasons to prefer one method to another? Security? Performance? Anything else? Method 1: function exempleConcat($str1, $str2) { return ...
235
votes
5answers
85978 views

How does "this" keyword work within a function?

I just came across an interesting situation in JavaScript. I have a class with a method that defines several objects using object-literal notation. Inside those objects, the this pointer is being us...
228
votes
11answers
89743 views

Function overloading by return type?

Why don't more mainstream statically typed languages support function/method overloading by return type? I can't think of any that do. It seems no less useful or reasonable than supporting overload ...
72
votes
16answers
47132 views

Why do we need break after case statements?

Why doesn't the compiler automatically put break statements after each code block in the switch? Is it for historical reasons? When would you want multiple code blocks to execute?
113
votes
8answers
20671 views

Why are arrays covariant but generics are invariant?

From Effective Java by Joshua Bloch, Arrays differ from generic type in two important ways. First arrays are covariant. Generics are invariant. Covariant simply means if X is subtype of Y then X[] w...
61
votes
6answers
9893 views

Can't set attributes of object class

So, I was playing around with Python while answering this question, and I discovered that this is not valid: o = object() o.attr = 'hello' due to an AttributeError: 'object' object has no attribute...
116
votes
14answers
50760 views

Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

Java doesn't allow multiple inheritance, but it allows implementing multiple interfaces. Why?
97
votes
5answers
11372 views

Why are C# 3.0 object initializer constructor parentheses optional?

It seems that the C# 3.0 object initializer syntax allows one to exclude the open/close pair of parentheses in the constructor when there is a parameterless constructor existing. Example: var x = new...
107
votes
21answers
74876 views

Why can't variable names start with numbers?

I was working with a new c++ developer a while back when he asked the question: "Why can't variable names start with numbers?" I couldn't come up with an answer except that some numbers can have text...

Previous Next