编码风格:函数和过程编码标准
Code Complete 2 的第 7.6 章让我感到困惑,我附上了一些示例代码(在 php 中),介意告诉我哪种风格是最好的吗?或者建议更好的东西?谢谢
样式 1
public function register($user, $pass) {
if($this->model->isRegistered($user)
{
return false;
}
else if($this->twitter->login($user, $pass))
{
return $this->model->addUser($user, $pass);
}
return false;
}
样式 2
public function register($user, $pass) {
if($this->model->isRegistered($user)
{
return false;
}
$this->twitter->login($user, $pass);
if($this->twitter->isLoggedIn())
{
return $this-model->addUser($user, $pass);
}
return false;
}
样式 3
public function register($user, $pass) {
if($this->model->isRegistered($user)
{
return false;
}
$status = $this->twitter->login($user, $pass);
if($status)
{
return $this->model->addUser($user, $pass);
}
return false;
}
我目前正在使用样式 1。虽然我不太确定它是否正确。
Ch 7.6 of Code Complete 2 is confusing me, I've attached some sample code (in php) mind telling me which style is the best? or suggest something better? thanks
Style 1
public function register($user, $pass) {
if($this->model->isRegistered($user)
{
return false;
}
else if($this->twitter->login($user, $pass))
{
return $this->model->addUser($user, $pass);
}
return false;
}
Style 2
public function register($user, $pass) {
if($this->model->isRegistered($user)
{
return false;
}
$this->twitter->login($user, $pass);
if($this->twitter->isLoggedIn())
{
return $this-model->addUser($user, $pass);
}
return false;
}
Style 3
public function register($user, $pass) {
if($this->model->isRegistered($user)
{
return false;
}
$status = $this->twitter->login($user, $pass);
if($status)
{
return $this->model->addUser($user, $pass);
}
return false;
}
I'm currently making use of Style 1. Though I'm not quite sure if its the right one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不想听起来太粗鲁,但我不喜欢这 3 种提议的风格。如果我要检查阻止函数执行的条件,我将始终坚持这种风格。一般来说:
所以我会重写你的代码如下:
无论如何,如果你需要对你的提议有意见,我会投票支持样式 3。
I don't want to sound too rude but I like noone of the 3 proposed styles. If I'm checking for conditions preventing the execution of a function, I'll always stick with this style. In general:
So I'd rewrite your code as following:
Anyway, if you need an opinion on what you proposed, I'd vote for style 3.
在样式 1 中,“if”和“else if”用于不同的条件,因此没有意义。
在样式 2 中,行:
在某些情况下很难阅读,但这是正确的。
对我来说最好的一种是样式 3。
In Style 1 "if" and "else if" is used on different conditions, so it doesn't make sense.
In Style 2 lines:
are too much hard to read in some situations but it's a proper.
For me the best one is Style 3.