-1

JavaScriptの基本を学ぶレベル1の大学生向けの短いチュートリアルを準備しています。タスクは、電話番号を検証することです。数字には数字以外の数字を含めることはできず、14桁以下にする必要があります。次のコードの抜粋は私が思いついたものであり、可能な限り読みやすくしたいと思います。

if (
    //set of rules for invalid phone number
        phoneNumber.length == 0 //empty
    ||  phoneNumber.length > 14 //too long
    ||  /\D/.test(phoneNumber) //contains non-digits
) {
    setMessageText(invalid);
} else {
    setMessageText(valid);
}

簡単な質問私は自分自身に完全に答えることができず、あなたの意見を聞きたいです:周囲の(最も外側の)ブラケットをどのように配置するか?通常のブラケットと中括弧の違いを理解するのは難しいです。あなたは通常)、最後の条件と同じ行に最後を置きますか?あなたは(それ自体でラインの最初のオープニングを維持しますか?個々のサブ条件も角かっこで囲んでいますか?(最初と最後を水平に揃えます)か、それとも最後)を?と同じ列に配置しifますか?

) {別の行を保持しますか、それとも)最後のサブ条件と同じ行に最後を配置して{から、新しい行に開口部を配置しますか?それとも) {、最後のサブ条件と同じ行にを配置しますか?

コミュニティウィキ。

編集 ブラケットの使用法と配置に関する意見のみを投稿してください。コードをリファクタリングする必要はありません。これは、数週間前にJavaScriptを紹介したばかりの人向けです。コードを短くしたり、パフォーマンスを向上させたりするために、コードの書き方について意見を求めているわけではありません。IF条件の前後に角かっこをどのように配置するかを知りたいだけです。

4

3 に答える 3

5

I would refactor the logic for validating the phone number into a function:

function isValidPhoneNumber(phone) {
  if (phone.length == 0) return false;
  if (phone.length > 14) return false;
  return !/\D/.test(phone);
}

Or you can use the regular expression to check the length also:

function isValidPhoneNumber(phone) {
  return /^\d{1,14}$/.test(phone);
}

With a function for validating the phone number, the code gets simpler:

if (isValidPhoneNumber(phoneNumber)) {
  setMessageText(valid);
} else {
  setMessageText(invalid);
}

Or even:

setMessageText(isValidPhoneNumber(phoneNumber) ? valid : invalid);
于 2010-03-13T13:42:07.853 に答える
1
message = invalid
if(phoneNumber.length > 0 && phoneNumber < 14 && /\D/.test(phonenumber)){
  message = valid
}
setMessageText(message)

基本的に、有効であることが証明されるまで無効である方が安全ですが、それは完全に主観的なものです。

また:

if(...){
}

常により良いです:

if(...)
{

}

javascriptの場合-これはセミコロンの挿入が原因です。これはここでは問題にならないかもしれませんが、他の場合には問題になる可能性があるため、言語で作業するときは同じ習慣を維持することをお勧めします。

lastly, I don't overload the code with more of this parentheses than I have to, especially when things are all "ANDed" - when there's mixed operator precedence, it's important to include the additional parentheses, because not everyone will want to think about that when reading the code.

This is all a bit subjective though. sorry.

于 2010-03-13T13:39:06.923 に答える
1

tuh-MAY-toe

toe-MAH-tuh

barring bizarre habits, "readable" is what you're used to seeing

i would format your example like this:

//set of rules for invalid phone number:
//    - not empty
//    - not too long (14 characters)
//    - can contain only digits
if (phoneNumber.length == 0 ||
    phoneNumber.length > 14 ||
    /\D/.test(phoneNumber))
{ 
    setMessageText(invalid); 
}
else 
{ 
    setMessageText(valid); 
} 

because I prefer to see the logic explained all together instead of strewn about the code statements. And I like braces on a line by themselves to make the blocks stand out better.

But - as others have pointed out - for "ultimate" readability of this example, it should be refactored into at least a isValidPhoneNumber function

于 2010-03-13T14:32:35.490 に答える