2014-09-24, 05:59 AM
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):
Example 2 (establish if the plugin is compatible with the installed MyBB version):
This way can actually be detrimental to code readability, but apart of that the code it's correct?
In essence, I would like to know if it's ok to use ternary operators in plugins. Or should I avoid this practice?
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?
In essence, I would like to know if it's ok to use ternary operators in plugins. Or should I avoid this practice?