機械翻訳について

条件でのNULL処理の使用

条件文がNULL値でどのように動作するかを理解することで、コード行の追加を回避できます。 変数someFlagがnullの可能性があるブール変数である場合、次の条件付きブロックはsomeFlagがtrueの場合にのみ実行されます。

String変数には、null、空の文字列("")または少なくとも1つの文字を含めることができます。 変数middleNameStringの場合、次の条件付きブロックは、middleNamenullではなく、少なくとも1文字を含む場合にのみ実行されます:

// If customer has a middle name...
if (middleName) {
  // Do something here if middleName has at least one character in it
}

変数recentOrdersListの場合、次の条件付きブロックは、recentOrdersnullではなく、少なくとも1つの要素が含まれている場合にのみ実行されます:

// If customer has any recent orders...
if (recentOrders) {
  // Do something here if recentOrders has at least one element
}
変数recentTransactionsMapの場合、次の条件付きブロックは、recentTransactionsnullではなく、マップ・エントリが少なくとも1つ含まれている場合にのみ実行されます:
// If supplier has any recent transactions...
if (recentTransactions) {
  // Do something here if recentTransactions has at least one map entry
}
変数customerIdnullにでき、そのデータ型が前述のもの以外である場合、次の条件付きブロックは、customerIdnull以外の値がある場合にのみ実行されます:
// If non-boolean customerId has a value...
if (customerId) {
  // Do something here if customerId has a non-null value
}
条件付きでMapエントリをテストする必要があり、Mapnullである可能性がある場合は、マップ・キーを名前で参照するときに安全ナビゲーション演算子(?.)を使用してください:
// Use the safe-navigation operator in case options Map is null
if (options?.orderBy) {
  // Do something here if the 'orderBy' key exists and has a non-null value
}