Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How do you define "elegant"?

by BrowserUk (Patriarch)
on Aug 17, 2006 at 00:23 UTC ( [id://567805]=note: print w/replies, xml ) Need Help??


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 :)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: How do you define "elegant"?
by pingo (Hermit) on Aug 17, 2006 at 17:57 UTC
    Not sure about the precedence rules, but shouldn't there be a parenthesis around that?
    not($year % 4 xor $year % 100 xor $year % 400);
Re^2: How do you define "elegant"?
by talexb (Chancellor) on Aug 18, 2006 at 12:08 UTC
      I worked under C coding standards for a while where the trinary* operator was prohibited as "too advanced"; maintenance programmers wouldn't understand it.

    Your post made me laugh out loud. I had a similar situation -- I was in a group that had a new leader parachuted in, and he hated it when I used the ternary operator -- too fancy, he said. I was half a dozen years out of Waterloo by then .. I think he was trying to make up for the fact that he had a diploma in Tourism. Or something.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re^2: How do you define "elegant"?
by halley (Prior) on Aug 22, 2006 at 18:26 UTC
    Personally, I think the long isleap() form is clearer to the maintenance coder, since it will resemble the problem definitions they will find in non-code sources. It's more literate, even without the comments, just for the visual rhythm and the name of the sub. I would deem the one-liner more clever, but cleverness is not a programmers' virtue.

    I otherwise agree with your overall thrust.

    As for the ternary, python has long tried to resist the loud calls for its inclusion, on the same sort of grounds as your C programming team. I think last I heard it was adopted for 2.5, but I could be wrong. Sometimes even Guido must bend to pragmatism.

    --
    [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://567805]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (9)
As of 2024-04-19 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found