Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
This list is filtered with keywords : foobar

Idiom #206 Switch statement with strings

Execute different procedures foo, bar, baz and barfl if the string str contains the name of the respective procedure. Do it in a way natural to the language.

Idiom #108 Determine if variable name is defined

Print the value of variable x, but only if x has been declared in this program.
This makes sense in some languages, not all of them. (Null values are not the point, rather the very existence of the variable.)

Idiom #254 Replace value in list

Replace all exact occurrences of "foo" with "bar" in the string list x

Idiom #329 Read value in a map

Assign to v the value stored in the map m for the key k.

Explain what happens if there is no entry for k in m.

Idiom #126 Multiple return values

Write a function foo that returns a string and a boolean value.

Idiom #295 String to Enum

Given the enumerated type T, create a function TryStrToEnum that takes a string s as input and converts it into an enum value of type T.

Explain whether the conversion is case sensitive or not.
Explain what happens if the conversion fails.

Idiom #223 for else loop

Loop through list items checking a condition. Do something else if no matches are found.

A typical use case is looping through a series of containers looking for one that matches a condition. If found, an item is inserted; otherwise, a new container is created.

These are mostly used as an inner nested loop, and in a location where refactoring inner logic into a separate function reduces clarity.