Concrete vs Abstract Code

By Xah Lee. Date: . Last updated: .

Here's illustration of 2 styles of the same code. One is concrete, the other abstract.

Concrete:

mv //xpc/users/xah/Documents/*.png c:/Users/xah/Downloads/x_todo_pics/ &&
mv //xpc/users/xah/Documents/*.jpg c:/Users/xah/Downloads/x_todo_pics/ &&
mv //xpc/users/xah/Pictures/*.png c:/Users/xah/Downloads/x_todo_pics/ &&
mv //xpc/users/xah/Pictures/*.jpg c:/Users/xah/Downloads/x_todo_pics/

Abstract:

$fromRoot = "//xpc/users/xah/"
$fromDirs = "Documents" , "Pictures"
$suffixPattern = "*.png" , "*.jpg"
$toDir = "c:/Users/xah/Downloads/x_todo_pics/"
foreach ($x in $fromDirs) {
$from = join-path $fromRoot $x;
foreach ($ext in $suffixPattern) { mv (join-path $from $ext) $toDir; } }

Concrete code is usually easier to understand, but is more verbose.

Abstract code is usually recommended, because it reduces repeatition, but may be harder to understand.

Programing Idioms and Style