questions about comparisions in the PHP code base (Technics)
Hello
During refactoring the include-files I found several PHP-code blocks I tend to describe as at least disadvantageous, with a bad performance or not well maintainable.
I found bunches of strings to compare with the value of an URL-parameter (i.e. for sorting the users list), same for recurring test for sorting direction (ASC
, DESC
). I would like to replace such if-munsters with a simple in_array
…
// actual status if ($order != 'user_id' && $order != 'user_name' && $order != 'user_email' && $order != 'user_type' && $order != 'registered' && $order != 'logins' && $order != 'last_login' && $order != 'user_lock' && $order != 'user_hp' && $order != 'email_contact' && $order != 'online') $order = 'user_name'; // what I'm intended to reach $orderValues['users_list'] = array('user_id', 'user_name', 'user_email', 'user_type', 'registered', 'logins', 'last_login', 'user_lock', 'user_hp', 'email_contact', 'online'); if (!in_array($order, $orderValues['users_list'])) $order = 'user_name';
… which led me to a few questions.
Is an array of allowed values maintainable? And consequential:
- How often these lists was changed in the past?
- How often it is expectable to be changed in the (near) future?
Where such lists should be stored to keep them globally accessible for all of our scripts?
As second, i found many occurences of comparisions in a if-else-manner which are IMHO better readable in a ternary operation.
// I found as an example: if (isset($_REQUEST['action'])) $action = $_REQUEST['action']; else $action = 'main'; // IMHO better $action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : 'main';
The ternary operator is in use at many places but because of the development over (around) 10 years, the notation of such checks is very non-uniform. Is it worth the effort to to comb through the scripts to replace the "old" with the "new" notation only for developers better readability of the scripts and a few hundred or thousand bytes less code?
Tschö, Auge
--
Trenne niemals Müll, denn er hat nur eine Silbe!