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
21
votes
1 answer

When is `new Error()` better than `Error()`?

The ES5 language spec clearly states that Error(foo) does the same thing as new Error(foo). But I notice that in the wild, the longer new Error(foo) form is much more common. Is there some reason for this? Is there any situation where using new…
joeytwiddle
  • 29,306
  • 13
  • 121
  • 110
21
votes
3 answers

Idiomatic Python logging: format string + args list vs. inline string formatting - which is preferred?

Is it advantageous to call logging functions with format string + args list vs. formatting inline? I've seen (and written) logging code that uses inline string formatting: logging.warn("%s %s %s" % (arg1, arg2, arg3)) and yet I assume it's better…
Inactivist
  • 9,997
  • 6
  • 29
  • 41
20
votes
10 answers

Is there a shortcut to create padded array in JavaScript?

I have this javascript: function padded_array(k, value){ var a = []; a[k] = value; return a; } padded_array(3, "hello"); //=> [undefined, undefined, undefined, 'hello'] Is it possible to shorten the code in the function body?
maček
  • 76,434
  • 37
  • 167
  • 198
19
votes
2 answers

Resources for learning idiomatic Haskell (eta reduction, symbolic infix operators, libraries etc.)

Despite some experience with Lisp and ML, I'm having a great deal of trouble learning to read and (idiomatically) write Haskell because the local style seems to be do eta elimination whenever possible eschew parentheses in favor of exploiting…
Wang
  • 3,247
  • 1
  • 21
  • 33
19
votes
1 answer

Using nnet for prediction, am i doing it right?

I'm still pretty new to R and AI / ML techniques. I would like to use a neural net for prediction, and since I'm new I would just like to see if this is how it should be done. As a test case, I'm predicting values of sin(), based on 2 previous…
dizzy
  • 381
  • 1
  • 2
  • 10
19
votes
1 answer

What is the idiomatic way to swap two elements in a vector

Is there a better or more concise way to do the following? (defn swap [v i1 i2] "swap two positions in a vector" (let [e1 (v i1) e2 (v i2)] (-> (assoc v i1 e2) (assoc i2 e1))))
Nick Orton
  • 3,563
  • 2
  • 21
  • 28
19
votes
12 answers

What are the important language features (idioms) of Python to learn early on

I would be interested in knowing what the StackOverflow community thinks are the important language features (idioms) of Python. Features that would define a programmer as Pythonic. Python (pythonic) idiom - "code expression" that is natural or…
John Burley
  • 1,208
  • 2
  • 14
  • 22
19
votes
4 answers

With std::byte standardized, when do we use a void* and when a byte*?

C++17 will include std::byte, a type for one atomically-addressable unit of memory, having 8 bits on typical computers. Before this standardization, there is already a bit of dilemma when pointing into "raw" memory - between using char*/unsigned…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
19
votes
3 answers

Best way of defining a compile-time constant

What's the best way of defining a simple constant value in C++11, such that there is no runtime penalty? For example: (not valid code) // Not ideal, no type, easy to put in wrong spot and get weird errors #define VALUE 123 // Ok, but integers…
Malvineous
  • 25,144
  • 16
  • 116
  • 151
19
votes
2 answers

What is "sentry object" in C++?

I answered this question, and Potatoswatter answered too as The modern C++ equivalent would be a sentry object: construct it at the beginning of a function, with its constructor implementing call(), and upon return (or abnormal exit), its …
Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
19
votes
4 answers

"GetOrCreate" - does that idiom have an established name?

Ok, consider this common idiom that most of us have used many times (I assume): class FooBarDictionary { private Dictionary fooBars; ... FooBar GetOrCreate(String key) { FooBar fooBar; if…
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
19
votes
3 answers

What's the idiomatic Clojure for "foo = bar || baz"?

I want to supply a default value which can be overridden. I know I can use a ternary, like this: (def foo (if (not (nil? bar)) bar baz)) But surely there is a more idiomatic way in Clojure to say "use bar, or baz if bar is nil. Any suggestions?
Josh Glover
  • 25,142
  • 27
  • 92
  • 129
18
votes
3 answers

pimpl for a templated class

I want to use the pimpl idiom to avoid having users of my library need our external dependencies (like boost, etc) however when my class is templated that seems to be impossible because the methods must be in the header. Is there something I can do…
David
  • 27,652
  • 18
  • 89
  • 138
18
votes
2 answers

Optionally taking the first item in a sequence

I need a function like Seq.head, but returning None instead of throwing an exception when the sequence is empty, i.e., seq<'T> -> 'T option. There are a jillion ways to do this. Here are several: let items = Seq.init 10 id let a = Seq.tryFind (fun _…
Daniel
  • 47,404
  • 11
  • 101
  • 179
18
votes
1 answer

Idiomatic way of using StringBuilder in kotlin?

I often write pretty complex toString() methods and this question is always bothering me - which variant is more clear to read. Following examples are simplified, usually there are a lot of conditionals, so single liners are not fit. 1) like in…
curioushikhov
  • 2,461
  • 2
  • 30
  • 44