How beautiful is your code?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Rebel
Posts: 6995
Joined: Thu Aug 18, 2011 12:04 pm

Re: How beautiful is your code?

Post by Rebel »

With a goto you can make your code somewhat faster when you know the branch prediction rate.
User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Re: How beautiful is your code?

Post by JuLieN »

As for me, I'd say: "small is beautiful". I always feel a wave of joy when I find a way to make an algorithm more compact. I'm happy when I see nothing I could possibly remove. And if the code is still perfectly understandable then it becomes a masterwork.
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
User avatar
hgm
Posts: 27809
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: How beautiful is your code?

Post by hgm »

You must like the micro-Max code, then. I know I do! :lol:
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: How beautiful is your code?

Post by lucasart »

hgm wrote:You must like the micro-Max code, then. I know I do! :lol:
The code is indeed beautiful and self-documenting. No need to add comments, it's cristal clear:

Code: Select all

#define F&#40;I,S,N&#41; for&#40;I=S;I<N;I++)
#define W&#40;A&#41; while&#40;A&#41;

int V=112,M=136,I=8e3,C=799,X,Y,Q,N,
d&#91;&#93;=&#123;-16,-15,-17,0,1,16,0,1,16,15,17,0,14,18,31,33,0,
     1,1,3,-1,3,5,9,
     7,-1,11,6,8,3,6&#125;,
b&#91;128&#93;=&#123;6,3,5,7,4,5,3,6&#125;;

char n&#91;&#93;=".?+pkltd?*?PKLTD";

D&#40;k,q,l,e,x,n&#41;
int k,q,l,e,x,n;
&#123;
 int i=0,j,t,p,u,r,y,m=n>1|q>e?q&#58;e,v,h,z;

 N++;
 do&#123;
  u=b&#91;i&#93;;
  if&#40;u&k&#41;
  &#123;r=p=u&7;
   j=d&#91;p+23&#93;;
   W&#40;r=p>2&r<0?-r&#58;-d&#91;++j&#93;)
   &#123;y=i;
    do
    &#123;y+=r;if&#40;y&M&#41;break;
     t=b&#91;y&#93;;if&#40;t&k|p<3&!&#40;r&7&#41;-!t&#41;break;
     v=99*d&#91;t&7|16&#93;;
     if&#40;v<0&#41;m=I;
     if&#40;m>=l&#41;return m;
    
     if&#40;h=n-&#40;y!=x&#41;)
     &#123;if&#40;p<6&#41;v+=b&#91;i+8&#93;-b&#91;y+8&#93;;
      if&#40;p==4&#41;v+=9*&#40;e>C?!(&#40;x^y&#41;&68&#41;&#58;-3&#41;;

      b&#91;i&#93;=0;b&#91;y&#93;=u;
      if&#40;p<3&#41;
      &#123;v-=9*((&#40;i-2&#41;&M||b&#91;i-2&#93;!=u&#41;+(&#40;i+2&#41;&M||b&#91;i+2&#93;!=u&#41;-1&#41;;
       if&#40;y+r+1&128&#41;&#123;b&#91;y&#93;|=7;v+=C;&#125;
      &#125;
      v=-D&#40;24-k,-l,-m,z=-e-v,y,h&#41;;
      v-=v>I/3;
      if&#40;x==9&#41;&#123;if&#40;v+I&&i==X&y==Y&#41;&#123;Q=z;return l;&#125;v=m;&#125;
      b&#91;i&#93;=u;b&#91;y&#93;=t;
 
      if&#40;v>m&#41;&#123;m=v;if&#40;x&8&#41;&#123;X=i;Y=y;&#125;&#125;
     &#125;
     t+=p<5;if&#40;p<3&&6*k+&#40;y&V&#41;==128&#41;t--;
    &#125;W&#40;!t&#41;;
   &#125;
  &#125;
 &#125;W&#40;i=i+9&~M&#41;;
 return m+I?m&#58;-D&#40;24-k,-I,I,0,x,1&#41;/2;
&#125;

