これはコーディングスタイルの質問であり、ここでは長く生き残れないかもしれません. :-) (コーディング スタイルは個人的な意見の問題であり、非常に多くの異なるスタイルがあります。) とにかく試してみます。:-)
少し違うやり方をします。IMO、これは、新しいプログラマーif..else
との適切な組み合わせを明確に示しています。begin..end
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
ます (これによりインデントが 1 レベル減り、コードの流れがより明確になります。3 つの可能なオプションがあり、明確に示されている 3 つのオプションがあります ( 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;
書式設定によって違いが生じることがある他のコード領域は、2、3 だけです。オフハンドで思いつく限り、すぐに触れてみます。
ケース ステートメント:
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 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 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..最後に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;