Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Why do it the easy way?

  • You cannot reply to this topic
12 replies to this topic

#1 Madhed   Crossbones+   -  Reputation: 3137

Like
11Likes
Like

Posted 25 February 2013 - 05:09 PM

Something I found in our javascript codebase

 

{
    //...
    isActive: function(active) {
        return (active ? true : false);
    }
    //...
}

 



Sponsor:

#2 slicer4ever   Crossbones+   -  Reputation: 3986

Like
1Likes
Like

Posted 25 February 2013 - 05:18 PM

I'm so happy we can vote up in this forum=-)


Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#3 Michael Tanczos   Senior Staff   -  Reputation: 5454

Like
6Likes
Like

Posted 25 February 2013 - 06:46 PM

Is it even possible to have *too* many ternary operators?  

 

bool isActive = ((running && !done) ? (done || running) : !(running && done)) ? true : (1==2);



#4 CC Ricers   Members   -  Reputation: 802

Like
7Likes
Like

Posted 26 February 2013 - 12:14 PM

At least it could be worse:

 

return (active ? active : active);


My development blog: Electronic Meteor

#5 Madhed   Crossbones+   -  Reputation: 3137

Like
2Likes
Like

Posted 26 February 2013 - 01:23 PM

Very good ideas for reafctoring in this thread, keep em coming.



#6 Alpha_ProgDes   Crossbones+   -  Reputation: 4692

Like
1Likes
Like

Posted 26 February 2013 - 03:29 PM

if (isActive == TRUE) {
    return 1;
}
else if (isActive == FALSE) {
    return 0;
}
 

Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#7 xycsoscyx   GDNet+   -  Reputation: 319

Like
2Likes
Like

Posted 26 February 2013 - 04:09 PM

How about something akin to what I find in some of my companies legacy code:

 

 

ASSERT(isActive); // We should never be inactive so lets just assert in debug, it'll be fine in release

return TRUE;



#8 Nercury   Crossbones+   -  Reputation: 796

Like
3Likes
Like

Posted 26 February 2013 - 05:25 PM

You have it easy, you are not maintaining legacy PHP code.

This is direct copy-paste from code currently in production.

Guess what is it doing. Hint: in function name.

 

    function extract_where_from_join($on_condition) {
	$where_list = array();
	
	$anal_list = array('="' => '"', '= "' => '"', "='" => "'", "= '" => "'"); 
	
	$anal_pos = FALSE;
	$anal_end_pos = FALSE;
	$found_anal_from = FALSE;
	$found_anal_to = FALSE;
	foreach ($anal_list as $anal_from => $anal_to) {
	    $anal_pos = strpos($on_condition, $anal_from);
	    if ($anal_pos !== FALSE) {
		$found_anal_from = $anal_from;
		$anal_end_pos = strpos($on_condition, $anal_to, $anal_pos + strlen($anal_from));
		if ($anal_end_pos !== FALSE) {
		    $found_anal_to = $anal_to;
		    break;
		}
	    }
	}
	
	if ($anal_pos !== FALSE && $anal_end_pos !== FALSE) {
	    $anal_value = substr($on_condition, $anal_pos + strlen($found_anal_from), $anal_end_pos - $anal_pos - strlen($found_anal_from));
	    $condition = 'and';
	    $not_space_bw_pos = strbipos($on_condition, $condition, $anal_pos);
	    if ($not_space_bw_pos === FALSE) {
		$where_list[] = array(trim(substr($on_condition, 0, $anal_pos)), $anal_value);
		$on_condition = substr($on_condition, $anal_end_pos + strlen($found_anal_to));
		if ($on_condition === FALSE) {
		    $on_condition = 'TRUE';
		}
	    } else {
		$where_list[] = array(trim(substr($on_condition, $not_space_bw_pos + strlen($condition), $anal_pos - $not_space_bw_pos -strlen($condition))), $anal_value);
		$on_condition = substr($on_condition, 0, $not_space_bw_pos) . substr($on_condition, $anal_end_pos + strlen($found_anal_to));
		if ($on_condition === FALSE) {
		    $on_condition = 'TRUE';
		}
	    }
	}
	
	return array($where_list, $on_condition);
    }

 

It turns out this is parsing SQL join statement and extracting it's condition to be used elsewhere as "where" condition. Talking about easy way?


Edited by Nercury, 26 February 2013 - 05:27 PM.


#9 ApochPiQ   Moderators   -  Reputation: 16413

Like
11Likes
Like

Posted 26 February 2013 - 05:29 PM

I'm far more amused than I probably should be by "$found_anal_from".

#10 Michael Tanczos   Senior Staff   -  Reputation: 5454

Like
5Likes
Like

Posted 26 February 2013 - 06:17 PM

You have it easy, you are not maintaining legacy PHP code.

This is direct copy-paste from code currently in production.

Guess what is it doing. Hint: in function name.

 

 

8)   Holy.. anal...   You know, this would be quite an odd naming convention rule to walk into if you got hired by a company.  (Use whatever variable names you want, as long as they include at least one porn industry term)

 

long long johnson = 69 & 0;  // Initialize counter to zero

 

That code looks pretty ridiculous for what it does.  I would think you could use a regex for this without much trouble.


Edited by Michael Tanczos, 26 February 2013 - 06:20 PM.


#11 Sik_the_hedgehog   Crossbones+   -  Reputation: 1834

Like
1Likes
Like

Posted 26 February 2013 - 09:24 PM

Was gonna comment on the original topic, but what the— Abbreviations gone awry o_O And this is why you should avoid abbreviations except for a few well-estabilished ones...

 

As for the original topic, I find this a lot (and yes, with basic types, not classes which could have side-effects):

if (!blah)
   blah = true;

This could have easily done the job:

blah = true;

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#12 Nercury   Crossbones+   -  Reputation: 796

Like
2Likes
Like

Posted 27 February 2013 - 01:03 AM

Was gonna comment on the original topic, but what the— Abbreviations gone awry o_O And this is why you should avoid abbreviations except for a few well-estabilished ones...

 

As for the original topic, I find this a lot (and yes, with basic types, not classes which could have side-effects):

 

if (!blah)
   blah = true;

This could have easily done the job:

 

blah = true;

 

Unless it is dynamically typed language (like javascript):

 

var blah = "hello";
if (!blah)
    blah = true;

// blah remains "hello"

blah = 15;
if (!blah)
    blah = true;

// blah remains 15

blah = 0;
if (!blah)
    blah = true;

// blah is true 

 

I hate dynamically typed languages for allowing this.



#13 Sik_the_hedgehog   Crossbones+   -  Reputation: 1834

Like
0Likes
Like

Posted 27 February 2013 - 07:56 AM

The examples of that I see are usually in C or C++ though...


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.





PARTNERS