MyBB Community Forums

Full Version: Using ternary operators in MyBB
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I need an opinion from you.

I want to use the ternary operators as a shorthand way of doing "if " statements with the aim to compact more lines of code into one.

These are two basic examples:

Example 1 (establish if the plugin is already installed or not):
function plugin_is_installed()
{
	global $db;
	return $db->table_exists("plugin_table")?true:false;
}
instead of:
function plugin_is_installed()
{
	global $db;
	if($db->table_exists("plugin_table"))
	{
		return true;
	}
	return false;
}



Example 2 (establish if the plugin is compatible with the installed MyBB version):
function plugin_info()
{
	return array(
		"name" => "Plugin",
		"description" => "Plugin description",
		"website" => "http:/www.website.com",
		"author" => "Author",
		"authorsite" => "http://www.authorsite.com",
		"version" => "1.0",
		"guid" => "",
		"compatibility" => plugin_compatibility(1600, 1800)
	);
}

function plugin_compatibility($min, $max)
{
	global $mybb;
	return $mybb->version_code<$min?$min:($mybb->version_code>$max?$max:$mybb->version_code);
}
instead of:
function plugin_info()
{
	return array(
		"name" => "Plugin",
		"description" => "Plugin description",
		"website" => "http:/www.website.com",
		"author" => "Author",
		"authorsite" => "http://www.authorsite.com",
		"version" => "1.0",
		"guid" => "",
		"compatibility" => plugin_compatibility(1600, 1800)
	);
}

function plugin_compatibility($min, $max)
{
	global $mybb;
	if($mybb->version_code<$min)
	{
		return $min;
	}
	else if($mybb->version_code>$max)
	{
		return $max;
	}
	else
	{
		return $mybb->version_code;
	}
}

This way can actually be detrimental to code readability, but apart of that the code it's correct? Undecided
In essence, I would like to know if it's ok to use ternary operators in plugins. Or should I avoid this practice?
For the second, definitely not. It makes the code much harder to follow through. For the first, you can simplify it even better without ternary operators as table_exists returns true or false anyway.
In your plugin, yes, why not.
Thank you both.
(2014-09-24, 06:29 AM)Euan T Wrote: [ -> ]For the first, you can simplify it even better without ternary operators as table_exists returns true or false anyway.
Right. There is no need to use the ternary operators for 'table_exists'. I just tried to give a simple example,  but it wasn't the most inspired.

(2014-09-24, 06:50 AM)Omar G. Wrote: [ -> ]In your plugin, yes, why not.
(2014-09-24, 06:29 AM)Euan T Wrote: [ -> ]For the second, definitely not. It makes the code much harder to follow through.
I'm still unsure: To be followed by who? Assuming that I'm the only author of the code, I don't see a problem here. IMO, the code looks more compact and using ternary operators can result in code that is actually easier to read than otherwise.

So... despite the debugging process may be slightly more difficult (since I can not place breakpoints on each of the sub expressions), is there any other reason why this practice should be avoided?

Ok, I rephrase the question: if I intend to release a plugin on MyBB, it is recommended to not use ternary operators and make the code more easily to be followed, reviewed and approved by the MyBB Devs?
It's entirely up to you, but remember that you might struggle to understand what's going on in 6 months time Toungue The code is your code and you can do what you want, but I've been bitten by mistakes my past self has made when using "shortcuts" such as ternary operators. I now generally only use them for simple assignments such as the following and never nest them:

$var = isset($postField) ? 'Set' : 'Not Set';
(2014-09-24, 07:52 AM)Euan T Wrote: [ -> ]It's entirely up to you, but remember that you might struggle to understand what's going on in 6 months time Toungue
Well... I would have never thought of THAT! Yes, this could be a problem... especially when will be needed to rewrite some parts of code. Right. Probably it's better to let others easily understand my code’s logic and also, when changes are required, to have a quick and straightforward process.

Thank you, Euan. I will follow your advice. Smile

A good day.
No problemSmile I've made the mistake before and come back to some extremely confusing code!