Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8250599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:56:49+00:00 2026-06-07T23:56:49+00:00

This question applies to every programming language that has a short-circuit AND operator, not

  • 0

This question applies to every programming language that has a short-circuit AND operator, not just C#.

The question is simple – is using short circuit evaluation to avoid an out of range index exception, for example:

if ((x > 0) && (bar[x] == foo))

or

if (((x > 0) && (x < bar.Length)) && (bar[x] == foo))

bad coding style? I know I could nest the loops like this:

if (x > 0)
{
    if (bar[x] == foo)
    {
    }
}

but I find it to be extremely unreadable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:56:50+00:00Added an answer on June 7, 2026 at 11:56 pm

    I would say that

    if ((x > 0) && (bar[x] == foo))
    

    is not bad coding style. It’s probably even good. I would definitely prefer it to a nested if structure like you describe.

    As an aside, I would reduce the number of parentheses you use. Both these are equally correct, at least in C# and most other C-derived languages:

    if (x > 0 && bar[x] == foo)
    if (x > 0 && x < bar.Length && bar[x] == foo)
    

    Readers who know the language (you have to assume this at some level) will easily be able to understand the above short-circuit expressions. Those readers would probably object to the nested if style, because it takes way more room than is necessary to get the correct behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using PrimeFaces p:messages but I think this question applies equally to h:messages
I know this may be simple question but want to know every ones opinion
i'm pretty sure that this question has allready been asked (but i didn't find
Ok a short question: Is there any SIMPLE software with a GUI, that lets
I believe this question applies to any of the For Html helpers, but my
I believe this question applies equally well to C# as to Java, because both
I'm coming largely from a c++ background, but I think this question applies to
This question is kinda aimed towards PHP but it probably applies to other languages
I am using Ruby on Rails, but I think this question could be applied
I don't know is this question more related to mathematics or programming and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.