3

In how far do we create variables in our methods or functions?

Do we only create one when we're using the result of the variable more then one time like this?

function someFunction(SomeClass $someClass) {
    $thisVar = $someClass->thisVar();
    doSomethingElse($thisVar);
    echo $thisVar;
}

Or just always create one no matter how many times we're using it (for better readability)?

Or just call the methods from the dependency directly like this?

function someFunction(SomeClass $someClass) {
    doSomethingElse($someClass->thisVar());
    echo $someClass->thisVar();
}
  • I'll go for the second option. If you are not modifying it and don't want to actually change it, than it's always better to call it where you use it. – tikend Jan 28 '15 at 10:02
  • So only make a variable when you're using it more then one time? – Bas Jan 28 '15 at 10:03
  • 1
    Basically all of this as needed. You can use variables for the sole purpose of increasing readability (though you don't have to always do this, just use where it makes sense). In some cases you may even split a function in several sub functions for readability, even if those new functions are only called once. Your second example may not be too great, but if there is some deep nesting it may become more interesting. Also when you have rather complex calculations that become difficult to read you may split them into smaller steps that way. – thorsten müller Jan 28 '15 at 10:04
  • Only make variable if it's new, e.g. if you want to add something to existing variable and than use it. Even if you are just returning it modified, there is no need to create new variable. If you are using old variable than just call it. – tikend Jan 28 '15 at 10:04
3

One reason for introducing a separate variable is to improve readability, assumed the real name of $someClass->thisVar() is not expressive enough. This can make sense even if the function is only called once:

function someFunction(SomeClass $someClass) {
    $explainingName = $someClass->thisVar();
    doSomethingElse($explainingName);
}

Of course, if you think the function name is clear enough, this variant

function someFunction(SomeClass $someClass) {
    doSomethingElse($someClass->selfExplanatoryName());
}

might be fine. But even in such a case, when both function names are very long,

function someFunction(SomeClass $someClass) {
    doSomethingElseWithAVeryLongFunctionName($someClass->selfExplanatoryButVeryLongName());
}

might be considered to be less readable than

function someFunction(SomeClass $someClass) {
     $thisVar = $someClass->selfExplanatoryButVeryLongName();
     doSomethingElseWithAVeryLongFunctionName($thisVar);
}

When you need to reuse the value at least twice like in your examples, a reason might be that $someClass->thisVar() must be called only once because of side effects, or it should be called only once because of a noteable performance impact.

A third reason might be to avoid code duplication. Think of this:

function someFunction(SomeClass $someClass) {
    doSomethingElse($someClass->thisVar($parameter1,$parameter2,$parameter3));
    echo $someClass->thisVar($parameter1,$parameter2,$parameter3);
}

here $someClass->thisVar($parameter1,$parameter2,$parameter3) is repated twice, which violates the dry principle. Your second example is an edge case: it is also a violation of the DRY principle, but not a severe one, and to avoid it you have to introduce more code than it actually saves you. So it is a trade-off with no clear "best" solution.

  • Yeah indeed, so only make one when you're making more use out of it? – Bas Jan 28 '15 at 10:14
  • @Bas there are really lots of cases where variables make sense even if only used once. Think of very long and complex calculations or string concatenations for example when you generate html or sql strings (in many cases there may be better options than string concatenation to do this but if you have to having subsections with clear names may make such things much more readable) – thorsten müller Jan 28 '15 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.