main&#40;)
&#123;
 int k=8,*p,c&#91;9&#93;,d;

 F&#40;X,0,8&#41;
 &#123;b&#91;X+V&#93;=&#40;b&#91;X&#93;+=16&#41;-8;b&#91;X+16&#93;=18;b&#91;X+96&#93;=9;
  F&#40;Y,0,8&#41;b&#91;16*Y+X+8&#93;=&#40;X-4&#41;*&#40;X-4&#41;+&#40;Y-3.5&#41;*&#40;Y-3.5&#41;;
 &#125;

 W&#40;1&#41;
 &#123;F&#40;N,0,121&#41;printf&#40;" %c",N&8&&&#40;N+=7&#41;?10&#58;n&#91;b&#91;N&#93;&15&#93;);
  p=c;W&#40;(*p++=getchar&#40;))>10&#41;;
  if&#40;*c-10&#41;&#123;X=*c-16*c&#91;1&#93;+C;Y=c&#91;2&#93;-16*c&#91;3&#93;+C;&#125;else
  &#123;d=6;N=0;W&#40;N<1e6&#41;D&#40;k,-I,I,Q,8,d++);&#125;
  if&#40;D&#40;k,-I,I,Q,9,2&#41;==I&#41;k^=24;
 &#125;
&#125;
By the way, did you submit micro-max to the IOCCC ? You could win with that!
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
User avatar
hgm
Posts: 27809
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: How beautiful is your code?

Post by hgm »

Oscar Toledo already won IOCCC once with a Chess program. Micro-Max is not really obfuscated, and the latest versions are extensively commented. But look at this:

Code: Select all

#define    F getchar&#40;))
 #define H&#40;z&#41;*n++=z;
       #include        <setjmp.h>
    #define v main&#40;0,0,0
           #define Z while&#40;
                                                 #define _ if&#40;
#define o&#40;d&#41; &#40;u&#91;l&#93;=0,l&#91;d&#93;=6^e,q=1e4>v,0&#41;,l&#91;d&#93;=0,u&#91;l&#93;=e^6,q&#41;
#define I&#40;H,n&#41; &#123; _ r=l&#91;x=H&#93;,!r|&#40;r^e&#41;<-1&#41;&#123; _ j=u&#91;l&#93;,-7==r|6==r\
)&#123; n; e=~e; return 1e5- f; &#125; u&#91;l&#93;=0,t=j+1,i=j-1; _!i&89<x&#41;i\
=j,t=6; _-1==t&30>x&#41;t=j,i=-7; Z++i<t&#41;&#123; b=S; d=0; S&=63; \
a=(&#40;j^e&#41;!=1?6!=&#40;j^e&#41;?O&#91;32+x/10&#93;-O&#91;u/10+32&#93;-q&#58;&#40;S|=6!=j?8\
&#58;1,2==u-x&#41;*9+9*&#40;x-u==2&#41;&#58;&#40;d=1==j?x-u&#58;u-x&#41;/8+!(!(&#40;x-u&#41;%\
10&#41;|r&#41;*99+&#40;j==1?90<x&#58;29>x&#41;*&#40;9*O&#91;28+i&#93;-288&#41;)+O&#91;r+28\
&#93;*9-288+O&#91;x%10+33&#93;-f-O&#91;33+u%10&#93;; x&#91;l&#93;=i; S|=&#40;21=\
=u|21==x&#41;*2+&#40;u==28|28==x&#41;*4+&#40;91==u|x==91&#41;*16+32\
*&#40;u==98|x==98&#41;+&#40;20==d&#41;*64*x; a-=k>f?main&#40;a,f+1\
,M,k&#41;&#58;0; _ i==c&u==h&!f&N&a>-1e4&x==y&#41;longjm\
p&#40;z,1&#41;; S=b; _!N|f&&&#40;a>M||!f&a==M&&1&rand&#40;)\
))&#123; _!f&#41;&#123; _ k&#41;&#123; c=i; h=u; y=x; &#125; &#125; else _ \
L-a<N&#41;&#123; n; e=~e; u&#91;l&#93;=j; x&#91;l&#93;=r; return\
 a; &#125; M=a; &#125; &#125; x&#91;l&#93;=r; u&#91;l&#93;=j; n; &#125; &#125;
