All Questions

5652
votes
30answers
830261 views

var functionName = function() {} vs function functionName() {}

I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent. The previous developer uses two way...
3464
votes
20answers
1454737 views

What does if __name__ == "__main__": do?

What does the if __name__ == "__main__": do? # Threading example import time, thread def myfunction(string, sleeptime, lock, *args): while True: lock.acquire() time.sleep(sleepti...
2763
votes
44answers
986060 views

How to merge two dictionaries in a single expression?

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The update() method would be what I need, if it returned its result instead of mod...
136
votes
8answers
16879 views

What is the "Execute Around" idiom?

What is this "Execute Around" idiom (or similar) I've been hearing about? Why might I use it, and why might I not want to use it?
240
votes
10answers
119698 views

How to implement the factory method pattern in C++ correctly

There's this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don't know how to do it, even though it sounds simple: How do I implement Factory M...
266
votes
7answers
405868 views

How to loop through a C++ map of maps?

How would I loop through a std::map in C++? My map is defined as: std::map< std::string, std::map<std::string, std::string> > For example, this holds data like this: m["name1"]["value1...
23
votes
3answers
3235 views

Can we increase the re-usability of this key-oriented access-protection pattern?

Can we increase the re-usability for this key-oriented access-protection pattern: class SomeKey { friend class Foo; // more friends... ? SomeKey() {} // possibly non-copyable too };...
77
votes
9answers
10551 views

Is there a downside to adding an anonymous empty delegate on event declaration?

I have seen a few mentions of this idiom (including on SO): // Deliberately empty subscriber public event EventHandler AskQuestion = delegate {}; The upside is clear - it avoids the need to check f...
46
votes
5answers
13919 views

LBYL vs EAFP in Java?

I was recently teaching myself Python and discovered the LBYL/EAFP idioms with regards to error checking before code execution. In Python, it seems the accepted style is EAFP, and it seems to work wel...
158
votes
13answers
97949 views

Get the key corresponding to the minimum value within a dictionary

If I have a Python dictionary, how do I get the key to the entry which contains the minimum value? I was thinking about something to do with the min() function... Given the input: {320:1, 321:0, 32...

Previous Next