Results 1 to 5 of 5

Thread: [RESOLVED] [2.0] the "correct" if statement syntax...if any

  1. #1

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Resolved [RESOLVED] [2.0] the "correct" if statement syntax...if any

    Hello everyone,

    I am a little bit confused as the the "correct syntaxt" for an if statement, as i frequently see sites use either...

    Sample 1:
    Code:
    if (someString != "hi") {
    
    //do stuff
    }

    Sample2:
    Code:
    if (!(someString == "hi")) {
    
    //do stuff
    }
    i see the first one more often...is there a write or wrong, or just preference...

    Thanks, Justin

  2. #2
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: [2.0] the "correct" if statement syntax...if any

    I would use the first sample per preference because it's easier to read without second thought. Do the same thing in a different way. You'd have to decide what you would rather read of the two versions. I can see some users missing the ! at first in the second example, but definitely not in the first example.
    Cheers!

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] the "correct" if statement syntax...if any

    I strongly second gjon's sentiments. I can't see any real justification for the second way as it simply complicates the code without adding value. I'd be fairly sure that the first way is also more efficient.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] the "correct" if statement syntax...if any

    Thirded. You should try to use as few operators as possible in order to maximise readability.

  5. #5

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: [2.0] the "correct" if statement syntax...if any

    Hey,

    Thanks, Thread Resolved.

    just wasnt sure whether there was a purpose if any to the second one...i too use the first 1, again for readability and it seems more logical to me...

    Thanks, Justin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width