idioms

New Styles in C#

Fluent APIs are very common these days. Lately, I'm finding them in almost every system I work with. Mostly, they enhance readability but sometimes they lock me in to inflexible specifications, making understanding the runtime behavior of the specification that they build almost impossible. Is there a consensus on how to create a good fl...

When to use each of T[], List<T>, IEnumerable<T>?

I usually find myself doing something like: string[] things = arrayReturningMethod(); int index = things.ToList<string>.FindIndex((s) => s.Equals("FOO")); //do something with index return things.Distinct(); //which returns an IEnumerable<string> and I find all this mixup of types/interface a bit confusing and it tickles my potential p...

Is returning nil from a [[class alloc] init] considered good practice?

And is it a common idiom in Objective-C. I've only seen this used on [[NSImage alloc] initWithContentsOfFile: str] and it always make me think there is a memory leak, because i called alloc and the mantra is: "Call alloc and call you must call release" - unless its one of the cases where you don't need too. ...

Idiomatic way to store a single, "first-class" list in MongoDB?

I have a special list (a sort of queue, as in the data structure, not as in a work queue) that I want to store in MongoDB. I need to access and manipulate this single list often in my application - and I don't have several of the same type of list. It would be easiest to store it in a single document, but the problem I'm having is figur...

Python idiom: List comprehension with limit of items

I'm basically trying to do this (pseudo code, not valid python): limit = 10 results = [xml_to_dict(artist) for artist in xml.findall('artist') while limit--] So how could I code this in a concise and efficient way? The XML file can contain anything between 0 and 50 artists, and I can't control how many to get at a time, and AFAIK, the...

using idioms in vim for perl

I have the perl-support plugin enabled. now, I tried the \idd idiom shortcut which would give you a my ($,$); statement with the cursor placed on the first var. Now the second var is displayed as <+name+>. In effect the my line after entering the first variables name would be my ( $top, $<+name+> ); If it was a code snippet I could ...

How do I cache a method with Ruby/Rails?

I have an expensive (time-consuming) external request to another web service I need to make, and I'd like to cache it. So I attempted to use this idiom, by putting the following in the application controller: def get_listings cache(:get_listings!) end def get_listings! return Hpricot.XML(open(xml_feed)) end When I call get_listin...

x or y: acceptable idiom, or obfuscation?

I have to extract values from a variable that may be None, with some defaults in mind. I first wrote this code: if self.maxTiles is None: maxX, maxY = 2, 2 else: maxX, maxY = self.maxTiles Then I realized I could shorten it to: maxX, maxY = self.maxTiles if self.maxTiles is not None else (2, 2) But then I realized this migh...

Is there a better way to write this named_scope? [Rails]

I am using this named_scope to search for products that have a description matching any word the user inputs. E.g., Product.description_like_any("choc pret") Will return products with names like "Chocolate Bar" "Chocolate Covered Pretzels" "Miniature Chocolate Ponies" Here's the named_scope I've written (which works) named_scope :...

Ruby's tap idiom in Python

There is a useful Ruby idiom that uses tap which allows you to create an object, do some operations on it and return it (I use a list here only as an example, my real code is more involved): def foo [].tap do |a| b = 1 + 2 # ... and some more processing, maybe some logging, etc. a << b end end >> foo => [1] With Rails...

Python style: for-in syntax, checking for empty lists and dictionaries.

I'm new to python and haven't yet read a lot of code to verify which styles are considered 'pythonic'. As I've started to code, I've been using this pattern alot. listThatMightBeEmpty = [] for items in listThatMightBeEmpty: print "this may or may not print but the loop won't cause any errors" I assume that it would be redundant t...

Idiomatic R method for "left joining" two data frames

I have two data frames that both have a column containing a factor like the following: > head(test.data) var0 var1 date store 1 109.5678 109.5678 1990-03-30 Store1 2 109.3009 108.4261 1990-06-30 Store1 3 108.8262 106.2517 1990-09-30 Store1 4 108.2443 108.6417 1990-12-30 Store1 5 109.5678 109.5678 1991-03-30 Store1 6 109...

ruby default argument idiom

What's the idiom in Ruby when you want to have a default argument to a function, but one that is dependent on another parameter / another variable? For example, in Python, an example is: def insort_right(a, x, lo=0, hi=None): if hi is None: hi = len(a) while lo < hi: mid = (lo+hi)//2 if x < a[mid]: hi = m...

Is the Perl Goatse 'Secret Operator' efficient?

The "goatse operator" or the =()= idiom in Perl causes an expression to be evaluated in list context. An example is: my $str = "5 and 4 and a 3 and 2 1 BLAST OFF!!!"; my $count =()= $str =~ /\d/g; # 5 matches... print "There are $count numbers in your countdown...\n\n"; As I interprete the use, this is what happens: $str =~ /\d/g ...

Learning simple programming conventions

Where can I learn more about simple programming conventions and design patterns? When I say simple I mean which is the preferred way of writing the following equivalent functions: function() { if (condition) { # condition wraps the entire function } } or function() { if (!condition) { return; } # rest of the funct...

.Net idioms learned through experience?

I'm new to .Net world, my primary language was C++ where there are lot of idioms that help code better(RAII,PIMPL...). What are the common idioms available for c#. ...