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.
Clear coding ?
Moderators: hgm, Rebel, chrisw
-
- Posts: 7220
- Joined: Mon May 27, 2013 10:31 am
Clear coding ?
Last edited by Henk on Sat Oct 03, 2015 12:44 pm, edited 1 time in total.
-
- Posts: 27807
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Clear coding ?
I don't like if(). Instead of
if(x < 0) x = 0;
I always write
x &= -(x >= 0);
Or better yet
x &= ~x>>31;
if(x < 0) x = 0;
I always write
x &= -(x >= 0);
Or better yet
x &= ~x>>31;
-
- Posts: 7220
- Joined: Mon May 27, 2013 10:31 am
Re: Clear coding ?
helphgm 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;
-
- Posts: 27807
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Clear coding ?
Of course when you care about readability, you could do
If I need
if(A) B=C;
I usually write
B = A ? C : B;
Code: Select all
#define CLIP_OFF_NEGATIVE_PART(x) x &= ~x>>31
CLIP_OFF_NEGATIVE_PART(x);
if(A) B=C;
I usually write
B = A ? C : B;
-
- Posts: 7220
- Joined: Mon May 27, 2013 10:31 am
Re: Clear coding ?
I would use 3DEShgm wrote:Of course when you care about readability, you could do
If I needCode: Select all
#define CLIP_OFF_NEGATIVE_PART(x) x &= ~x>>31 CLIP_OFF_NEGATIVE_PART(x);
if(A) B=C;
I usually write
B = A ? C : B;
-
- Posts: 620
- Joined: Fri Feb 08, 2008 10:44 am
- Location: Madrid - Spain
Re: Clear coding ?
why dont you use defines:
#define OR ||
if (a OR b) {...}
or
#define OR(a,b) ((a) || (b))
if OR(a,b) {....}
#define OR ||
if (a OR b) {...}
or
#define OR(a,b) ((a) || (b))
if OR(a,b) {....}
-
- Posts: 741
- Joined: Tue May 22, 2007 11:13 am
Re: Clear coding ?
From http://en.cppreference.com/w/cpp/langua ... lternativeKempelen wrote:why dont you use defines:
#define OR ||
if (a OR b) {...}
or
#define OR(a,b) ((a) || (b))
if OR(a,b) {....}
In C:
Code: Select all
#include <iso646.h>
if (a or b) { ... }
Code: Select all
if (a or b) { ... }
-
- Posts: 589
- Joined: Tue Jun 04, 2013 10:15 pm
Re: Clear coding ?
I usually do this: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;
Code: Select all
x = max(x, 0);
[Account deleted]
-
- Posts: 7220
- Joined: Mon May 27, 2013 10:31 am
Re: Clear coding ?
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.
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.
-
- Posts: 931
- Joined: Tue Mar 09, 2010 3:46 pm
- Location: New York
- Full name: Álvaro Begué (RuyDos)
Re: Clear coding ?
And this goes right to the pile of evidence that Henk is a troll.