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
18
votes
3 answers

How should I replace vector::const_iterator in an API?

I've been given the task of polishing the interface of a codec library. We're using C++17, and I can only use the standard library (i.e. no Boost). Currently, there's a Decoder class that looks roughly like this: class Decoder : public Codec…
splicer
  • 5,344
  • 4
  • 42
  • 47
18
votes
9 answers

How to count non-null elements in an iterable?

I'm looking for a better/more Pythonic solution for the following snippet count = sum(1 for e in iterable if e)
Alexandru
  • 25,070
  • 18
  • 69
  • 78
18
votes
4 answers

How to name this key-oriented access-protection pattern?

Apparently this key-oriented access-protection pattern: class SomeKey { friend class Foo; SomeKey() {} // possibly non-copyable too }; class Bar { public: void protectedMethod(SomeKey); // only friends of SomeKey have…
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
18
votes
3 answers

Idiomatic config management in clojure?

What is an idiomatic way to handle application configuration in clojure? So far I use this environment: ;; config.clj {:k1 "v1" :k2 2} ;; core.clj (defn config [] (let [content (slurp "config.clj")] (binding [*read-eval* false] …
18
votes
5 answers

Python negative zero slicing

I often find myself having to work with the last n items in a sequence, where n may be 0. The problem is that trying to slice with [-n:] won't work in the case of n == 0, so awkward special case code is required. For example if len(b): …
Antimony
  • 37,781
  • 10
  • 100
  • 107
17
votes
2 answers

What to do with size_t vs. std::size_t?

Having just read: Does "std::size_t" make sense in C++? I realize using ::size_t is not standards-compliant (although supported by my compiler) when you #include . I want to comply with the standard, but I do not want to prepend std:: to…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
17
votes
6 answers

Ruby's tap idiom in Python

There is a useful Ruby idiom that uses tap which allows you to create an object, do some operations on it and return it (I use a list here only as an example, my real code is more involved): def foo [].tap do |a| b = 1 + 2 # ... and some…
Michał Kwiatkowski
  • 9,196
  • 2
  • 25
  • 20
17
votes
10 answers

x86 assembly idioms

I've been trying to get a good hold on the x86 assembly language, and was wondering if there was a quick-and-short equivalent of movl $1, %eax. That's when I thought that a list of idioms used frequently in the language would perhaps be a good…
susmits
  • 2,210
  • 2
  • 23
  • 27
17
votes
4 answers

Idiom vs. pattern

In the context of programming, how do idioms differ from patterns? I use the terms interchangeably and normally follow the most popular way I've heard something called, or the way it was called most recently in the current conversation, e.g. "the…
Roger Pate
17
votes
6 answers

How can I implement a generator in C++?

I want to know how to implement a generator , like Python, in C++? Python can use keyword "yield" to do so. But how to do it in C++?
okman
16
votes
2 answers

JavaScript idiom: create a function only to invoke it

I am learning YUI and have occasionally seen this idiom: Why do they create a function just to invoke it? Why not just write: For example see here.
flybywire
  • 261,858
  • 191
  • 397
  • 503
16
votes
2 answers

Ruby "return unless nil" idiom

I've got a smelly method like: def search_record(*args) record = expensive_operation_1(foo) return record unless record.nil? record = expensive_operation_2(foo, bar) return record unless record.nil? record =…
pithyless
  • 1,659
  • 2
  • 16
  • 31
16
votes
2 answers

Learning Elisp - what are the highest quality libraries to read source code?

When learning a new programming language, "read source code" is a common advice received by the experts. However, with such a huge system like emacs, build over decades by many people, it is not so easy for the beginner to figure out which libraries…
Thorsten
  • 3,451
  • 3
  • 20
  • 25
16
votes
2 answers

What is the most idiomatic way to merge two error types?

I have a type Foo whose methods may "raise" errors of an associated type Foo::Err. pub trait Foo { type Err; fn foo(&mut self) -> Result<(), Self::Err>; } I have another trait Bar with a method intended to process a Foo. Bar may issue…
Pierre-Antoine
  • 1,915
  • 16
  • 25
16
votes
5 answers

What is the underscore (" _ ") used for in Python?

I have seen this in a few contexts, e.g., in sequence unpacking: _, x = L.pop() # e.g., L is a list of tuples to initialize a container: X = _ So obviously this is not an element of the formal python syntax, rather the uses i am aware of appear…
doug
  • 69,080
  • 24
  • 165
  • 199