Top "Idioms" questions

A programming idiom is the usual and customary way to write code in a particular language.

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

Given the following code, what does the if __name__ == "__main__": do? # Threading example import time, thread def myfunction(string, sleeptime, …

python namespaces main python-module idioms
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 …

javascript function syntax idioms
How do I reverse an int array in Java?

I am trying to reverse an int array in Java. This method does not reverse the array. for(int i = 0; …

java arrays idioms idiomatic
How can I loop through a C++ map of maps?

How can I loop through a std::map in C++? My map is defined as: std::map< std::string, …

c++ loops dictionary iteration idioms
Python idiom to return first item or None

I'm sure there's a simpler way of doing this that's just not occurring to me. I'm calling a bunch of …

python idioms python-2.4
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? …

python dictionary min minimum idioms
What is the pythonic way to detect the last element in a 'for' loop?

I'd like to know the best way (more compact and "pythonic" way) to do a special treatment for the last …

for-loop python idioms fencepost
Python: most idiomatic way to convert None to empty string?

What is the most idiomatic way to do the following? def xstr(s): if s is None: return '' else: …

string python idioms
String concatenation with Groovy

What is the best (idiomatic) way to concatenate Strings in Groovy? Option 1: calculateAccountNumber(bank, branch, checkDigit, account) { bank + branch + checkDigit + …

string groovy idioms string-concatenation gstring
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 …

c++ design-patterns idioms factory-method