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.

In reply to Re: How do you define "elegant"? by BrowserUk
in thread How do you define "elegant"? by Mutant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.