Ged*_*ias 3 delphi coding-style
function MyFunc: Boolean;
begin
if eval then
Result := True
else
Result := False;
/* Note it's a fancy example, I know that in this case I can do: Result := Eval */
end;
Run Code Online (Sandbox Code Playgroud)
要么
function MyFunc: Boolean;
begin
Result := False;
if eval then
Result := True;
/* good by else statement */
end;
Run Code Online (Sandbox Code Playgroud)
这实际上取决于方法的复杂性,你应该总是以可读性为目标,这些例子对我来说都很好
function MyFunc: Boolean;
begin
Result := False;
if (Something or SomethingElse) and Whatever then
Result := True;
end;
function MyFunc: Boolean;
begin
Result := (Something or SomethingElse) and Whatever;
end;
function MyFunc: Boolean;
begin
Exit((Something or SomethingElse) and Whatever);
end;
function MyFunc: Boolean;
begin
if (Something or SomethingElse) and Whatever then
Result := True
else
Result := False;
end;
Run Code Online (Sandbox Code Playgroud)
我个人一样,喜欢避免使用其他语句并尽量少写代码,所以我会选择示例2,但是示例1也很好,选项3和4的可读性不是很高.
我想如果你把这4个例子给初学者,第一个是最容易理解的.