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 8391649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:19:05+00:00 2026-06-09T19:19:05+00:00

When learning to code, I was taught the following style when checking the value

  • 0

When learning to code, I was taught the following style when checking the value of a variable:

int x;
Object *object;
...
if(x == 7) { ... }
if(object == NULL) { ... }

However, now that I am in the field, I have encountered more than one co-worker who swears by the approach of switching the lhs and rhs in the if statements:

if(7 == x) { ... }
if(NULL == object) { ... }

The reasoning being that if you accidentally type = instead of ==, then the code will fail at compile. Being unaccustomed to this style, reading 7 == x is difficult for me, slowing my comprehension of their code.

It seems if I adopt this style, I will likely someday in the future save myself from debugging an x = 7 bug, but in the mean time, every time somebody reads my code I may be wasting their time because I fear the syntax is unorthodox.

Is the 7 == x style generally accepted and readable in the industry, or is this just a personal preference of my coworkers?

  • 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-09T19:19:06+00:00Added an answer on June 9, 2026 at 7:19 pm

    The reasoning being that if you accidentally type = instead of ==, then the code will fail at compile.

    True. On the other hand, I believe modern C and C++ compilers (I’m assuming you’re using one of those languages? You haven’t said) will warn you if you do this.

    Have you tried it with the compiler you’re using? If it doesn’t do it by default, look to see if there are flags you can use to provoke it – ideally to make it an error rather than just a warning.

    For example, using the Microsoft C compiler, I get:

    cl /Wall Test.c
    test.c(3) : warning C4706: assignment within conditional expression
    

    That’s pretty clear, IMO. (The default warning settings don’t spot it, admittedly.)

    Being unaccustomed to this style, reading 7 == x is difficult for me, slowing my comprehension of their code.

    Indeed. Your approach is the more natural style, and should (IMO) be used unless you’re really dealing with a compiler which doesn’t spot this as a potential problem (and you have no alternative to using that compiler).

    EDIT: Note that this isn’t a problem in all languages – not even all C-like languages.

    For example, although both Java and C# have a similar if construct, the condition expression in both needs to be implicitly convertible to a Boolean value. While the assignment part would compile, the type of the expression in your first example would be int, which isn’t implicitly convertible to the relevant Boolean type in either language, leading to a compile-time error. The rare situation where you’d still have a problem would be:

    if (foo == true)
    

    which, if typo’d to:

    if (foo = true)
    

    would compile and do the wrong thing. The MS C# compiler even warns you about that, although it’s generally better to just use

    if (foo)
    

    or

    if (!foo)
    

    where possible. That just leaves things like:

    if (x == MethodReturningBool())
    

    vs

    if (MethodReturningBool() == x)
    

    which is still pretty rare, and there’s still a warning for it in the MS C# compiler (and probably in some Java compilers).

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

Sidebar

Related Questions

I am learning PHP PEAR and testing the following code. However when I login,
I am learning some COM code and the following code puzzled me. STDMETHODIMP _(ULONG)
I am learning EF Code First from Programming Entity Framework Code First. The following
I am learning android and so I wrote the following code for learning ScrollView
I have this learning code I wrote for the morris seq in f# that
I am now learning a code from the opencv codebook ( OpenCV 2 Computer
I am learning c# code from one of the applications that I run SQL
I am learning some good code practice that's why i was going through some
I'm learning to code in Java. I have this assessment that I need to
I just started learning how to code and I am following along Chris Pine's

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.