Page 1 of 1

CODE PUZZLES FROM OD :)

PostPosted: 13 May 2012, 12:08
by paul424
In this threat I encourage others and me to post some weird or overtalked code fragments from existing OD's code >_< :

1 Tile.cpp . How the internal if - else statement could be "pulled out ":) ?
{l Code}: {l Select All Code}
**********************************************************************************************************************************************************************************************




        if (ss)
        {
            //FIXME:  This code should be moved over to the rendering thread and called via a RenderRequest
            if (thisRequestIsForMe&&(ent!=NULL))
            {
                ent->setVisible(true);
            }
            addPlayerMarkingTile(p);
        }
        else
        {
            //FIXME:  This code should be moved over to the rendering thread and called via a RenderRequest
            if (thisRequestIsForMe&&(ent!=NULL))
            {
                ent->setVisible(false);
            }
            removePlayerMarkingTile(p);
        }

**********************************************************************************************************************************************************************************************

Re: CODE PUZZLES FROM OD :)

PostPosted: 21 May 2012, 22:08
by dusted
Since ss evaluates to true, I'd be surprised if "true" is not simply anything that evaluates to true.

{l Code}: {l Select All Code}

if (thisRequestIsForMe&&(ent!=NULL))
{
    ent->setVisible(ss);
}

if (ss)
{
  addPlayerMarkingTile(p);
} else {
  removePlayerMarkingTile(p);
}



However, if true is not simply something that evaluates to true, you could do this instead:

{l Code}: {l Select All Code}
if (thisRequestIsForMe&&(ent!=NULL))
{
    ent->setVisible( (ss)?true:false );
}

Re: CODE PUZZLES FROM OD :)

PostPosted: 22 May 2012, 09:21
by Zlodo
If ss needs to be converted to bool the following looks a bit cleaner imo:

ent->setVisible( !!ss );

Re: CODE PUZZLES FROM OD :)

PostPosted: 22 May 2012, 21:49
by MCMic
I find (ss?true:false) way clearer than !!ss. (If I read code and see !!ss, I'm thinking "error").
But can't you just cast it to boolean?

Re: CODE PUZZLES FROM OD :)

PostPosted: 22 May 2012, 23:45
by svenskmand
Is ss a bool? If so and you do not like !ss, which very common by the way, then use ss==false, which is as clear as it gets imo.

Re: CODE PUZZLES FROM OD :)

PostPosted: 23 May 2012, 08:25
by dusted
if it's not 0, it's true, yayc (L)

Re: CODE PUZZLES FROM OD :)

PostPosted: 23 May 2012, 13:41
by Zlodo
MCMic {l Wrote}:I find (ss?true:false) way clearer than !!ss. (If I read code and see !!ss, I'm thinking "error").
But can't you just cast it to boolean?

Yes, you can also do that. It comes down to a matter of taste, personally I prefer the double-bang because I think it gets the point across well enough without adding too much noise.

Re: CODE PUZZLES FROM OD :)

PostPosted: 26 May 2012, 16:11
by MCMic
svenskmand {l Wrote}:Is ss a bool? If so and you do not like !ss, which very common by the way, then use ss==false, which is as clear as it gets imo.

No the problem was not that I did not like the !ss, I like it, I don't like the double one !!ss, used to cast to boolean, I find (bool)ss much clearer.