How beautiful is your code?
Moderators: hgm, Rebel, chrisw
-
- Posts: 6995
- Joined: Thu Aug 18, 2011 12:04 pm
Re: How beautiful is your code?
With a goto you can make your code somewhat faster when you know the branch prediction rate.
-
- Posts: 2949
- Joined: Mon May 05, 2008 12:16 pm
- Location: Bordeaux (France)
- Full name: Julien Marcel
Re: How beautiful is your code?
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 ]
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
-
- Posts: 27809
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: How beautiful is your code?
You must like the micro-Max code, then. I know I do!
-
- Posts: 3232
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: How beautiful is your code?
The code is indeed beautiful and self-documenting. No need to add comments, it's cristal clear:hgm wrote:You must like the micro-Max code, then. I know I do!
Code: Select all
#define F(I,S,N) for(I=S;I<N;I++)
#define W(A) while(A)
int V=112,M=136,I=8e3,C=799,X,Y,Q,N,
d[]={-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},
b[128]={6,3,5,7,4,5,3,6};
char n[]=".?+pkltd?*?PKLTD";
D(k,q,l,e,x,n)
int k,q,l,e,x,n;
{
int i=0,j,t,p,u,r,y,m=n>1|q>e?q:e,v,h,z;
N++;
do{
u=b[i];
if(u&k)
{r=p=u&7;
j=d[p+23];
W(r=p>2&r<0?-r:-d[++j])
{y=i;
do
{y+=r;if(y&M)break;
t=b[y];if(t&k|p<3&!(r&7)-!t)break;
v=99*d[t&7|16];
if(v<0)m=I;
if(m>=l)return m;
if(h=n-(y!=x))
{if(p<6)v+=b[i+8]-b[y+8];
if(p==4)v+=9*(e>C?!((x^y)&68):-3);
b[i]=0;b[y]=u;
if(p<3)
{v-=9*(((i-2)&M||b[i-2]!=u)+((i+2)&M||b[i+2]!=u)-1);
if(y+r+1&128){b[y]|=7;v+=C;}
}
v=-D(24-k,-l,-m,z=-e-v,y,h);
v-=v>I/3;
if(x==9){if(v+I&&i==X&y==Y){Q=z;return l;}v=m;}
b[i]=u;b[y]=t;
if(v>m){m=v;if(x&8){X=i;Y=y;}}
}
t+=p<5;if(p<3&&6*k+(y&V)==128)t--;
}W(!t);
}
}
}W(i=i+9&~M);
return m+I?m:-D(24-k,-I,I,0,x,1)/2;
}
main()
{
int k=8,*p,c[9],d;
F(X,0,8)
{b[X+V]=(b[X]+=16)-8;b[X+16]=18;b[X+96]=9;
F(Y,0,8)b[16*Y+X+8]=(X-4)*(X-4)+(Y-3.5)*(Y-3.5);
}
W(1)
{F(N,0,121)printf(" %c",N&8&&(N+=7)?10:n[b[N]&15]);
p=c;W((*p++=getchar())>10);
if(*c-10){X=*c-16*c[1]+C;Y=c[2]-16*c[3]+C;}else
{d=6;N=0;W(N<1e6)D(k,-I,I,Q,8,d++);}
if(D(k,-I,I,Q,9,2)==I)k^=24;
}
}
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
-
- Posts: 27809
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: How beautiful is your code?
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())
#define H(z)*n++=z;
#include <setjmp.h>
#define v main(0,0,0
#define Z while(
#define _ if(
#define o(d) (u[l]=0,l[d]=6^e,q=1e4>v,0),l[d]=0,u[l]=e^6,q)
#define I(H,n) { _ r=l[x=H],!r|(r^e)<-1){ _ j=u[l],-7==r|6==r\
){ n; e=~e; return 1e5- f; } u[l]=0,t=j+1,i=j-1; _!i&89<x)i\
=j,t=6; _-1==t&30>x)t=j,i=-7; Z++i<t){ b=S; d=0; S&=63; \
a=((j^e)!=1?6!=(j^e)?O[32+x/10]-O[u/10+32]-q:(S|=6!=j?8\
:1,2==u-x)*9+9*(x-u==2):(d=1==j?x-u:u-x)/8+!(!((x-u)%\
10)|r)*99+(j==1?90<x:29>x)*(9*O[28+i]-288))+O[r+28\
]*9-288+O[x%10+33]-f-O[33+u%10]; x[l]=i; S|=(21=\
=u|21==x)*2+(u==28|28==x)*4+(91==u|x==91)*16+32\
*(u==98|x==98)+(20==d)*64*x; a-=k>f?main(a,f+1\
,M,k):0; _ i==c&u==h&!f&N&a>-1e4&x==y)longjm\
p(z,1); S=b; _!N|f&&(a>M||!f&a==M&&1&rand()\
)){ _!f){ _ k){ c=i; h=u; y=x; } } else _ \
L-a<N){ n; e=~e; u[l]=j; x[l]=r; return\
a; } M=a; } } x[l]=r; u[l]=j; n; } }
typedef int G; char J [ 78 ], O [ ]
= "HRQAMS#-smaqrh[UTZYTU[|TBA("
"$#(ABT|ba`gg`ab8>GK[_`fFDZXEYR" "L\t####"
"##B#A#@#G#F#E#D#K\t\3Zlv#tjm" "\3J#tjm\3Pwb"
"ofnbwf\3Joofdbo\3)&`&`.&`&`" "#+&g*\t"; G y,
c,h,e,S,*s,l[149]; jmp_buf z ; G main(G L,G f,
G N,G k){ G u=99,p,q,r,j,i,x ,t, a, b,d,M=-1e9
; char *n; if( *l){ e=~e; Z u >21){ q= l[--u]^e;
_!-- q){ _!l[p=e?u-10:u+10]){ I(p,)_ e?u>80 & !l[p
-=10]:u<39&!l[p+=10])I(p,)} _ l[p=e?u-11:9+u] )I(p,)
else _ u-1==S>>6){ l[u-1]=0; I(p,l[u-1]=-2^e); } _ l[
p=e?u-9:11+u])I(p,)else _ S>>6==1+u){ l[1+u]=0; I(p,l
[1+u]=e^-2); } } _!--q){ n=O+41; Z++n<50+O)I(u+80-*n,
)} _ 0<q&4>q){ n=q==2?53+O:O+49; Z++n<O+(q!=1)*4+54
){ p=u; do I(p-=*n-80,)Z!p[l]); } } _ 4==q){ n=49+O
; Z++n<O+58)I(u-*n+80,)_ e&!(S&24)|!e&!(S&3)&&k&&
!l[u-2]&!l[u-1]&!l[u-3]&&o(u)&o(u-1)){ l[u-1]= 4
^e; l[u-4]=0; I(u-2,l[u-1]=0; l[u-4]=e^4); } _
e&!(S&40)|!e&!(S&5)&&k&&!l[u+1]&!l[2+u]&&o(u)&
o(1+u)){ l[u+1]=e^4; l[3+u]=0; I(u+2,l[1+u
]=0; l[u+3]=4^e); } } } e=~e; return M; }
Z h<130){l[h]=-(21>h|98<h|2 >(h+1 )%
10); O[h++]^=3; } n=O +14; s=20+l; Z
++s<29+l){ 10[s]=1; 70[s]=~ ( * s = *
n++ -+84); 60 [ s] =-2; } Z n=J){ puts
(58+O); u=19; Z++u<100){ H(32)_!( u%10
))H(32)H(O[7+l[u]])_(9+u)%10>7){ H(58
-u/10)H(32)_ u&1)puts(n=J); } } puts
(O+58); _-1e4 >v , 1)){ e=~e; puts
(O+(v,0)> 1e4?e?90:82:96)); break
; } _ 1<L&e) { d=v,2+L); printf
(O+114,h%10+64,58-h/10,y%10+64
,58 -y/10,d); } else{ putchar
(62 ) ; h= (95 & F-44; c=l[h
+=(56-F *10]; y=(95&F-44; y
+=(56-F*10; Z 10!=(u=(95
&F)){ c=5; Z--c>1&&u!=c
[O]); c=e^c-7; } } _!
setjmp(z)){ v+1,1);
puts( 106+
O); } } Z
10!=
F; }
-
- Posts: 2949
- Joined: Mon May 05, 2008 12:16 pm
- Location: Bordeaux (France)
- Full name: Julien Marcel
Re: How beautiful is your code?
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. )hgm wrote:You must like the micro-Max code, then. I know I do!
"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 ]
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
-
- Posts: 838
- Joined: Thu Jul 05, 2007 5:03 pm
- Location: British Columbia, Canada
Re: How beautiful is your code?
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!michiguel wrote: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.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 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
-
- Posts: 3232
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: How beautiful is your code?
Goto has certainly fueled some heated debates. Here's a very interesting discussion on the subject: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.
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.
Definitely agree about open mindedness. Regarding longjmp, this is not any more dirty than goto or C++ exceptions!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".
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.
-
- Posts: 2658
- Joined: Wed Mar 10, 2010 10:18 pm
- Location: Hamburg, Germany
- Full name: Srdja Matovic
Re: How beautiful is your code? simplicity
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
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
-
- Posts: 1334
- Joined: Sun Jul 17, 2011 11:14 am
Re: How beautiful is your code?
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
A switch...case based MakeMove().
Matthew:out
Some believe in the almighty dollar.
I believe in the almighty printf statement.
I believe in the almighty printf statement.