0

これは以前に回答されたことがあると思いますが、長いコード行をフォーマットするときのベストプラクティスに興味があります。

実行時間が長すぎる場合、それぞれをどのようにフォーマットしますか

これは、以下の適切なコードではなく、各タイプのrunonステートメントをフォーマットする方法に関するものであることに注意してください。

javascript/jqueryに長い条件演算子があるとします

var tablesToHide = $('table').length > 1 ? $('table') : $($('.nav > li[class="active"] > a').attr('href'));
tablesToHide.hide();

nullチェックを使用したJavaの長い条件があるとします

if(person.firstName != null && person.firstName.length() > 32 && person.firstName.length() < 5 && person.lastName != null &&  person.lastName.length() > 32 && person.lastName.length() < 5){//ridiculous operation}

長い操作があるとしましょう

long worldVsUnitedStates = (worldDuckCount + worldCatCount + worldTugBoatCount)/(unitedStatesTugBoatCount + unitedStatesDuckCount + unitedStatesCatCount)

Guava操作のような長いメソッド呼び出し

final Iterable<AccountingDocument> documentList = Iterables.filter(resultRecord.getAccountingDocuments(), AccountingDocument.class);

ロギングステートメントのような大きなメソッドパラメータ

logger.entering("UserAccountingAdministrationController", "createNewUserAccountingDocument", new Object[] { userAccountingForm, result, model });

FindBugsとthrows宣言を使用するための大きなメソッドパラメータ

public void saveAccountingFormWithValues( @Nullable FooA firstValue, @Nonnull FooB secondValue, @Nullable FooC thirdValue, @Nullable FooD fourthValue) throws DataAccessException 
4

1 に答える 1

1

Javaコーディング規約には、「多くの端末やツールで適切に処理されないため」、コード行を80文字より長くしないようにという推奨事項がありました...明らかに、これはもはや当てはまりませんが、そうすべきではありません。読みやすさを追求するべきではないという意味ではありません。

LCDや高解像度の画面でも、すべての人が同じフォントサイズを使用するわけではないので(私の開発者の1人は目のために14〜16 ptのフォントを使用します)、読みやすさを目指し、ステートメントを理解しやすくする必要があります。

if特にステートメントや複雑な計算などでは、可能な場合は論理要素をグループ化します。

多くは個々のステートメントに帰着します(また、すべてのタブが等しいわけではないことを覚えておいてください)が...

私は個人的に次のようなものを使用します...

if(person.firstName != null && 
   person.firstName.length() > 32 && 
   person.firstName.length() < 5 && 
   person.lastName != null &&  
   person.lastName.length() > 32 && 
   person.lastName.length() < 5) {...}

long worldVsUnitedStates = (worldDuckCount + 
                            worldCatCount +
                            worldTugBoatCount) /
                           (unitedStatesTugBoatCount + 
                            unitedStatesDuckCount + 
                            unitedStatesCatCount)

final Iterable<AccountingDocument> documentList = Iterables.filter(
    resultRecord.getAccountingDocuments(), 
    AccountingDocument.class);

私見では

于 2012-10-09T01:37:10.323 に答える