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
27
votes
16 answers

What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience?

What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience? An example: class A { public: char s[1024]; char *p; A::A() { p = s; } void changeS() const { p[0] = 'a'; } }; Even know…
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
26
votes
3 answers

What is the idiomatic Hamcrest pattern to assert that each element of an iterable matches a given matcher?

Examine the following snippet: assertThat( Arrays.asList("1x", "2x", "3x", "4z"), not(hasItem(not(endsWith("x")))) ); This asserts that the list doesn't have an element that doesn't end with "x". This, of course, is the…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
26
votes
1 answer

Will std::string end up being our compile-time string after all?

Many developers and library authors have been struggling with compile-time strings for quite a few years now - as the standard (library) string, std::string, requires dynamic memory allocation, and isn't constexpr. So we have a bunch of questions…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
26
votes
5 answers

Is this big complicated thing equal to this? or this? or this?

Let's say I'm working with an object of class thing. The way I'm getting this object is a bit wordy: BigObjectThing.Uncle.PreferredInputStream.NthRelative(5) I'd like to see if this thing is equal to x or y or z. The naive way to write this…
Joe
  • 3,804
  • 7
  • 35
  • 55
25
votes
8 answers

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…
Tim Gilbert
  • 5,721
  • 5
  • 33
  • 28
25
votes
7 answers

Idiomatic way to create n-ary cartesian product (combinations of several sets of parameters)

To create all possible combinations of two sets of parameters and perform an action on them, you can do: setOf(foo, bar, baz).forEach { a -> setOf(0, 1).forEach { b -> /* use a and b */ } } However, if you have (potentially many)…
Erik
  • 4,305
  • 3
  • 36
  • 54
25
votes
4 answers

idiomatic C for const double-pointers

I am aware that in C you can't implicitly convert, for instance, char** to const char** (c.f. C-Faq, SO question 1, SO Question 2). On the other hand, if I see a function declared like so: void foo(char** ppData); I must assume the function may…
jwd
  • 10,837
  • 3
  • 43
  • 67
24
votes
4 answers

Python nested looping Idiom

I often find myself doing this: for x in range(x_size): for y in range(y_size): for z in range(z_size): pass # do something here Is there a more concise way to do this in Python? I am thinking of something along the lines…
cacti
  • 475
  • 3
  • 15
23
votes
2 answers

What is the template idiom?

I was reading this and was trying to understand what N3601 was about. It said this idiom comes up a lot in a web search, but I couldn't find anything. What is the template idiom, what does it solve, how is it used, what is are…
Jorge Ortega
  • 291
  • 1
  • 4
  • 11
23
votes
6 answers

Idiomatic Ruby filter for nil-or-empty?

I'm looking for a more idiomatic way to filter out nil-or-empty elements of an array. I have many methods of the form: def joined [some_method, some_other_method].compact.reject(&:empty?).join(' - ') end This will take the result of some_method…
benizi
  • 4,046
  • 5
  • 28
  • 25
23
votes
2 answers

ddply + summarize for repeating same statistical function across large number of columns

Ok, second R question in quick succession. My data: Timestamp St_01 St_02 ... 1 2008-02-08 00:00:00 26.020 25.840 ... 2 2008-02-08 00:10:00 25.985 25.790 ... 3 2008-02-08 00:20:00 25.930 25.765 ... 4 2008-02-08 00:30:00 25.925…
Reuben L.
  • 2,806
  • 2
  • 29
  • 45
22
votes
4 answers

Idiomatic use of parentheses in Ruby

array.include? 'foo' or array.include? 'bar' is a syntax error (unexpected keyword_or). Parentheses solve the problem, but as I'm new to Ruby I've no idea which of the following is considered more idiomatic: Option 1 array.include?('foo') or…
davidchambers
  • 23,918
  • 16
  • 76
  • 105
22
votes
5 answers

How do I use Python to easily expand variables to strings?

What's a nice idiom to do this: Instead of: print "%s is a %s %s that %s" % (name, adjective, noun, verb) I want to be able to do something to the effect of: print "{name} is a {adjective} {noun} that {verb}"
Lionel
  • 3,188
  • 5
  • 27
  • 40
22
votes
2 answers

Idiomatically gather results from a dict of futures

I'm trying to write something as idiomatic as possible to gather results from futures stored in a dict. Let's imagine I have the following code: import asyncio async def sleep(seconds): print(f'sleeping for {seconds} seconds') await…
jordixou
  • 331
  • 3
  • 8
22
votes
9 answers

Pythonic way to write 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?
Htechno
  • 5,901
  • 4
  • 27
  • 37