MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1&1

Programing: Why Idiomatic Programing Are Bad

Xah Lee,

This article shows a code snippets in {Common Lisp, perl, python} to illustrate why idiomatic programing are bad.

Here's constructions in various languages. See if you can understand what they do and identify each language.

(floor x y)
x/y
x//y
require POSIX; floor(x/y);
int(x/y);

They are all doing floor(x/y).

In Common Lisp (floor (/ x y)) can be written as (floor x y). Because Common Lisp's “floor” function essentially acts as a integer quotient function. The second argument is optional, with default value of 1. (see spec: Source www.lispworks.com.)

The (floor x y) is not a good lang design, more of a lang quirk.

Similarly, in Python 2.x, x/y will work when both x and y are integers. Also, x//y works too, but that // is a unreadable syntax quirk.

Similarly, in perl, you can do require POSIX; floor(x/y);. The “POSIX” instead of “Math” is a quirk. In any case, “floor” should really be builtin. Another way to do this in perl is int(x/y).

All of the above are quirks. They rely on computer engineering by-products (such as “int”), or rely on the lang's idiosyncrasy. One easy way to measure it is whether a programer can read and understand a program without having to spend a lot time to delve into its tricks. Problem with these lang idioms is that it's harder to understand, and whatever advantage/optimization they provide is microscopic and temporary.

Best is really floor(x/y).

Idiomatic programing is a bad thing. It was spread by perl, of course, in the 1990s. Idiomatic languages, i.e. lang with huge number of idioms, such as perl, is the worst.

Thanks to Zach Beane, Kaz Kylheku, for the tip on Common Lisp's “floor”.

For actual code of these langs using floor(a/b) doing something useful, see: In-place Algorithm for Reversing a List in Perl, Python, Lisp, Mathematica.

See also: 〔Programming Languages Have Social Mores Not Idioms By Zed A Shaw. @ learncodethehardway.org…

blog comments powered by Disqus