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
117
votes
5 answers

What is idiomatic code?

I'd be interested in some before-and-after c# examples, some non-idiomatic vs idiomatic examples. Non-c# examples would be fine as well if they get the idea across. Thanks.
MrBoJangles
  • 12,127
  • 17
  • 61
  • 79
113
votes
11 answers

`if key in dict` vs. `try/except` - which is more readable idiom?

I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case: I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do nothing and continue…
LeeMobile
  • 3,775
  • 6
  • 31
  • 32
107
votes
3 answers

String concatenation with Groovy

What is the best (idiomatic) way to concatenate Strings in Groovy? Option 1: calculateAccountNumber(bank, branch, checkDigit, account) { bank + branch + checkDigit + account } Option 2: calculateAccountNumber(bank, branch, checkDigit, account)…
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
104
votes
21 answers

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.
Red Hyena
  • 2,988
  • 5
  • 25
  • 24
87
votes
8 answers

Kotlin and idiomatic way to write, 'if not null, else...' based around mutable value

Suppose we have this code: class QuickExample { fun function(argument: SomeOtherClass) { if (argument.mutableProperty != null ) { doSomething(argument.mutableProperty) } else { doOtherThing() } …
Rob Pridham
  • 4,780
  • 1
  • 26
  • 38
85
votes
22 answers

Best ruby idiom for "nil or zero"

I am looking for a concise way to check a value to see if it is nil or zero. Currently I am doing something like: if (!val || val == 0) # Is nil or zero end But this seems very clumsy.
csexton
  • 24,061
  • 15
  • 54
  • 57
85
votes
9 answers

Is there a downside to adding an anonymous empty delegate on event declaration?

I have seen a few mentions of this idiom (including on SO): // Deliberately empty subscriber public event EventHandler AskQuestion = delegate {}; The upside is clear - it avoids the need to check for null before raising the event. However, I am…
serg10
  • 31,923
  • 16
  • 73
  • 94
83
votes
10 answers

What is a programming idiom?

I see the phrase "programming idiom" thrown around as if it is commonly understood. Yet, in search results and stackoverflow I see everything... From micro: Incrementing a variable Representing an infinite loop Swapping variable values To…
Corbin March
  • 25,526
  • 6
  • 73
  • 100
79
votes
12 answers

Idiom for iterating "between each consecutive pair of elements"

Everyone encounters this issue at some point: for(const auto& item : items) { cout << item << separator; } ... and you get an extra separator you don't want at the end. Sometime it's not printing, but, say, performing some other action, but…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
77
votes
5 answers

LBYL vs EAFP in Java?

I was recently teaching myself Python and discovered the LBYL/EAFP idioms with regards to error checking before code execution. In Python, it seems the accepted style is EAFP, and it seems to work well with the language. LBYL (Look Before You…
ryeguy
  • 65,519
  • 58
  • 198
  • 260
72
votes
8 answers

What is the idiomatic way to slice an array relative to both of its ends?

Powershell's array notation has rather bizarre, albeit documented, behavior for slicing the end of arrays. This section from the official documentation sums up the bizarreness rather well: Negative numbers count from the end of the array. For…
alx9r
  • 3,675
  • 4
  • 26
  • 55
68
votes
15 answers

Common Ruby Idioms

One thing I love about ruby is that mostly it is a very readable language (which is great for self-documenting code) However, inspired by this question: Ruby Code explained and the description of how ||= works in ruby, I was thinking about the ruby…
DanSingerman
  • 36,066
  • 13
  • 81
  • 92
65
votes
7 answers

Multiple constructors: the Pythonic way?

I have a container class that holds data. When the container is created, there are different methods to pass data. Pass a file which contains the data Pass the data directly via arguments Don't pass data; just create an empty container In Java, I…
Johannes
  • 3,300
  • 2
  • 20
  • 35
65
votes
2 answers

What is the idiomatic way to return an error from a function with no result if successful?

In Rust, I believe the idiomatic way to deal with recoverable errors is to use Result. For example this function clearly is idiomatic: fn do_work() -> Result {...} Of course, there are also functions that have a single, obvious,…
Others
  • 2,876
  • 2
  • 30
  • 52
59
votes
4 answers

Finding an item that matches predicate in Scala

I'm trying to search a scala collection for an item in a list that matches some predicate. I don't necessarily need the return value, just testing if the list contains it. In Java, I might do something like: for ( Object item : collection ) { if…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
1
2
3
80 81