【发布时间】:2017-12-29 12:39:42
【问题描述】:
对齐还是不对齐...
以下方法的优缺点。
我喜欢使用第一种情况。因为我认为它是最优的,但其他人却不这么认为。
- 向文件添加新常量时 - diff 是 1 个字符串,而不是更多
- 更清晰,更漂亮
- 链式调用 (-> -> -> ->) - 更清晰
你怎么看?可能有关于它的 PSR 吗?
案例风格1
class A {
const AB = 1;
const PAGE = 2;
const PP_SOMETHING_VERY_LONG = 3;
public function testLoooooooooong()
{
$a = 1;
$somethingWeryLong = 2;
$c = 3;
$res = $a + $c;
$b = 1;
$n = 100;
return 0;
}
public function test2($objectttttttttttttttLooooooong)
{
$objectttttttttttttttLooooooong->callSooooooooomethingLooooooong()
->call(
$this->testLoooooooooong(),
$this->testLoooooooooong2(),
$this->testLoooooooooong3()
)
->call(
$this->testLoooooooooong(),
$this->testLoooooooooong2(),
$this->testLoooooooooong3()
);
}
}
案例风格2
class A {
const AB = 1;
const PAGE = 2;
const PP_SOMETHING_VERY_LONG = 3;
public function testLoooooooooong() {
$a = 1;
$somethingWeryLong = 2;
$c = 3;
$res = $a + $c;
$b = 1;
$n = 100;
return 0;
}
public function test2($objectttttttttttttttLooooooong) {
$objectttttttttttttttLooooooong->callSooooooooomethingLooooooong()
->call(
$this->testLoooooooooong(),
$this->testLoooooooooong2(),
$this->testLoooooooooong3()
)
->call(
$this->testLoooooooooong(),
$this->testLoooooooooong2(),
$this->testLoooooooooong3()
);
}
}
【问题讨论】:
-
我更喜欢案例 2。我经常对齐那些“=”。但不总是。但经常。关于缩进,当代码应该缩进时,我总是只使用 4 个空格来缩进代码。例如,这适用于 IF 内的内容。我不使用 TAB 缩进。不推荐。
-
没有实际测试,示例 1 似乎与 PSR2 匹配
-
我已经投票结束这个问题,因为这主要是一个风格问题,完全基于意见。您唯一需要做的就是使其在您的文件中保持一致