idioms

What C++ idioms should C++ programmers use?

Question What C++ idioms should C++ programmers know? By C++ idioms, I mean design patterns or way of doing certain things that are only applicable for C++ or more applicable for C++ than most other languages. Edit: Please explain why one should use the idioms and what the idioms do. ...

What's the idiomatic way to turn a single char into a string in Java?

Right now I'm using the following to minimize boxed object creation: String myString = "" + myChar; Is this the idiomatic way to do it? (IMHO it feels a little awkward.) ...

Ruby setter idiom

I'm working on a Chart class and it has a margin parameter, that holds :top, :bottom, :right and :left values. My first option was to make margin a setter and set values like this: # Sets :left and :right margins and doesn't alter :top and :bottom chart.margins = {:left => 10, :right => 15} It's nice, because it is clearly a setter, ...

Setting variables with blocks in ruby

I find myself using PHP-like loops a lot in Ruby and it feels wrong when the rest of the language is so neat. I wind up with code like this: conditions_string = '' zips.each_with_index do |zip, i| conditions_string << ' OR ' if i > 0 conditions_string << "npa = ?" end # Now I can do something with conditions string I feel like I...

Just-In-Time Derivation

There's a less common C++ idiom that I've used to good effect a few times in the past. I just can't seem to remember if it has a generally used name to describe it. It's somewhat related to mixins, CRTP and type-erasure, but is not specifically any of those things. The problem is found when you want to add some implementation to a clas...

Python - Idiom to check if string is empty, print default

heya, I'm just wondering, is there a Python idiom to check if a string is empty, and then print a default if it's is? (The context is Django, for the __unicode__(self) function for UserProfile - basically, I want to print the first name and last name, if it exists, and then the username if they don't both exist). Cheers, Victor ...

Python Idiom. Need Clarification.

From http://jaynes.colorado.edu/PythonIdioms.html "Build strings as a list and use ''.join at the end. join is a string method called on the separator, not the list. Calling it from the empty string concatenates the pieces with no separator, which is a Python quirk and rather surprising at first. This is important: stri...

Is there an idiomatic way to specify default values for optional parameters in Ruby?

Is there a more concise and idiomatic way to write the following code, which is used to specify default values for optional parameters (in the params/options hash) to a method? def initialize(params={}) if params.has_key? :verbose @verbose = params[:verbose] else @verbose = true # this is the default value end end I wou...

Named Parameter idiom in Java

How to implement Named Parameter idiom in Java? (especially for constructors) I am looking for an Objective-C like syntax and not like the one used in JavaBeans. A small code example would be fine. Thanks. ...

Idiom vs. pattern

In the context of programming, how do idioms differ from patterns? I use the terms interchangeably and normally follow the most popular way I've heard something called, or the way it was called most recently in the current conversation, e.g. "the copy-swap idiom" and "singleton pattern". The best difference I can come up with is code w...

Is there a better alternative to this Ruby idiom?

I'm finding myself writing this bit of code in my controllers a lot: params[:task][:completed_at] = Time.parse(params[:task][:completed_at]) if params[:task][:completed_at] Don't get hung up on what I'm doing here specifically, because the reasons change every time; but there are many circumstances where I need to check for a value in...

List of idiomatic word pairs

I remember seeing somewhere a dictionary of idiomatic word pairs for use in programming. Like get-set, open-close, allocate-free and so on. Does anyone remember an URL? ...

Listing elegant C++ idioms

I just came across this post and found a very elegant way of initializing maps http://stackoverflow.com/questions/138600/initializing-a-static-stdmapint-int-in-c/1730798#1730798 So i wanted to start a thread in which we can list such idioms. They surely make you appreciate the power and expressiveness of C++ ...

Is there any metaprogramming patterns catalog for Python?

I have just read Python Cookbook. The book is amazing. I think the best use of this book is that it provides lots of examples that show python in real problem applications. Many of the idioms include metaprogramming techniques. I wonder if there is any catalog that summarizes metaprogramming idioms in Python? Python Cookbook is very ...

python style question around reading small files

What is the most pythonic way to read in a named file, strip lines that are either empty, contain only spaces, or have # as a first character, and then process remaining lines? Assume it all fits easily in memory. Note: it's not tough to do this -- what I'm asking is for the most pythonic way. I've been writing a lot of Ruby and Java ...

R - idiomatic way to deal with lists of data frames

I have 30 runs of data, each stored in a separate CSV file, runi.csv, i = 0:29. Let's say I want to collect them all into a list. Best way I know how to do this is runs = list() for (i in 1:30) { runs[[i]] = read.csv(paste("run", i-1, ".csv")); } Now let's further say that each of these data frames stored in the list has the same co...

Define concise F# class with post constructor logic

For this class definition: type Foo(f1: int, f2: string) = member x.F1 = f1 member x.F2 = PostProcess f2 Will PostProcess (some string manipulation function) gets called every time f2 is accessed? If the answer is yes and I want to avoid it, what’s the right idiom? Is this one below recommended? It is a bit too verbose for me. type F...

Avoiding unnecessary slice copying in Python.

Is there a common idiom for avoiding pointless slice copying for cases like this: >>> a = bytearray(b'hello') >>> b = bytearray(b'goodbye, cruel world.') >>> a.extend(b[14:20]) >>> a bytearray(b'hello world') It seems to me that there is an unnecessary copy happening when the b[14:20] slice is created. Rather than create a new slice i...

Is the Non-Virtual Interface (NVI) idiom as useful in C# as in C++?

In C++, I often needed NVI to get consistency in my APIs. I don't see it used as much among others in C#, though. I wonder if that is because C#, as a language, offers features that makes NVI unnecessary? (I still use NVI in C#, though, where needed.) ...

Basic C++ Idioms / Techniques

Note: marked as community wiki. In recent days, I've realized how little I know about C++. Besides: using the STL implementing RAII implementing ref-counted smart pointers writing my own policy-based template classes overloading operators << for fun What other techniques are must-know for a good C++ programmer? Thanks! ...