idioms

Updating a C# 2.0 events example to be idiomatic with C# 3.5?

I have a short events example from .NET 2.0 that I've been using as a reference point for a while. We're now upgrading to 3.5, though, and I'm not clear on the most idiomatic way to do things. How would this simple events example get updated to reflect idioms that are now available in .NET 3.5? // Args class. public class TickArgs : Eve...

Idiomatic ruby for temporary variables within a method

Within a method, I am using i and j as temporary variables while calculating other variables. What is an idiomatic way of getting rid of i and j once they are no longer needed? Should I use blocks for this purpose? i = positions.first while nucleotide_at_position(i-1) == nucleotide_at_position(i) raise "Assumption violated" if i == 1 ...

What is the idiomatic way of performing an "integer " conversion/typecast in javascript?

Another question asked about the meaning of the code snippet a >>> 0 in Javascript. It turns out that it is a clever way of ensuring that a variable is a unsigned 32-bit integer. This is pretty neat, but I don't like it for two reasons. The intent of the expression is not clear, at least not to me. It does not work for negative nu...

What are good examples of Pythonic libraries?

I'm curious of what other people consider good, Pythonic code. What I mean by Pythonic are libraries that show off good and idiomatic Python code. Adhering to the guidelines of Python. I.e good naming, correct use of modules and generally following best practices and so on. What's your favorite idiomatic Python library? Please provide ...

What's the most idiomatic Clojure way to write this?

I wrote this function that does this (easier to show than explain): (split 2 (list 1 2 3 4 5 6)) => ((1 2) (2 3) (3 4) (4 5) (5 6)) (defn split [n xs] (if (> (count xs) (dec n)) (cons (take n xs) (split n (rest xs))) '())) I understand that in Clojure the list is not the only first class data structure. Would it mak...

Best Loop Idiom for special casing the last element

I run into this case a lot of times when doing simple text processing and print statements where I am looping over a collection and I want to special case the last element (for example every normal element will be comma separated except for the last case). Is there some best practice idiom or elegant form that doesn't require duplicatin...

What is the purpose of an anonymous JavaScript function wrapped in parentheses?

Possible Duplicate: JavaScript: Why the anonymous function wrapper? I read the code of some JavaScript libraries, and found in some js files there are such statements: ;(function(){ })(); I've got a few questions : What the code does? Why the code is designed in such way? How to invoke functions defined in the code? ...

REST idiom for a sub-collection?

My understanding of REST (admittedly limited to pretty much the wikipedia page) is that idiom for GETing a collection is ../resource/ and an item is ../resource/itemId. Is there a standard idiom for GETing for a sub-collection? For example, if the items in the collection have some state toggle (say states A, B, C, D), and I want to be ...

Pythonic way to functions/methods with a lot of arguments

Imagine this: def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta, theta, iota, kappa): pass The line overpass the 79 characters, so, what's the pythonic way to multiline it? ...

how do i make this python code less ugly

First of all python is an awesome language. This is my first project using python and I've made a ridiculous amount of progress already. There's no way that this code below is the best way to do this. What's the most idiomatic way write a class definition? class Course: crn = course = title = tipe = cr_hours = seats = instru...

My Python Program Files cleaner, a better way to write this?

Similar to my other post I have created a new little utility and wanted to get some feedback if there is a way to make my script more Pythonic per say. It is a simple tool for Windows that removes empty directory from the Program Files directory because sometimes programs leave behind an empty directory when they uninstall. import os d...

STL erase-remove idiom vs custom delete operation and valgrind

This is an attempt to rewrite some old homework using STL algorithms instead of hand-written loops and whatnot. I have a class called Database which holds a Vector<Media *>, where Media * can be (among other things) a CD, or a Book. Database is the only class that handles dynamic memory, and when the program starts it reads a file form...

What is the copy-and-swap idiom?

What is this idiom and when should it be used? Which problems does it solve? Will the idiom change when C++0x is used? Although it's been mentioned in many places, we didn't have any singular "what is it" question and answer, so here it is. Here is a partial list of places where it was previously mentioned: What are your favorite C++ ...

Why cast to Closeable first?

While reading some Java source, I came across this line: ((Closeable) some_obj).close(); some_obj is obviously an instance of a class which implements the Closeable interface. My question is, why do they first cast some_obj to Closeable before invoking close(). Couldn't I just do some_obj.close(); ...

API design: is "fault tolerance" a good thing?

I've consolidated many of the useful answers and came up with my own answer below For example, I am writing a an API Foo which needs explicit initialization and termination. (Should be language agnostic but I'm using C++ here) class Foo { public: static void InitLibrary(int someMagicInputRequiredAtRuntime); static void TermLi...

How to name this key-oriented access-protection pattern?

Apparently this key-oriented access-protection pattern: class SomeKey { friend class Foo; SomeKey() {} // possibly non-copyable too }; class Bar { public: void protectedMethod(SomeKey); // only friends of SomeKey have access }; ... doesn't have a known name yet, thus i'd like to find a good one for it so we can refe...

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 }; class Bar { public: void protectedMethod(SomeKey); // only friends of SomeKey have access }; To avoid continued misunderstandings, ...

More idiomatic ruby way to write @var = obj['blah'] unless obj['blah'].nil?

I'm sure there is a more idiomatic ruby way to write the code below: @var = obj['blah'] unless obj['blah'].nil? I've got a whole load of these to do (see below), and there must be a nicer way! @num_x = obj['num_x'] unless obj['num_x'].nil? @num_y = obj['num_y'] unless obj['num_y'].nil? @num_iterations = obj['num_iterations'] unless o...

What's the point of "for x in y" in Ruby?

I'm learning Ruby and RoR at the moment, and I came across this: <% for post in @posts %> in the Rails guide. I'd understood that the idiomatic way to do this in Ruby is with: <% @posts.each do |post| %> If there is a difference then what is it? And if there isn't a difference then wouldn't it be better for the Rails people to be p...

Perl idiom for getting a maximum number of elements in an array

I wanted to chop off all but the first five elements of an array, so I stupidly did: @foo = @foo[ 0 .. 4 ]; and heartily praised my own cleverness. But that broke once @foo ended up with only three elements, because then I ended up with two undefs on the end, instead of a three-element array. So I changed it to: @foo = @foo > 5 ? @fo...