As a phper, how do you name temporary variables

Source: Internet
Author: User
Title, when writing code, you will always encounter some temporary variables
So you usually choose what name to take for the TEMP variable,
Like what

$foo, $a, $x

Wait a minute?

Reply content:

Title, when writing code, you will always encounter some temporary variables
So you usually choose what name to take for the TEMP variable,
Like what

$foo, $a, $x

Wait a minute?

The name of the temporary variable, the main thing is to see where the
If it's used in a loop, it's definitely

$i, $j, $k

In the case of foreach, most of the cases will be

$k, $v, $key, $val

If it's a temporary use,

$temp, $t

I really do not know what to use, but also to use, the general I call

$ignore _me

The other is called by type name

$int, $num, $str, $arr

Simple and easy to read meaningful words

Temporary variables are named as normal variables, and the life cycle of a variable is not represented by the variable name.

    一个变量既然存在,就一定是不能删的。他必然有类型,他必然有用途,既然有类型有用途,他必须有自己的名字,哪怕是 $intNumber、$keyOfUsersArray、$tempFileUploaded 也比 $tmp1、$tempFile 好。    最糟糕的两个变量名是 $data1 和 $data2,我认为再次就是 $tmp 、 $temp 了。    如果你们公司只有一个临时工,那么你可以叫他 "临时工" ,这没问题。但是如果新来了一个临时工呢?叫 "新来的临时工" ?再来怎么办?所以最好的办法是一开始就给他们取个名字。比如 "胖王","李花匠","小寸头","王大力"...

Add a way to remove temporary variables described in refactoring-improving the design of existing code-use a query instead of a temporary variable (replace temp with query):

Before refactoring:

...$basePrice = $this->quantity * $this->itemPrice; //临时变量$basePriceif ($basePrice > 1000) {    return $basePrice * 0.95;} else {    return $basePrice * 0.38;}...

After refactoring:

...if ($this->basePrice() > 1000) {    return $this->basePrice() * 0.95;} else {    return $this->basePrice() * 0.38;}...//用于代替临时变量的查询式public function basePrice() {    return $this->quantity * $this->itemPrice;}...

The use motive as described in the book:
The problem with temporary variables is that they are temporary and can only be used within the owning function. Because temporary variables are visible only within the owning function, they drive you to write longer functions, because only then can you access the temporary variables you want to access. If you replace the temporary variable with a query method), then all functions in the same class will be able to obtain this information. This will give you a great deal of help in writing clearer code for this class.

The same as the @lazyboy in the loop, the other is the variable name plus _tmp suffix

Do not understand why there should be a "temporary" concept, the script run out of all the variables are destroyed, when running, we seldom actively unset it, discuss his life cycle and what is the point?

As for the scope, this with the mood Bai.

$temp
$tmp

As far as I'm concerned, if it's a single temporary variable, I'll use it _ , and a few will have something to say, but always _ start with. Oh.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.