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
41
votes
8 answers

Python "Every Other Element" Idiom

I feel like I spend a lot of time writing code in Python, but not enough time creating Pythonic code. Recently I ran into a funny little problem that I thought might have an easy, idiomatic solution. Paraphrasing the original, I needed to collect…
Matt Luongo
  • 14,371
  • 6
  • 53
  • 64
40
votes
12 answers

Idiomatic efficient Haskell append?

List and the cons operator (:) are very common in Haskell. Cons is our friend. But sometimes I want to add to the end of a list instead. xs `append` x = xs ++ [x] This, sadly, is not an efficient way to implement it. I wrote up Pascal's triangle in…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
38
votes
2 answers

What are some GraphQL schema naming best practices?

I'm beginning development on a nontrivial application for which we're considering GraphQL. When working on the initial draft of our schema, I've become a bit paralyzed trying to establish naming conventions that will scale as the product matures. I…
Mike Marcacci
  • 1,913
  • 1
  • 18
  • 24
38
votes
7 answers

when to use if vs elif in python

If I have a function with multiple conditional statements where every branch gets executed returns from the function. Should I use multiple if statements, or if/elif/else? For example, say I have a function: def example(x): if x > 0: …
Sean Geoffrey Pietz
  • 1,120
  • 2
  • 12
  • 17
37
votes
9 answers

Idiomatic construction to check whether a collection is ordered

With the intention of learning and further to this question, I've remained curious of the idiomatic alternatives to explicit recursion for an algorithm that checks whether a list (or collection) is ordered. (I'm keeping things simple here by using…
maasg
  • 37,100
  • 11
  • 88
  • 115
37
votes
2 answers

What does it mean by "#define X X"?

In Linux header file epoll.h, I found the following code: enum EPOLL_EVENTS { EPOLLIN = 0x001, #define EPOLLIN EPOLLIN ... } What does it mean by #define EPOLLIN EPOLLIN?
xmllmx
  • 39,765
  • 26
  • 162
  • 323
37
votes
4 answers

Is there a JavaScript idiom to change "undefined" to "null"?

There's quite a few JavaScript idioms that coerce between types and similar things. ! can convert anything falsey to boolean true, !! can convert anything falsey to actual boolean false, + can convert true, false, or a string representing a number…
hippietrail
  • 15,848
  • 18
  • 99
  • 158
37
votes
10 answers

idioms for returning multiple values in shell scripting

Are there any idioms for returning multiple values from a bash function within a script? http://tldp.org/LDP/abs/html/assortedtips.html describes how to echo multiple values and process the results (e.g., example 35-17), but that gets tricky if some…
Wang
  • 3,247
  • 1
  • 21
  • 33
36
votes
2 answers

Golang and inheritance

I want to provide a base struct with methods in my library that can be 'extended'. The methods of this base struct rely on methods from the extending struct. This is not directly possible in Go, because struct methods only have acces to the structs…
theduke
  • 3,027
  • 4
  • 29
  • 28
34
votes
9 answers

Pythonic way to have a choice of 2-3 options as an argument to a function

I have a Python function which requires a number of parameters, one of which is the type of simulation to perform. For example, the options could be "solar", "view" or "both. What is a Pythonic way to allow the user to set these? I can see various…
robintw
  • 27,571
  • 51
  • 138
  • 205
34
votes
3 answers

Proper way to dynamically add functions to ES6 classes

I have a simple class with a single method exec(arg1,..,argn) and I want to have a number of alias methods which call exec with predefined argument values (e.g. exec_sync = exec.bind(this, true)). The following does the trick: class Executor { …
Yan Foto
  • 10,850
  • 6
  • 57
  • 88
34
votes
9 answers

Is str.replace(..).replace(..) ad nauseam a standard idiom in Python?

For instance, say I wanted a function to escape a string for use in HTML (as in Django's escape filter): def escape(string): """ Returns the given string with ampersands, quotes and angle brackets encoded. """ …
Michael
  • 11,612
  • 10
  • 41
  • 43
34
votes
2 answers

Hashes of Hashes Idiom in Ruby?

Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups. However, when inserting one must always check if the first index already exists in the hash. For example: h = Hash.new h['x'] = Hash.new if not…
David
  • 1,188
  • 12
  • 15
34
votes
10 answers

Alternative to the `match = re.match(); if match: ...` idiom?

If you want to check if something matches a regex, if so, print the first group, you do.. import re match = re.match("(\d+)g", "123g") if match is not None: print match.group(1) This is completely pedantic, but the intermediate match variable…
dbr
  • 165,801
  • 69
  • 278
  • 343
33
votes
8 answers

Why implicitly check for emptiness in Python?

The Zen of Python says that explicit is better than implicit. Yet the Pythonic way of checking a collection c for emptiness is: if not c: # ... and checking if a collection is not empty is done like: if c: # ... ditto for anything that can…
GNUnit
  • 577
  • 5
  • 9