If I was to call a method which took a parameter, and then defined a variable at the same time, would that be considered "bad practice"?
Example:
if( file_exists( $file = "skins/Default/Controllers/Demo.php" ) )
{
require( $file );
}
I feel as though it makes things easier as it doesn't require creating another variable above, nor does it clutter the code up by writing the string twice.
Is declaring a variable inside of calling a method bad practice?
Yes, because it hides the intent behind other functionality.
$file = "skins/Default/Controllers/Demo.php";
if (file_exists($file)) {
require($file);
}
is easier to read and reason about than:
if (file_exists($file = "skins/Default/Controllers/Demo.php")) {
require($file);
}
because it'd be easily mistaken for $file == "skins/Default/Controllers/Demo.php"
, which is common to see within an if
statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With