False VS return error

I am trying to understand the main difference between returning false from an instruction as opposed to an error echo that allows the user to correct their representation.

Let's take the following function, which is used to get the URL of the conversion API in Google, and analyze three more parameters $amount, $from, $to . I use explode to get the numeric values โ€‹โ€‹returned by the API "" .

 function currency_convert($googleCurrencyApi, $amount, $from, $to) { $result = file_get_contents($googleCurrencyApi . $amount . $from . '=?' . $to); $expl = explode('"', $result); if ($expl[1] == '' || $expl[3] == '') { return false; } else { return array( $expl[1], $expl[3] ); } } 

What is the advantage of returning false if the statement is true compared to the echo from the construct message? I see a false return regularly used in many forums, etc.

Thanks in advance.

+4
source share
4 answers

Neither we, nor 2012, throw an Exception or handle it in any way.

 function currency_convert($googleCurrencyApi, $amount, $from, $to) { $result = file_get_contents($googleCurrencyApi . $amount . $from . '=?' . $to); $expl = explode('"', $result); if ($expl[1] == '' || $expl[3] == '') { throw new Exception('An error has occured. Describe the error here'); } return array( $expl[1], $expl[3] ); } 

Then when you call the function:

 try { currency_convert($googleApi, $amount, $from, $to) } catch (Exception $e) { /* Do whatever you want with $e */ } 

Read about Exceptions and try, catch block here

Benefits

  • If it is raw, it will stop the script, quickly determining where the problem is.
  • If processed, it can be easily processed as if the return false event occurred.
  • Exception stops the function, which means that the return statement will never appear.
  • An exception may display a constructive message to the user, and it may be useful for developers to find out what the error is.
  • Exceptions are objects whose classes can be extended , so you can effectively create several types of errors and exceptions , such as, but not limited to: IllegalArgumentException , MathException , FileReadException , ReallyAwesomeException , and then handle everything differently

     try { /* Code Here */ } catch (IllegalArgumentException $e) { echo 'Illegal Argument!'; } catch (Exception $e) { echo 'General Error! '. $e->getMessage(); } 
+5
source

There are no advantages from each other, it all depends on how you structure your code. You can return false to the client side method that displays the error.

It's all about what you do with the return value. If false, the message "failed" else "success" is displayed.

+1
source

Why? Because:

  • Reusing codes can change it more easily, and they can change what to do.
  • You can make and check easier in one step.
  • Repetition of the error will not be constructive on the code side, only on the user side. Returning false is useful for both.
+1
source

Functions / methods should only output anything if that is their goal. To give an example:

Bad:

 function showSomething($something) { $something = doSomething($something); echo $something; } function doSomething($something) { if (empty($something)) { echo 'ERROR!'; } return $something; } 

Good:

 function showSomething($something) { $something = doSomething($something); if ($something === FALSE) { echo 'ERROR!'; } else { echo $something; } } function doSomething($something) { if (empty($something)) { return FALSE; } return $something; } 
+1
source

Source: https://habr.com/ru/post/1395639/


All Articles