Daniel Rikowski
Daniel Rikowski

Reputation: 72514

Is it bad practice to use temporary variables to avoid typing?

I sometimes use temporary variables to shorten the identifiers:

private function doSomething() {
    $db = $this->currentDatabase;
    $db->callMethod1();
    $db->callMethod2();
    $db->callMethod3();
    $db->...
}

Although this is a PHP example, I'm asking in general:

Is this bad practice? Are there any drawbacks?

Upvotes: 12

Views: 1559

Answers (9)

NeoZoom.lua
NeoZoom.lua

Reputation: 2901

A getter will solve your problem:

private function doSomething() {
    getDB()->callMethod1();
    getDB()->callMethod2();
    getDB()->callMethod3();
}

by clean code N.

Upvotes: 0

Pablo Rodriguez
Pablo Rodriguez

Reputation: 582

I don't think there is a performance penalty if you use the original variable instead of skipping the first dereference ($this->currentDatabase).

However, as readability is much improved using the abbreviation, go for it!

Of course it also will depend on your team's coding conventions.

Upvotes: 1

mauris
mauris

Reputation: 43619

This example is perfectly fine, since you are using it in functions/methods.

The variable will be unset right after the method/function ends - so there's not much of a memory leak or what.

Also by doing this, you "sort of" implemented DRY - don't repeat yourself.

Why write so many $this->currentDatabase when you can write $db. And what if you have to change $this->currentDatabase to some other values?

Upvotes: 15

Nakedible
Nakedible

Reputation: 4168

It depends what is the contract on $this->currentDatabase. Can it change at any time, after any method call? If it changes, are you supposed to keep on using the object you did when you made your first db call, or are you supposed to always us the current value? This dictates if you must always use $this->currentDatabase, or if you must always store it in a variable before using.

So, strictly speaking, this is not a style question at all.

But, assuming the member is never changed during function calls such as this, it makes no difference. I'd say storing it in a variable is slightly better, as it is easier to read and avoids a member access on an object at every operation. The compiler may optimize it away if it's good, but in many languages such optimizations are very difficult - and accessing a local variable is almost invariably faster than accessing a member of an object.

Upvotes: 3

MikeJ-UK
MikeJ-UK

Reputation: 620

I seem to remember that Steve McConnell recommends against using temporary variables in "Code Complete". At the risk of committing heresy, I have to disagree. I prefer the additional readability introduced. I also find myself adding them to aid single-step debugging, then seeing no reason to remove them.

Upvotes: 2

JeffV
JeffV

Reputation: 54487

No, I think, this is ok. Often performance if not as critical as clean readable code.

Also, you are trading memory a small allocation hit on the stack for faster method calls by avoiding extra dereferencing.

Upvotes: 0

Janusz
Janusz

Reputation: 189494

If you do this carefully it is absolutely fine. As long as you only use a few of this variables in a small amount of code and inside of small functions I think this is ok.

If you have a lot of this variables and they are badly named like i,j,l and f in the same function the understandability of your code will suffer. If this is the case I would rather type a little bit more then have not understandable code. This is one reason a good IDE has automatic code completion.

Upvotes: 0

Peter
Peter

Reputation: 48968

In general :

  • Both $db as $this->currentDatabase point to exactly the same object.
  • The little space allocated for $db is freed (or elligeable for garbage collection) when the function ends

so I'd say : no, it's not bad practice.

Upvotes: 2

Gyom
Gyom

Reputation: 3920

Actually, you're not trying to avoid typing (otherwise, you'd use a completion mechanism in your editor), but you're just making your function more readable (by using "abbreviations") which is a good thing.

Drawbacks will show up when you start doing this to avoid typing (and sacrifice readability)

Upvotes: 6

Related Questions