Why do it the easy way?

Started by
11 comments, last by Sik_the_hedgehog 11 years, 1 month ago

Something I found in our javascript codebase


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

Advertisement

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.

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

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

At least it could be worse:

return (active ? active : active);

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

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


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

Beginner in Game Development?  Read here. And read here.

 

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;

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?

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

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.

This topic is closed to new replies.

Advertisement