CODE PUZZLES FROM OD :)

CODE PUZZLES FROM OD :)

Postby paul424 » 13 May 2012, 12:08

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);
        }

**********************************************************************************************************************************************************************************************
User avatar
paul424
OD Moderator
 
Posts: 660
Joined: 24 Jan 2012, 13:54

Re: CODE PUZZLES FROM OD :)

Postby dusted » 21 May 2012, 22:08

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 );
}
User avatar
dusted
 
Posts: 83
Joined: 27 Feb 2010, 04:35

Re: CODE PUZZLES FROM OD :)

Postby Zlodo » 22 May 2012, 09:21

If ss needs to be converted to bool the following looks a bit cleaner imo:

ent->setVisible( !!ss );
Zlodo
 
Posts: 17
Joined: 14 Dec 2009, 12:36

Re: CODE PUZZLES FROM OD :)

Postby MCMic » 22 May 2012, 21:49

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?
User avatar
MCMic
 
Posts: 723
Joined: 05 Jan 2010, 17:40

Re: CODE PUZZLES FROM OD :)

Postby svenskmand » 22 May 2012, 23:45

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.
Jamendo.com - The best music store on the net, uses CC licenses.
User avatar
svenskmand
OD Moderator
 
Posts: 1850
Joined: 09 Dec 2009, 00:07
Location: Denmark

Re: CODE PUZZLES FROM OD :)

Postby dusted » 23 May 2012, 08:25

if it's not 0, it's true, yayc (L)
User avatar
dusted
 
Posts: 83
Joined: 27 Feb 2010, 04:35

Re: CODE PUZZLES FROM OD :)

Postby Zlodo » 23 May 2012, 13:41

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.
Zlodo
 
Posts: 17
Joined: 14 Dec 2009, 12:36

Re: CODE PUZZLES FROM OD :)

Postby MCMic » 26 May 2012, 16:11

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.
User avatar
MCMic
 
Posts: 723
Joined: 05 Jan 2010, 17:40

Who is online

Users browsing this forum: No registered users and 1 guest