There is such a small function. Is there a way to make it more capacious and productive? Can another construction (not if else) or otherwise implement a comparison? PS learn PHP is just starting

function game_parameters_check($gameMode, $level, $betValue) { if($gameMode != 'real' && $gameMode != 'demo') { exit(); } if($level != 'easy' && $level != 'medium' && $level != 'hard') { exit(); } if($betValue < 15 || $betValue > 500) { exit(); } } 
  • one
    Well, for starters, the name of functions is usually written in lowerCamelCase , just like your variables. And so, there is nothing particularly to optimize on the code, except to throw everything into one condition - it’s worth duplicating exit(); ? - InDevX
  • one
    Well, you can somehow make $levels = ['easy','medium','hard']; if ( !in_array($level,$levels) ) {...} $levels = ['easy','medium','hard']; if ( !in_array($level,$levels) ) {...} again, it is very difficult to influence the performance of your example - InDevX

3 answers 3

PS learn PHP is just starting

My answer is not about how to write the code in three lines.

1 Function Name

Usually the verb is put at the beginning of the name: get, set, update, check, validate, etc. The name of the function / method should reflect what she / he is doing. Thus check_game_parameters : checking game parameters.

2 Purpose of the function

Such an abundance of exit in one function and without displaying error messages is not serious. Another couple of such functions and then tortured to run around the application with var_dump .

If this is a validation / validation function, then it should at least return true or false . Thus: if the passed game parameters satisfy some predetermined conditions, then return true . In all other cases, false .

3 Function Arguments

At the input we have three arguments, each of which passes through two or three checks. Total for the current moment - 7 checks .

Almost always problems arise because of too long methods, often containing a lot of information, buried under complex logic, which they usually contain in themselves. The main type of refactoring here is the “Method Allocation”, as a result of which the code fragment becomes a separate method. - Martin Fowler .

In our case, not a method, but a function. Three arguments are three separate functions, from which we need either true or false .

4 Naming Arguments

  1. $gameMode - we check the parameters of the game, therefore the prefix game superfluous.
  2. $level fine.
  3. $betValue - out of context, and so it is clear that this value.

 echo '<pre>'; function check_game_parameters($mode, $level, int $bet) { return validate_mode($mode) && validate_level($level) && validate_bet($bet); } function validate_mode($mode) { return in_array($mode, [ 'real', 'demo' ]); } function validate_level($level) { return in_array($level, [ 'easy', 'medium', 'hard' ]); } function validate_bet(int $bet) { return $bet >= 15 && $bet <= 500; } if (!check_game_parameters('real', 'hard', 15)) { echo 'Проверка не пройдена<br>'; exit(); } echo 'Проверка пройдена<br>'; 
  • Thanks for the detailed answer! In the process of writing, I decided to display messages of one or another error into exit (), but I will consider your version for the future for similar situations. I also think it is possible to combine your approach and the output of different messages by comparing the received lines of auxiliary functions in the summarizing function check_game_parameters () (described them in general). - orel-22

Well, only two options come to mind:

 function game_parameters_check($gameMode, $level, $betValue) { if($gameMode != 'real' && $gameMode != 'demo') exit(); if($level != 'easy' && $level != 'medium' && $level != 'hard') exit(); if($betValue < 15 || $betValue > 500) exit(); } 

Or else

 function game_parameters_check($gameMode, $level, $betValue) { if(!in_array($gameMode, [ 'real', 'demo' ])) exit(); if(!in_array($level, [ 'easy', 'medium', 'hard' ])) exit(); if($betValue < 15 || $betValue > 500) exit(); } 

In the second variant, it is better, of course, to translate the gameMode variable into lower case.

     function check_game_parameters($gameMode, $level, $betValue) { $allowedRule = [ 'level' => ['easy','medium','hard'], 'game_mode' => ['real','demo'], ]; if(! in_array($gameMode,$allowedRule['game_mode']) || ! in_array($level,$allowedRule['level'])) exit(); if($betValue <= 15 || $betValue > 400) exit(); } 

    And if in the future one more level will be added, or a game mode? add new if's? In this case, it is enough to add this new mode \ level to the $allowedRule