You are here

Do you put your l-values on the right? | Cypress Semiconductor

Do you put your l-values on the right?

My recent comment about tilde (~) versus pling (!) caused some comment in this neck of the woods so I thought I'd follow up with another contentious post about C programming. Where should the lvalue go in a conditional expression?

OK, that's boring terminology, so let's write a wee bit of code to make the point. Do you write this...

#define FAIL (0)

int result = whatever();

if( result == FAIL ) { }

Or this...

if( FAIL == result ) { }

The issue is, of course, that the code brings us close to the dreaded single-equals problem. The first version is emminently readable because it follows an English subject-verb-object format. Lovely. But it has a built-in risk. Mis-type that == as just an = and all hell breaks loose. Firstly, the block of code never runs, regardless of the return from whatever(). The compiler (should) optimize it all out (even if optimization is disabled this is probably going to happen - the code cannot run so why generate the instructions?). What happens in the code that might come after that block is anybody's guess! It is all a bit of an "oopsie" and we've all done it. Well you all have. Not me. No. never. Ahem. Moving on...

So, the smarty-pants amongst us reverse the expression and put the non-writable part on the left. That method means a typo is an easy to fix compiler error, not run-time wierdness. That's a great solution. But it looks strange and I hate it!

What do you guys do? Opt for safety or readability? My take, which might get me into trouble, is that while I have actually done the single-equals thing (OK, I admit it, can we move on?) I have never done so in code that I gave to someone else. I've always figured it out and, after the first time, done so pretty quickly. The end result is code that is "right" and readable.

Am I making a mature, informed decision about my C programming life or am I leaning on the "thin ice" sign-post as I strap on my skates?

Comments

Bob Marlowe's picture

The GCC throws a warning when coding a construct like

if(MyVar = 427) DoSomething();

and says that additional parentheses are required to express a wanted assignment.
Saved me a couple of lengthy searches since (I admit) from time to time I leave out one of the equal signs.
Trusting the compiler I prefer the "better reading" form over the "never error" way.

H L's picture

#define FAIL (0)
#define EQS ==

int result = whatever();

if( result EQS FAIL ) { }

Use EQS when comparing
Use = when assigning

yfs's picture

Good point - if the compiler warns reliably then there is no need to sacrifice readability. Now, if I could only read the warnings more reliably!!!

yfs's picture

I like H L's answer too. Very cunning. Both of these suggestions are safe and clear. I wish our blog system did not remove new lines... it would not make a good source code editor!

Soniya Singh's picture

Hello friends, Visit Our website and get an online steam wallet code generator which provides 100% safe and working steam wallet codes which you can redeem to your steam account anywhere in the world.

taylorshaw151_2687531's picture

The codes offered here are very helpful in making the project all by me. The detailed instructions along with the image helped to understand the concept. The site has been providing a lot of such interesting topics regarding PSOC 4 Software Enablement. tabletresellers.com

lincyrose90_2704921's picture

This article shares some code here and that code bring us close to the dreaded single equal’s problem. But I think this article provides very limited details regarding this. Can you update with more details over here.http://vividtablets.com/

Muneer Ahmed's picture

I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information. preschools in Owensboro KY

Muneer Ahmed's picture

You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you. how much money can you make trading options

Muneer Ahmed's picture

Just admiring your work and wondering how you managed this blog so well. It’s so remarkable that I can't afford to not go through this valuable information whenever I surf the internet!  top rated

r143011_2835121's picture

You guys are doing a valuable job by letting us know about the various details regarding the PSOC designs and its applications. I am currently doing a project on the same and these details will be definitely useful for that. elder ones

asadalikhatrint_2946251's picture

I am continually hunting on the web down articles that can help me. There is clearly a considerable measure to think about this. I think you made some great focuses in Features too. Continue working, extraordinary job ! Mortgage Area Manager

asadalikhatrint_2946251's picture

This is very much a work in progress whenever I find out about one who is more beautiful than any of these I will add her and kick out number ten Thanks for sharing the informative post. Bounce House Rental Herndon VA

ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.