typedef int G; char J &#91; 78 &#93;, O &#91; &#93;
=   "HRQAMS#-smaqrh&#91;UTZYTU&#91;|TBA&#40;"
"$#&#40;ABT|ba`gg`ab8>GK&#91;_`fFDZXEYR"         "L\t####"
"##B#A#@#G#F#E#D#K\t\3Zlv#tjm"         "\3J#tjm\3Pwb"
"ofnbwf\3Joofdbo\3&#41;&`&`.&`&`"         "#+&g*\t"; G y,
c,h,e,S,*s,l&#91;149&#93;; jmp_buf z         ; G main&#40;G L,G f,
G N,G k&#41;&#123; G u=99,p,q,r,j,i,x         ,t, a, b,d,M=-1e9
; char *n; if&#40; *l&#41;&#123; e=~e; Z       u >21&#41;&#123; q= l&#91;--u&#93;^e;
_!-- q&#41;&#123; _!l&#91;p=e?u-10&#58;u+10&#93;)&#123;   I&#40;p,)_ e?u>80   & !l&#91;p
-=10&#93;&#58;u<39&!l&#91;p+=10&#93;&#41;I&#40;p,)&#125; _ l&#91;p=e?u-11&#58;9+u&#93;   &#41;I&#40;p,)
else _ u-1==S>>6&#41;&#123; l&#91;u-1&#93;=0; I&#40;p,l&#91;u-1&#93;=-2^e&#41;;  &#125; _ l&#91;
p=e?u-9&#58;11+u&#93;&#41;I&#40;p,&#41;else _ S>>6==1+u&#41;&#123; l&#91;1+u&#93;=0; I&#40;p,l
&#91;1+u&#93;=e^-2&#41;; &#125; &#125; _!--q&#41;&#123; n=O+41; Z++n<50+O&#41;I&#40;u+80-*n,
)&#125; _ 0<q&4>q&#41;&#123;  n=q==2?53+O&#58;O+49; Z++n<O+&#40;q!=1&#41;*4+54
)&#123; p=u; do I&#40;p-=*n-80,&#41;Z!p&#91;l&#93;); &#125; &#125; _ 4==q&#41;&#123; n=49+O
 ; Z++n<O+58&#41;I&#40;u-*n+80,)_ e&!&#40;S&24&#41;|!e&!&#40;S&3&#41;&&k&&
 !l&#91;u-2&#93;&!l&#91;u-1&#93;&!l&#91;u-3&#93;&&o&#40;u&#41;&o&#40;u-1&#41;)&#123; l&#91;u-1&#93;= 4
  ^e; l&#91;u-4&#93;=0; I&#40;u-2,l&#91;u-1&#93;=0; l&#91;u-4&#93;=e^4&#41;; &#125; _
  e&!&#40;S&40&#41;|!e&!&#40;S&5&#41;&&k&&!l&#91;u+1&#93;&!l&#91;2+u&#93;&&o&#40;u&#41;&
   o&#40;1+u&#41;)&#123; l&#91;u+1&#93;=e^4; l&#91;3+u&#93;=0;   I&#40;u+2,l&#91;1+u
   &#93;=0; l&#91;u+3&#93;=4^e&#41;; &#125; &#125; &#125; e=~e;   return M; &#125;
    Z h<130&#41;&#123;l&#91;h&#93;=-&#40;21>h|98<h|2       >&#40;h+1 )%
    10&#41;; O&#91;h++&#93;^=3; &#125; n=O +14;       s=20+l; Z
     ++s<29+l&#41;&#123; 10&#91;s&#93;=1; 70&#91;s&#93;=~    ( * s = *
      n++ -+84&#41;; 60 &#91; s&#93; =-2; &#125; Z  n=J&#41;&#123; puts
       &#40;58+O&#41;; u=19; Z++u<100&#41;&#123; H&#40;32&#41;_!( u%10
       )&#41;H&#40;32&#41;H&#40;O&#91;7+l&#91;u&#93;&#93;)_&#40;9+u&#41;%10>7&#41;&#123; H&#40;58
        -u/10&#41;H&#40;32&#41;_ u&1&#41;puts&#40;n=J&#41;; &#125; &#125; puts
         &#40;O+58&#41;; _-1e4 >v , 1&#41;)&#123; e=~e; puts
          &#40;O+&#40;v,0&#41;> 1e4?e?90&#58;82&#58;96&#41;); break
           ; &#125; _ 1<L&e&#41; &#123; d=v,2+L&#41;; printf
            &#40;O+114,h%10+64,58-h/10,y%10+64
             ,58 -y/10,d&#41;; &#125; else&#123; putchar
              &#40;62 ) ; h= &#40;95 & F-44; c=l&#91;h
                +=&#40;56-F *10&#93;; y=&#40;95&F-44; y
                   +=&#40;56-F*10; Z 10!=&#40;u=&#40;95
                    &F&#41;)&#123; c=5; Z--c>1&&u!=c
                      &#91;O&#93;); c=e^c-7; &#125; &#125; _!
                         setjmp&#40;z&#41;)&#123; v+1,1&#41;;
                               puts&#40;   106+
                                O&#41;; &#125;   &#125; Z
                                 10!=
                                  F; &#125;
