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
-1
votes
2 answers

Is it good practice to implement virtual methods using multiple inheritance?

I have: class A { virtual void foo() = 0; virtual void bar() = 0; }; class Fooing { virtual void foo() = 0; }; class Barring { virtual void bar() = 0; }; class TallFooing : public Fooing { void foo(){ std::cout << "tall foo"…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
-1
votes
2 answers

Idiomatically removing bracketted list elements

How can one generate a new list has all the elements of old-list except for some parts bracketted between line where f1(start_line) is true and f2(end_line) is true Naive code def remove_bracketted(orig_list) ignore_flag = false new_list =…
-1
votes
2 answers

python equivalent to perl's @{$obj->method($args)} or %{$obj->method($args)}

Title demonstrates the perl idiom I wish to convey in my python script. I realize this could be construed as bad practice, potentially due to (lack of) exception handling. Though nonetheless I find value in performing such operations in…
-1
votes
3 answers

What do I do instead of grep'ing the output of ps?

Everyone knows how annoying this is: [mybox:~ #] ps aux | grep myservice root 2273 0.0 0.0 4360 760 ? Ss 18:06 0:00 /usr/sbin/myservice root 18590 0.0 0.0 4100 788 pts/2 S+ 21:21 0:00 grep myservice you get…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
-1
votes
4 answers

what is the work-around to "non-static variable cannot be referenced from a static context"?

There's a particular idiom to putting a method, or perhaps anonymous inner class, somehow, into the main method of a driver class: package net.bounceme.dur.misc; import net.bounceme.dur.misc.Foo; public class StaticRef { Foo f =…
Thufir
  • 8,216
  • 28
  • 125
  • 273
-2
votes
1 answer

Why is it so badly slow to pass variadic arguments?

package main import ( "fmt" "time" ) func main() { n := 1024 dst1 := make([]byte, n) dst2 := make([]byte, 0, n) dst3 := make([]byte, 0, n) start := time.Now() for i := 0; i < n; i++ { dst1[i] = byte(i) …
xmllmx
  • 39,765
  • 26
  • 162
  • 323
-2
votes
2 answers

Is it pythonic to use the '__main__' mechanism to test the module?

Suppose we have the following module: # my_module.py def my_sum(a, b): return a + b + 1 if __name__ == '__main__': s = my_sum(2, 3) print(s) How bad / good / pythonic is it to test my modules or parts of them like that? EDIT: I'm not…
-2
votes
3 answers

Golang idiom for passing multiple argument types

I am new to learning Go and have a question around defining an argument that could be one of two types. Take the code: type Thing struct { a int b int c string d string } type OtherThing struct { e int f int c string d…
apr_1985
  • 1,764
  • 2
  • 14
  • 27
-2
votes
1 answer

Is creating a dedicated function a nice way to handle errors

I've recently watch this talk by Liz Rice (which I highly recommend!), and I've seen that instead of writing: func main() { if err := someFunc(); err != nil { panic(err) } } she does: func main() { must(someFunc()) } func…
hacb
  • 175
  • 2
  • 10
-2
votes
1 answer

Return 0.0 and 1.0 if input falls below 0.0 or above 1.0

The goal is to "do something" if the input value falls between (0.0, 1.0). Otherwise, return 1.0 if input is >= 1.0 or return 0.0 if input is <= 0.0 The straightforward way would be to: def func(x): if x >= 1.0: return 1.0 elif x…
alvas
  • 115,346
  • 109
  • 446
  • 738
-2
votes
2 answers

Workaround for lack of support for array constants?

Go doesn't have array constants. My application receives messages containing several types of numeric codes which I need to display as phrases. If array contants existed I could do something like: func foo() { ... fmt.Println(facename[f]) …
RedGrittyBrick
  • 3,827
  • 1
  • 30
  • 51
-2
votes
1 answer

what does this unusual python (or shell?) idiom mean? "!mv {path} {path}.png"

!mv {path} {path}.png This is one line from python script. What does this mean? I've never seen this before. There is also something similar in the code: !/some/path/not-string-just-as-it-is-here.png Seems like some kind of string manipulation…
nanoook
  • 21
  • 1
  • 6
-2
votes
2 answers

When/if to make a non-virtual function a member function

I am trying to get a feel for modern C++ idioms and best practices, and I wanted to ask if, when authoring a class, there was ever a time one should make a function a member function, instead of a free-function in the class's namespace, besides when…
-2
votes
1 answer

How can I execute a block to populate a variable only if the variable is blank?

I have some instance methods in a class that must be called in a sequence. The failure of any method in a sequence will require the previous method to be re-called. I'm storing the result of each successful method call in a class variable: class…
RubyRedGrapefruit
  • 12,066
  • 16
  • 92
  • 193
-3
votes
1 answer

Idiomatic way to do basic pattern matching on multiple variables in Python

I want to achieve what this code (written in Crystal) does: enum PieceKind Pawn, Rook, Bishop, Knight, King, Queen end def piece_kind_at_init(x, y) case y when 2, 7 then PieceKind::Pawn when 1, 8 case x when 1, 8…
daw kot
  • 17
  • 2
1 2 3
80
81