Clear coding ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Clear coding ?

Post by Henk »

1) I don't like '||' anymore.

Might as well write

if (a) { c }
else if (b) { c }

instead of if (a || b) {c}

Or not ?

Maybe || is shorter but chances are bigger that you overlook a condition.

2) I also don't like 'else'.

So better write

if (a) {c }
if (!a && b) {c }

Don't care about low level efficiency.


3) Eliminating '&&' too. You get:

if (a) {c}
if (!a)
{
if (b) { c}
}

Perhaps step 3 goes too far.
Last edited by Henk on Sat Oct 03, 2015 12:44 pm, edited 1 time in total.
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Clear coding ?

Post by hgm »

I don't like if(). Instead of

if(x < 0) x = 0;

I always write

x &= -(x >= 0);

Or better yet

x &= ~x>>31;
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Clear coding ?

Post by Henk »

hgm wrote:I don't like if(). Instead of

if(x < 0) x = 0;

I always write

x &= -(x >= 0);

Or better yet

x &= ~x>>31;
help
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Clear coding ?

Post by hgm »

Of course when you care about readability, you could do

Code: Select all

#define CLIP_OFF_NEGATIVE_PART&#40;x&#41; x &= ~x>>31

CLIP_OFF_NEGATIVE_PART&#40;x&#41;;
If I need

if(A) B=C;

I usually write

B = A ? C : B;
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Clear coding ?

Post by Henk »

hgm wrote:Of course when you care about readability, you could do

Code: Select all

#define CLIP_OFF_NEGATIVE_PART&#40;x&#41; x &= ~x>>31

CLIP_OFF_NEGATIVE_PART&#40;x&#41;;
If I need

if(A) B=C;

I usually write

B = A ? C : B;
I would use 3DES
User avatar
Kempelen
Posts: 620
Joined: Fri Feb 08, 2008 10:44 am
Location: Madrid - Spain

Re: Clear coding ?

Post by Kempelen »

why dont you use defines:

#define OR ||

if (a OR b) {...}

or

#define OR(a,b) ((a) || (b))

if OR(a,b) {....}
Fermin Serrano
Author of 'Rodin' engine
http://sites.google.com/site/clonfsp/
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: Clear coding ?

Post by Rein Halbersma »

Kempelen wrote:why dont you use defines:

#define OR ||

if (a OR b) {...}

or

#define OR(a,b) ((a) || (b))

if OR(a,b) {....}
From http://en.cppreference.com/w/cpp/langua ... lternative

In C:

Code: Select all

#include <iso646.h>
if &#40;a or b&#41; &#123; ... &#125;
In C++:

Code: Select all

if &#40;a or b&#41; &#123; ... &#125;
mvk
Posts: 589
Joined: Tue Jun 04, 2013 10:15 pm

Re: Clear coding ?

Post by mvk »

hgm wrote:I don't like if(). Instead of

if(x < 0) x = 0;

I always write

x &= -(x >= 0);

Or better yet

x &= ~x>>31;
I usually do this:

Code: Select all

x = max&#40;x, 0&#41;;
[Account deleted]
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Clear coding ?

Post by Henk »

I use C#.NET which does not have #define.
I thought .NET framework would be beneficial for it contains many classes you can use.


By the way I don't care about '||' but if you have something like
if (a && b && (c || d || e || f))
you might overlook d.

Actually I would like to write fool proof code as much as possible without adding extra complexity.
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Clear coding ?

Post by AlvaroBegue »

And this goes right to the pile of evidence that Henk is a troll.