User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Re: How beautiful is your code?

Post by JuLieN »

hgm wrote:You must like the micro-Max code, then. I know I do! :lol:
Actually I do. :) Micro-Max is an impressive achievement! But for it to produce an artistic feeling it would also have to be self-documenting (that is, directly understandable by someone else than its author. ;) )
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
wgarvin
Posts: 838
Joined: Thu Jul 05, 2007 5:03 pm
Location: British Columbia, Canada

Re: How beautiful is your code?

Post by wgarvin »

michiguel wrote:
JuLieN wrote:In our little community, sources of engines like Fruit or Stockfish are often praised, the first one for its clarity, the second one for its mastering of all C++ tricks (something that, as a pascal programmer, I have to trust you for). But what is a beautiful code ?

I just read a very interesting article on the subject:
http://kotaku.com/5975610/the-exception ... t=56177550

So how beautiful is your code?
I don't believe there is such thing. Beautiful means nothing and it is in the eye of the beholder. Names that I think could be applied to compliment code are "straightforward", "readable", "efficient", "modular", "maintainable" etc., but beautiful is a silly name, the same thing happens when chefs praise food. The food could be "flavorful", "intense", "spicy" etc. but they use poetry instead.
Oh, I think there's definitely such a thing as "beautiful code". It can be very subjective, but that doesn't mean it doesn't exist!

I read "beautiful code" as a sort of technical shorthand for "code that has all the good properties that code should have, and none of the bad smells that bad code usually has". I think its closely related to another nebulous hard-to-define concept: "Simplicity".

Over the years, I have collected this list of quotes related to computer programming, so I'll take this opportunity to post them here. Many of them are about simplicity:
Various people wrote: Nearly every complex solution to a programming problem that I have looked at carefully has turned out to be wrong. -- Brent Welch

Controlling complexity is the essence of computer programming. -- Brian Kernigan

Simplicity is prerequisite for reliability. -- Edsger Dijkstra

Make everything as simple as possible, but not simpler. -- Albert Einstein

The computing scientist's main challenge is not to get confused by the complexities of his own making. -- Edsger Dijkstra

Good design adds value faster than it adds cost. -- Thomas C. Gale

The hardest part of design, is keeping features out. -- Donald Norman

Perfection [in design] is achieved, not when there is nothing more to add, but when there is nothing left to take away. -- Antoine de Saint-Exupéry

A complex system that works is invariably found to have evolved from a simple system that worked. -- John Gall

The belief that complex systems require armies of designers and programmers is wrong. A system that is not understood in its entirety, or at least to a significant degree of detail by a single individual, should probably not be built. -- Niklaus Wirth

One of the great skills in using any language is knowing what not to use, what not to say. There's that simplicity thing again. -- Ron Jeffries

Beauty is more important in computing than anywhere else in technology because software is so complicated. Beauty is the ultimate defence against complexity. -- David Gelernter

Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Martin Golding

The skill of writing is to create a context in which other people can think. -- Edwin Schlossberg

The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. -- Tom Cargill

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

If debugging is the process of removing software bugs, then programming must be the process of putting them in. -- Edsger Dijkstra


First, solve the problem. Then, write the code. -- John Johnson

Sometimes it pays to stay in bed on Monday, rather than spending the rest of the week debugging Monday's code. -- Dan Salomon

I invented the term 'Object-Oriented', and I can tell you I did not have C++ in mind. -- Alan Kay

Measuring programming progress by lines of code is like measuring aircraft building progress by weight. -- Bill Gates


I’ve finally learned what 'upward compatible' means. It means we get to keep all our old mistakes. -- Dennie van Tassel

It always takes longer than you expect, even when you take into account Hofstadter's Law. -- Hofstadter's Law

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski

Before software can be reusable it first has to be usable. -- Ralph Johnson

