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

  • BroadCity

    BroadCity

    10 ŞUBAT 2010
  • ExcelTutorials

    ExcelTutoria

    2 Mayıs 2009
  • Jaclyn W

    Jaclyn W

    5 Mayıs 2006