Questions tagged [idioms]

A programming idiom is the usual and customary way to write code in a particular language. Idioms are highly recognizable ways of overcoming a particular limitation and/or writing commonly-used code, sometimes with a purpose that is separate from the literal meaning of the code. An idiom can also be the standard way of implementing something when there are multiple ways to do it.

From Wikipedia:

A programming idiom is a means of expressing a recurring construct in one or more programming languages. Generally speaking, a programming idiom is an expression of a simple task, algorithm, or data structure that is not a built-in feature in the programming language being used, or, conversely, the use of an unusual or notable feature that is built into a programming language.

Something is usually described as being of "idiomatic use" when the construct is common, brief and "comes naturally" to the user of the programming language.

1207 questions
32
votes
1 answer

Seeking constructive criticism on monad implementation

I'm learning monads, this is my first working one (aside from the trivial monad). Feel free to criticize everything in it ruthlessly. I'm especially interested in "more idiomatic" and "more elegant" kind of responses. This monad counts the number of…
abesto
  • 2,331
  • 16
  • 28
32
votes
12 answers

How do I manipulate $PATH elements in shell scripts?

Is there a idiomatic way of removing elements from PATH-like shell variables? That is I want to take PATH=/home/joe/bin:/usr/local/bin:/usr/bin:/bin:/path/to/app/bin:. and remove or replace the /path/to/app/bin without clobbering the rest of the…
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
31
votes
6 answers

PHP - best way to initialize an object with a large number of parameters and default values

I'm designing a class that defines a highly complex object with a ton (50+) of mostly optional parameters, many of which would have defaults (eg: $type = 'foo'; $width = '300'; $interactive = false;). I'm trying to determine the best way to set up…
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
31
votes
5 answers

What's the closest thing in C++ to retroactively defining a superclass of a defined class?

Suppose I have the class class A { protected: int x,y; double z,w; public: void foo(); void bar(); void baz(); }; defined and used in my code and the code of others. Now, I want to write some library which could very well…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
30
votes
8 answers

Is there a tutorial that teaches common Ruby programming idioms used by experienced programmers, but may not be obvious to newcomers?

I'm looking for a Ruby's equivalent of Code Like a Pythonista: Idiomatic Python Desirable features: easy to read single document which covers all topics: tips, tricks, guidelines, caveats, and pitfalls size less than a book idioms should work out…
jfs
  • 399,953
  • 195
  • 994
  • 1,670
30
votes
3 answers

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…
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
30
votes
18 answers

Famous eponymous programming techniques

In some sports certain techniques or elements are named after the athlete who invented or first performed them—for example, Biellmann spin. Is their widespread use of such names for programming techniques and idioms? What are they? To be clear, I…
Alice
  • 2,291
  • 2
  • 15
  • 11
30
votes
5 answers

Best practice for long string literals in Go

I've got a long string literal in Go: db.Exec("UPDATE mytable SET (I, Have, Lots, Of, Fields) = ('suchalongvalue', 'thisislongaswell', 'ohmansolong', 'wowsolong', 'loooooooooooooooooooooooooong')") I see two ways to make this more manageable: raw…
joshlf
  • 21,822
  • 11
  • 69
  • 96
30
votes
4 answers

The preferred way to set matplotlib figure/axes properties

Say I have a matplotlib axes called ax, and I want to set several of its properties. Currently, I do it like this: ax.set_yscale('log') ax.set_xlim([0,10]) ax.set_xlabel('some label') But it gets tedious after a while. Then I ran into this…
jarondl
  • 1,593
  • 4
  • 18
  • 27
29
votes
3 answers

Ruby: Finding most recently modified file

What's an idiomatic way to find the most recently modified file within a directory?
Mike
  • 19,267
  • 11
  • 56
  • 72
28
votes
10 answers

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…
camdez
  • 1,614
  • 1
  • 18
  • 18
28
votes
24 answers

In what contexts do programming languages make real use of an Infinity value?

So in Ruby there is a trick to specify infinity: 1.0/0 => Infinity I believe in Python you can do something like this float('inf') These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use…
Chris Lloyd
  • 12,100
  • 7
  • 36
  • 32
28
votes
5 answers

Is static_cast(-1) the right way to generate all-one-bits data without numeric_limits?

I'm writing C++ code in an environment in which I don't have access to the C++ standard library, specifically not to std::numeric_limits. Suppose I want to implement template constexpr T all_ones( /* ... */ ) Focusing on unsigned…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
28
votes
5 answers

Properly terminating program. Using exceptions

Question: Is using exceptions the proper way to terminate my program if all I want is to display an error message and close (accounting that I may be deep in the program)? Or can I just explicitly call something like exit(EXIT_FAILURE) instead? What…
Programmer001
  • 2,240
  • 2
  • 19
  • 35
27
votes
3 answers

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…
dawg
  • 98,345
  • 23
  • 131
  • 206