Engineers are all basically high-functioning autistics who have no idea how normal people do stuff. -- Cory Doctorow

In programming, as in everything else, to be in error is to be reborn. -- Alan Perlis

A good way to stay flexible is to write less code. -- Pragmatic Programmer

The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague. -- Edsger Dijkstra

If you're willing to restrict the flexibility of your approach, you can almost always do something better. -- John Carmack

It's a curious thing about our industry: not only do we not learn from our mistakes, we also don't learn from our successes. -- Keith Braithwaite

Complexity kills. It sucks the life out of developers, it makes products difficult to plan, build and test, it introduces security challenges and it causes end-user and administrator frustration. -- Ray Ozzie

When I am working on a problem I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong. -- R. Buckminster Fuller

[T]he cost of adding a feature isn't just the time it takes to code it. The cost also includes the addition of an obstacle to future expansion. ... The trick is to pick the features that don't fight each other. -- John Carmack

You're bound to be unhappy if you optimize everything. -- Donald Knuth

Good code is short, simple, and symmetrical - the challenge is figuring out how to get there. -- Sean Parent

The trick is to fix the problem you have, rather than the problem you want. -- Bram Cohen

In programming the hard part isn't solving problems, but deciding what problems to solve. -- Paul Graham

It is not that uncommon for the cost of an abstraction to outweigh the benefit it delivers. Kill one today! -- John Carmack

So much complexity in software comes from trying to make one thing do two things. -- Ryan Singer

Premature optimization, that's like a sneeze. Premature abstraction is like ebola; it makes my eyes bleed. -- Christer Ericson

Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise. -- Edsger Dijkstra
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: How beautiful is your code?

Post by lucasart »

michiguel wrote: For instance, a pascal programmer could see a goto and vomit. A C programmer could use it in a way that it very convenient to simplify the code and see it beautiful. I grew as a pascal programmer and I developed an aversion to gotos that I believed played against me when I started with C. Still today I do not use any, when my mind is telling me it could be better to do it in certain cases.
Goto has certainly fueled some heated debates. Here's a very interesting discussion on the subject:
http://kerneltrap.org/node/553/2131
When you read it, you'll quickly feel the difference between dogmatic and psycho-rigid heretics (the goto hate club), and right-minded people whose opinion is forged by experience rather than dogma. OK, I'm biaised... I think goto is a good think, to use in rare occasions when it helps make code more readable.
michiguel wrote: I think we need to keep our minds open. setjmp longjmp is another case that some people have strong disagreements about it, when it comes to "beauty".
Definitely agree about open mindedness. Regarding longjmp, this is not any more dirty than goto or C++ exceptions!
Throwing an exception is nothing else than a longjmp (with the added detail of stack unwinding, calling destructors of objects allocated on the stack before the throw, detail which is a mere C++ adaptation of the C longjmp as there are no destructors in C). So those who think that longjmp are evil should think the same of exceptions or goto. In fact, a "break" or a "continue" is nothing more than a disguised goto, no ? So if you forbid yourself to use exceptions, longjmp, goto, break, continue, you're making your life artificially harder, and your code artificially more complicated (and less readable).

The problem of goto, is that incompetent programmers write spaghetti code with it. Then goto gets blamed instead of incompetence of people who misuse it... Same for pointers: incompetent programmers can;'t handle them correctly, so Java is invented to allow incompetent programmers to still be on the job market, and Java propaganda tells you that pointers are evil...

So open mind is the most important: if these things exist and someone like Linus Torvaldes uses them, you can be sure there is a good reason for it (despite what some dogmatic university professors say). Incompetent people write bad code, with or without goto, setjmp, pointers etc.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
smatovic
Posts: 2658
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: How beautiful is your code? simplicity

Post by smatovic »

hmm, difficult question,
imo it is also about simplicity,
when you express something complex in an simple manner
it can be be beautiful....

in this sense, my code needs a pretty up ;)

--
Srdja
ZirconiumX
Posts: 1334
Joined: Sun Jul 17, 2011 11:14 am

Re: How beautiful is your code?

Post by ZirconiumX »

Beauty is in the eye of the beholder - Miguel would probably vomit at what I consider to be (rather) beautiful.

A switch...case based MakeMove().

Matthew:out
Some believe in the almighty dollar.

I believe in the almighty printf statement.