in reply to How do you define "elegant"?
Elegance, in code, is when it's syntax reflects
the semantics of the high level operation being performed,
not the operational construction of how to perform it.
A simple example, showing how syntax has evolved to do this (using half remembered FORTRAN & C).
This
IF not condition THEN GOTO 10 I = 1 GOT0 20 10 I = 2 20
became this
INTEGER II; IF condition THEN II = 1 ELSE II = 2 ENDIF
became this
int i = ( condition ) ? 1 : 2;
I worked under C coding standards for a while where the trinary* operator was prohibited as "too advanced"; maintenance programmers wouldn't understand it.
Another, Perlish, example is;
{ my $temp = $a[ $i ]; $a[ $i ] = $a[ $j ]; $a[ $j ] = $temp; }
versus
@a[ $i, $j ] = @a[ $j, $i ];
The syntax clearly and concisely captures the operation without the extraneous noise of scope levels, temporary variables or the need to push code off into a subroutine somewhere else in the file; much less a different file.
And a final example from an earlier extended discussion:
sub isleap { my ($year) = @_; return 1 if (( $year % 400 ) == 0 ); # 400's are leap return 0 if (( $year % 100 ) == 0 ); # Other centuries are not return 1 if (( $year % 4 ) == 0 ); # All other 4's are leap return 0; # Everything else is not }
This can also be written as
not $year % 4 xor $year % 100 xor $year % 400;
Yes, it forces the programmer to know what xor does and it's precedence; but I first used this expression in 1978/9 in Basic Plus, where xor was known as EQV. Encounter it once, look it up, use it and it will become a part of your lexicon.
Elegance in fashion is often associated with things like "the simple black dress". A simple, unadorned black frock devoid of frills, bows or other extraneous accouterments that flatters it's wearer. Understated is the keyword. The secret of the simple black dress is in the hidden details. The precise cutting and expert tailoring that make it appear simply refined, whilst hiding the knowhow and detail that allow it to fit perfectly and so flatter.
I would define elegance in code as concealed detail and hidden refinement. Concise syntax that completely captures the semantics of the high level operation whilst concealing it's mechanics.
*Then trinary, now ternary--though I don't think that our kids ride terncycles; or play ternangles in school bands; or learn about ternceratops; or that the French flag is known as the terncolor; or that some gladiators fought with terndents; Nor do they eat ternfle for pudding :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do you define "elegant"?
by talexb (Chancellor) on Aug 18, 2006 at 12:08 UTC | |
|
Re^2: How do you define "elegant"?
by pingo (Hermit) on Aug 17, 2006 at 17:57 UTC | |
|
Re^2: How do you define "elegant"?
by halley (Prior) on Aug 22, 2006 at 18:26 UTC |