Reputation: 332
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.
Upvotes: 4
Views: 55
Reputation: 179046
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.
Upvotes: 7
Reputation: 1899
it could be considered as bad practice, as it reduces readability (=> maintainability) of the code.
$file = "skins/Default/Controllers/Demo.php";
if( file_exists( $file ) )
{
require( $file );
}
is more readable
Upvotes: 0