[Vtigercrm-developers] A question on efficiency/semantics...

Sukhdev Mohan s.mohan at myti.it
Wed May 5 15:04:09 GMT 2021


>> if ($variableList) {
> 
> would provide the same result? To me that simple if($var) statement says, "if $var is not equal to a boolean false, or 0, or null then continue"...

Yup, $var = [] is still 0 so it’s a falsy value (love the C based programming languages).

I’d like to propose, let’s anticipate the rejection instead of nested ifs for the expected values (normally ifs should be used to handle exceptions not for the basic functionalities), like this:

If(!$var) { // check what will break the code
    // reject statement and exit routine
}

// continue the normal flow

This would also improve readability as it’s short and you immediately see what can cause a break.

Best Regards,
Sukhdev Mohan | Software Developer







> Il giorno 5 mag 2021, alle ore 16:14, Alan Lord <alanslists at gmail.com> ha scritto:
> 
> In the fairly recent past there have been a number of minor commits made which, I guess, are to help prevent php WARNING and/or NOTICE type messages, e.g.
> 
> Something like this:
> 
>> if (count($variableList) > 0) {
> 
> became this:
> 
>> if (!empty($variableList) && (count($variableList) > 0)) {
> 
> But this seem to be overkill...
> 
> As I understand things simply doing this:
> 
>> if ($variableList) {
> 
> would provide the same result? To me that simple if($var) statement says, "if $var is not equal to a boolean false, or 0, or null then continue"...
> 
> count($var) would return an integer if $var is a string, so it is not a useful explicit test for an array.
> 
> And from the php manual, empty($var) says "a variable is considered empty if it does not exist or if its value equals false"
> 
> 
> Thoughts / Comments?
> 
> 
> Al
> 
> _______________________________________________
> http://www.vtiger.com/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20210505/a3c52b3a/attachment.html>


More information about the vtigercrm-developers mailing list