不确定使用 Pascal 切换 [英] Unsure tabbing with Pascal
问题描述
我正在尝试标记一些代码,请有人检查以确保我已正确完成此操作(为简单起见更改了一些代码):
Hi I'm trying to tab some code, please could someone check to make sure I have done this correctly (Changed some code for simplicity):
begin
if Password <> Database['Database']
then showmessage ('Message')
else
if NewPassword <> Retype
then showmessage ('Message')
else
begin
if Message (yes, No, etc) =yes
then
begin
List
List
List.post;
showmessage ('Message')
end
else close;
end;
end;
推荐答案
这是一个编码风格的问题,可能不会长久存在.:-)(编码风格在很大程度上取决于个人意见,并且有很多不同的风格.)无论如何,我会试一试.:-)
This is a coding style question, and may not survive here very long. :-) (Coding style is very much a matter of personal opinion, and there are an awful lot of different ones out there.) I'll give it a shot, anyway. :-)
我的做法略有不同.IMO,这清楚地向新程序员显示了 if..else
和 begin..end
的正确配对:
I'd do it slightly differently. IMO, this clearly shows the proper pairings of if..else
and begin..end
to a new programmer:
begin
if Password <> Database['Database'] then
showmessage ('Message')
else
if NewPassword <> Retype then
showmessage ('Message')
else
begin
if Message (yes, No, etc) = yes then
begin
List;
List;
List.post;
showmessage ('Message');
end
else
close;
end;
end;
在我自己的代码中,我仍然会做一些不同的事情(但只有很小的区别).我会将 else
if password
移动到同一行(它减少了一级缩进,对我来说使代码流更清晰.我们有三种可能选项,并且清楚地显示了三个选项(if this
、else if this
、else this
):
In my own code, I'd do it a little differently still (but only a minor difference). I'd move the else
if password
to the same line (it reduces one level of indent, and to me makes the flow of the code more clear. We have three possible options, and there are three options clearly shown (if this
, else if this
, else this
):
begin
if Password <> Database['Database'] then // option 1
showmessage ('Message')
else if NewPassword <> Retype then // option 2
showmessage ('Message')
else // option 3
begin
if Message (yes, No, etc) = yes then
begin
List;
List;
List.post;
showmessage ('Message');
end
else
close;
end;
end;
只有几个其他代码区域的格式有时会产生影响.我会尝试快速接触我能想到的尽可能多的人.
There are only a couple of other code areas where formatting sometimes makes a difference. I'll try to quickly touch as many of them as I can think of off-hand.
案例陈述:
case i of
0: DoThingForZero; // Only one line to execute for 0
1: begin // Two things to do for 1
DoSetupForOne;
DoThingForOne;
end;
2: DoThingForTwo;
else // Handle anything other than 0, 1, 2
DoThingsForOtherValues;
end;
While 语句:
while not Query1.Eof do
begin
// Process each field in current record of table
Query1.Next; // Move to next row (easy to forget, infinite loop happens. :-)
end;
重复陈述:
i := 1;
repeat
i := i + SomeFunctionResultReturningVariousValues();
until (i > 50)
For 循环:
for i := 0 to List.Count - 1 do
begin
ProcessItem(List[i]);
end;
for i := List.Count - 1 downto 0 do
List[i].Delete;
For..in 循环:
For..in loops:
for ch in SomeString do // For each character in a string,
WriteLn(ch, ' = ', Ord(ch)); // write the ordinal (numeric) value
ReadLn;
尝试..最后:
SL := TStringList.Create; // Create object/open file/whatever (resource)
try
// Code using resource
finally
SL.Free; // Free the resource
end;
尝试..除外:
try
// Do something that might raise an exception
except
on E: ESomeVerySpecificException do
begin
// Handle very specific exception
end;
on E: ESomeLessSpecificException do
begin
// Handle less specific exception
end;
else
raise;
end;
Try..finally with try..except:
Try..finally with try..except:
SL := TStringList.Create; // Allocate resource
try
try
// Do something that might raise exception
except
// Handle exception as above
end;
finally
SL.Free; // Free resource
end;
这篇关于不确定使用 Pascal 切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!