スクリプトのコメント
今から数か月後にコードを表示する同僚がロジックの実行内容を記憶できるように、スクリプトを文書化することが重要です。
現在の行の残りをコメントにする二重スラッシュの組合せ//
を使用するか、/*
のopen-commentとclose-commentの組合せを使用し、その後に*/
を使用できます。 後者のスタイルは複数の線にまたがることができます。
動作中の両方のスタイルの例を次に示します:
// Loop over the names in the list
for (name in listOfNames) {
/*
* Update the location for the current name.
* If the name passed in does not exist, will result in a no-op
*/
updateLocationFor(name, // name of contact
'Default', /* location style */
)
}
複数行コメントを使用する場合、ネストされた/* ... */ コメントを別のコメントの内側に表示することは不正です。 たとえば、次は許可されません:
// Nested, multi-line comment below is not legal
def interest = 0
/*
18-MAY-2001 (smuench) Temporarily commented out calculation!
/*
* Interest Accrual Calculation Here
*/
interest = complexInterestCalculation()
*/
かわりに、次のような既存の複数行ブロックをコメント・アウトできます:
// Nested, multi-line comment below is legal
def interest = 0
//
// 18-MAY-2001 (smuench) Temporarily commented out calculation!
//
// /*
// * Interest Accrual Calculation Here
// */
// interest = complexInterestCalculation()
//
または、初期コードで//
スタイルのコメントを使用していた場合、次のことも有効です:
// Nested, multi-line comment below is not legal
def interest = 0
/*
18-MAY-2001 (smuench) Temporarily commented out calculation!
//
// Interest Accrual Calculation Here
//
interest = complexInterestCalculation()
*/
最も一般的なコメントのスタイル・ガイドでは、スクリプトの最初に複数行のコメントを使用し、後続の行に単一行のコメントを使用することをお薦めします。 これにより、デバッグ目的でコードを簡単にコメントアウトできます。 したがって、一般的なスクリプトは次のようになります:
/*
* Object validation rule for BankAccount
*
* Ensures that account is not overdrawn
*/
def balance = CurrentBalance
// Use an object function to calculate uncleared charges
def unclearedCharges = unclearedChargesAmountForAccount()
// Perform some other complicated processing
performComplicatedProcessing()
// return true if the account is not overdrawn
return balance > unclearedCharges