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
55
votes
6 answers

DRY Ruby Initialization with Hash Argument

I find myself using hash arguments to constructors quite a bit, especially when writing DSLs for configuration or other bits of API that the end user will be exposed to. What I end up doing is something like the following: class Example …
Kyle E. Mitchell
  • 703
  • 1
  • 6
  • 6
53
votes
4 answers

Convert rune to int?

In the following code, I iterate over a string rune by rune, but I'll actually need an int to perform some checksum calculation. Do I really need to encode the rune into a []byte, then convert it to a string and then use Atoi to get an int out of…
miku
  • 181,842
  • 47
  • 306
  • 310
52
votes
18 answers

Best Loop Idiom for special casing the last element

I run into this case a lot of times when doing simple text processing and print statements where I am looping over a collection and I want to special case the last element (for example every normal element will be comma separated except for the last…
Dougnukem
  • 14,709
  • 24
  • 89
  • 130
51
votes
10 answers

Python indentation in "empty lines"

Which is preferred ("." indicating whitespace)? A) def foo(): x = 1 y = 2 .... if True: bar() B) def foo(): x = 1 y = 2 if True: bar() My intuition would be B (that's also what vim does for me), but I see…
nisc
  • 4,222
  • 4
  • 29
  • 34
50
votes
3 answers

Get the first element of a list idiomatically in Groovy

Let the code speak first def bars = foo.listBars() def firstBar = bars ? bars.first() : null def firstBarBetter = foo.listBars()?.getAt(0) Is there a more elegant or idiomatic way to get the first element of a list, or null if it's not possible? …
Adam Schmideg
  • 10,590
  • 10
  • 53
  • 83
50
votes
9 answers

Idiomatic way of handling nullable or empty List in Kotlin

Say I have a variable activities of type List?. If the list is not null and not empty, I want to do something, otherwise I want to do something else. I came up with following solution: when { activities != null && !activities.empty ->…
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
49
votes
4 answers

Idiomatic use of std::rel_ops

What is the preferred method of using std::rel_ops to add the full set of relational operators to a class? This documentation suggests a using namespace std::rel_ops, but this seems to be deeply flawed, as it would mean that including the header for…
Mankarse
  • 39,818
  • 11
  • 97
  • 141
49
votes
4 answers

Idiomatic Python: 'times' loop

Say I have a function foo that I want to call n times. In Ruby, I would write: n.times { foo } In Python, I could write: for _ in xrange(n): foo() But that seems like a hacky way of doing things. My question: Is there an idiomatic way of doing…
perimosocordiae
  • 17,287
  • 14
  • 60
  • 76
49
votes
6 answers

C++ code for state machine

This was an interview question to be coded in C++: Write code for a vending machine: Start with a simple one where it just vends one type of item. So two state variables: money and inventory, would do. My answer: I would use a state machine…
Romonov
  • 8,145
  • 14
  • 43
  • 55
47
votes
7 answers

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

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. Why one should use the idioms, and what do the…
Partial
  • 9,529
  • 12
  • 42
  • 57
46
votes
2 answers

Read a file and get an array of strings

I want to read a file and get back a vector of Strings. The following function works, but is there a more concise or idiomatic way? use std::fs::File; use std::io::Read; fn lines_from_file(filename: &str) -> Vec { let mut file = match…
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
44
votes
10 answers

Are one-line 'if'/'for'-statements good Python style?

Every so often on here I see someone's code and what looks to be a 'one-liner', that being a one line statement that performs in the standard way a traditional 'if' statement or 'for' loop works. I've googled around and can't really find what kind…
Federer
  • 33,677
  • 39
  • 93
  • 121
44
votes
6 answers

Ruby idiom for substring from index until end of string

Just wondering if there's a Ruby idiom for extracting a substring from an index until the end of the string. I know of str[index..-1] which works by passing in a range object to the String's [] method but it's a little clunky. In python, for…
Sherwin Yu
  • 3,180
  • 2
  • 25
  • 41
42
votes
8 answers

Rails idiom to avoid duplicates in has_many :through

I have a standard many-to-many relationship between users and roles in my Rails app: class User < ActiveRecord::Base has_many :user_roles has_many :roles, :through => :user_roles end I want to make sure that a user can only be assigned any role…
KingPong
  • 1,439
  • 1
  • 16
  • 22
41
votes
2 answers

What is the idiomatic way to assoc several keys/values in a nested map in Clojure?

Imagine you have a map like this: (def person { :name { :first-name "John" :middle-name "Michael" :last-name "Smith" }}) What is the idiomatic way to change values associated with both :first-name and :last-name in one…
byteclub
  • 605
  • 1
  • 5
  • 10
1 2
3
80 81