idioms

Good or Bad C++ Idiom - Objects used purely for constructor/destructor?

I have a few classes which do nothing except in their constructors/destructors. Here's an example class BusyCursor { private: Cursor oldCursor_; public: BusyCursor() { oldCursor_ = CurrentCursor(); SetCursor(BUSY_CURSOR); } ~BusyCursor() { SetCursor(oldCursor_); } } // example of use...

Javascript idiom for code structure

My javascript gets included multiple times with the script tag, like so: <script src="code.js></script> <script src="code.js></script> <script src="code.js></script> Right now I have this code inside code.js to make my code run only once without overwriting my namespaces: if(typeof _ow == "undefined" ){ _ow = {}; // code in here will...

What are the best resources to learn writing idiomatic c#?

I am looking for something like idiomatic ruby for c# programmers It should: be for people that can already programm be terse not be to basic just found a better thread for this question on stackoverflow: c-coding-standard-best-practices ...

Useful mini patterns (not design patterns)

My most used mini pattern is: VideoLookup = new ArrayList { new ArrayList { buttonVideo1, "Video01.flv" }, new ArrayList { buttonVideo2, "Video02.flv" }, new ArrayList { buttonVideo3, "Video03.flv" }, new ArrayList { buttonVideo4, "Video04.flv...

What are the important language features (idioms) of Python to learn early on

I would be interested in knowing what the StackOverflow community thinks are the important language features (idioms) of Python. Features that would define a programmer as Pythonic. Python (pythonic) idiom - "code expression" that is natural or characteristic to the language Python. Plus, Which idioms should all Python programmers lea...

Is "regex" in modern programming languages really "context sensitive grammar"?

Over the years, "regex" pattern matching has been getting more and more powerful to the point where I wonder: is it really just context-sensitive-grammar matching? Is it a variation/extension of context-free-grammar matching? Where is it right now and why don't we just call it that instead of the old, restrictive "regular expression"? ...

Common Ruby Idioms

One thing I love about ruby is that mostly it is a very readable language (which is great for self-documenting code) However, inspired by this question: http://stackoverflow.com/questions/609612/ruby-code-explained and the description of how ||= works in ruby, I was thinking about the ruby idioms I don't use, as frankly, I don't fully g...

Are there any common Design patterns or common idioms that are important for C++ win32 multithreading programming?

One example could be: RAII - Resource Acquisition is Initialization used with critical sections Any others that are important, popular and you often use? ...

How should a custom view update a model object?

This is a Cocoa n00b question - I've been programming GUI applications for years in other environments, but now I would like to understand what is "idiomatic Cocoa" for the following trivialized situation: I have a simple custom NSView that allows the user to draw simple shapes within it. Its drawRect implementation is like this: - (vo...

JavaScript idiom: create a function only to invoke it.

I am learning YUI and have occasionally seen this idiom: <script> (function x(){ do abcxyz})(); </script> Why do they create a function just to invoke it? Why not just write: <script> do abcxyz </script> For example see here. ...

What idiom (if any) do you prefer for naming the "this" parameter to extension methods in C#, and why?

The first parameter to a C# extension method is the instance that the extension method was called on. I have adopted an idiom, without seeing it elsewhere, of calling that variable "self". I would not be surprised at all if others are using that as well. Here's an example: public static void Print(this string self) { if(self != null...

Ruby: How do I invoke a function via an object reference?

Consider this contrived example: # Dispatch on value of fruit_kind: TYPE_A = :apple TYPE_B = :banana TYPE_C = :cherry eating_method = nil case fruit_kind # Methods to use for different kinds of fruit (assume these are # already defined) when TYPE_A then eating_method = bite when TYPE_B then eating_method = peel when TYPE_C...

Ruby: How do I use symbols to represent things in an array?

I have an array of arrays that looks like this: fruits_and_calories = [ ["apple", 100], ["banana", 200], ["kumquat", 225], ["orange", 90] ] I also have a method I want to invoke on each element of the array: fruits_and_calories.each do |f| eat(f[0], f[1]) I'd really like to be able to say something like: fruits_and_calorie...

Expressing quantities with units prettily in Scala

I need support for quantities with units. I'd like the type system to enforce unit correctness as much as possible. For example, it shouldn't be possible to combine grams with dollars. I'm going down the path of parameterized types, but this code seems far more repetitious than Scala code I've seen from others. abstract class UnitOfMea...

How do I write a Ruby method to handle zero, one, or many inputs?

I've got a Ruby method like the following: # Retrieve all fruits from basket that are of the specified kind. def fruits_of_kind(kind) basket.select { |f| f.fruit_type == kind.to_s } end Right now, you can call this like: fruits_of_kind(:apple) # => all apples in basket fruits_of_kind('banana') # => all bananas in basket and s...

Sort a list of objects by using their attributes in Ruby

I have a list of Fruit structs called basket. Each Fruit struct has a name (a string) and a calories (an integer). I would like to sort basket so that: The Fruits with the highest calories appear first. For example, a fruit with 500 calories appears before a fruit with 400 calories. If two Fruits have equal calories, the Fruit whose na...

Is there an idiom in Java for empty methods which exist to satisfy an interface?

Let's say I have a class Foo implementing an interface such as MouseListener. The MouseListener interface consists of five methods but I only wish to override one of them (mouseClicked()). Is there a standard, idiomatic way of formatting the other methods? My inclination was to write the following: @Override public void mouseClicked(...

What is the clojure equivalent of the Python idiom "if __name__ == '__main__'"?

I'm dabbling in clojure and am having a little trouble trying to determine the clojure (and / or Lisp) equivalent of this common python idiom. The idiom is that at the bottom of a python module there is often a bit of test code, and then a statement which runs the code, for example: # mymodule.py class MyClass(object): """Main logi...

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: return s s = xstr(a) + xstr(b) update: I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types besides strings. I'm awfully impressed by Vinay Sajip's lambda suggestion, ...

Avoid deadlocks in a multithreaded process

What are the best practices/idioms should someone follow in order to avoid deadlocks? ...