SORU
31 EKİM 2008, Cuma


En iyi yolu birden fazla koşul ile ifade biçimine

Eğer iki veya daha fazla koşullara göre yürütmek için bazı kod istiyorsan hangi bir ifade biçimi için en iyi yolu nedir ?

ilk örnek:-

if(ConditionOne && ConditionTwo && ConditionThree)
{
   Code to execute
}

İkinci örnek:-

if(ConditionOne)
{
   if(ConditionTwo )
   {
     if(ConditionThree)
     {
       Code to execute
     }
   }
}

en kolay ve her durum uzun fonksiyon adı falan olabilir akılda tutarak okuma, anlama.

CEVAP
31 EKİM 2008, Cuma


Seçeneği tercih ederim

bool a, b, c;

if( a && b && c )
{
   //This is neat & readable
}

Özellikle, uzun değişken/yöntem koşulları varsa sadece onları kırmak hattını arayabilirsiniz

if( VeryLongConditionMethod(a) &&
    VeryLongConditionMethod(b) &&
    VeryLongConditionMethod(c))
{
   //This is still readable
}

Eğer daha da karmaşık olurlarsa, o zaman durumu yapmayı düşünüyorum yöntemleri ayrı ayrı dışında eğer ifade

bool aa = FirstVeryLongConditionMethod(a) && SecondVeryLongConditionMethod(a);
bool bb = FirstVeryLongConditionMethod(b) && SecondVeryLongConditionMethod(b);
bool cc = FirstVeryLongConditionMethod(c) && SecondVeryLongConditionMethod(c);

if( aa && bb && cc)
{
   //This is again neat & readable
   //although you probably need to sanity check your method names ;)
}

Seçeneği için tek neden IMHO 'B' else fonksiyonlar her bir durum için çalıştırmak için ayrı bir yere sahip olacaktır.

örneğin

if( a )
{
    if( b )
    {
    }
    else
    {
        //Do Something Else B
    }
}
else
{
   //Do Something Else A
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Capcom Unity

    Capcom Unity

    5 NİSAN 2010
  • Caroline Martin

    Caroline Mar

    19 EYLÜL 2008
  • KliptOut KwazeeKilla

    KliptOut Kwa

    24 ŞUBAT 2010