473,383 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Question about style

What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?

Jun 15 '06 #1
10 1398

sp****@gmail.com wrote:
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?


Both ;-)

IMHO the "correctness" of a particular coding style can't be judged
objectively; there will always be personal bias as to which way is The
Right Way. Ultimately, it's up to you to decide whether you can read it
and whether it's acceptable or not, unless you're modifying someone
else's code, in which case it's better to adopt their coding style for
the sake of consistency and clarity.

That being said, I'm not sure I like the specific example you gave.
It's mildly clever, but I would prefer to see something like

if (x < MAX_VALUE) ++x;

because it makes the intent much clearer.

--
Mike S

Jun 15 '06 #2
Mike S wrote:
sp****@gmail.com wrote:
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?
Both ;-)

IMHO the "correctness" of a particular coding style can't be judged
objectively; there will always be personal bias as to which way is The
Right Way.


Perhaps someone will find an objective way to evaluate it. Even if
not I'd still like to have a collection of subjective opinions.
That being said, I'm not sure I like the specific example you gave.
It's mildly clever, but I would prefer to see something like

if (x < MAX_VALUE) ++x;


That is indeed the most obvious way of doing it. The problem with
it is that you can only use it in places where the language allows a
statement as opposed to an expression. Of course you can also use
x += x<MAX_VALUE ? 1 : 0 but I find this somewhat silly since
x<MAX_VALUE taken on its own already has the correct values.

Personally I'd use either one depending among other factors on the
whim of the moment. And I have to say that I don't use it for
reasons of cleverness. I have simply developed a taste for
expressions of this sort where the numerical value of a logical
expression is used for numerical computations since my early BASIC
days because I read on some magazine back then that the interpreter
is likely to execute these faster than using if.

Jun 15 '06 #3
On Thu, 15 Jun 2006 15:43:51 -0700, spibou wrote:
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?


Personally, I would have used parantheses to make my intention clear, like
this: "x += (x < MAX_VALUE)". It could also be written like this:
"(x < MAX_VALUE ? ++x : x)".

--

Arild Hystad
Jun 15 '06 #4

sp****@gmail.com wrote:
Mike S wrote:
sp****@gmail.com wrote:
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?

[snip]
It's mildly clever, but I would prefer to see something like

if (x < MAX_VALUE) ++x;

[snip]
Personally I'd use either one depending among other factors on the
whim of the moment. And I have to say that I don't use it for
reasons of cleverness. I have simply developed a taste for
expressions of this sort where the numerical value of a logical
expression is used for numerical computations since my early BASIC
days because I read on some magazine back then that the interpreter
is likely to execute these faster than using if.


Well, to be honest, the expression form x += x < MAX_VALUE is already
starting to grow on me; I would use parentheses though:

x += (x < MAX_VALUE);

(In fact, I when I first retyped the expression, I unconsciously
inserted the parentheses, I guess out of habit...). Anyway, I didn't
mean to make it sound as if using an "if" statement would be better or
worse - an "if" would simply serve to make your intention (painfully)
obvious. There is at least some merit in this as a general practice
when writing code, for the benefit of people reading your code, but at
the same time, if e.g. a future maintenance progammer can't untangle
the meaning of x += (x < MAX_VALUE) after a few seconds, then they
aren't qualified enough to maintain *anyone's* code ;-)

--
Mike S

Jun 16 '06 #5

sp****@gmail.com wrote:
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?


It's ok. The expression is same as:
y = x < MAX_VALUE;
x += y;

The operator '<' takes precedence over '=' in evaluation. I think it's
good to make the equality clear and nature. But something like "x++ +=
x++ < MAX_VALUE" is a rather bad idea.

Jun 16 '06 #6
"lovecreatesbeauty" <lo***************@gmail.com> writes:
sp****@gmail.com wrote:
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?


It's ok. The expression is same as:
y = x < MAX_VALUE;
x += y;

The operator '<' takes precedence over '=' in evaluation. I think
it's good to make the equality clear and nature. But something like
"x++ += x++ < MAX_VALUE" is a rather bad idea.


It's not just a bad idea, it's illegal (more precisely, it's a
constraint violation). "x++" does not yield an lvalue, so it can't
appear on the left side of an assignment. (Even if that weren't the
case, of course, modifying x twice between sequence points would
invoke undefined behavior.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 16 '06 #7

sp****@gmail.com wrote:
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?


Use a fscking if stmt. You'll thank me when your first project hits
100K lines, hundreds of files and you can STILL READ THE DAMN CODE.

if (x < MAX_VALUE) { ++x; }

That may be longer [I would indent and put the ++x; on a newline] but
it's immensely easier to read [at high speed] and will prevent you from
ripping your brain out of your skull when you have to maintain 100K
lines of gibberish code.

Tom

Jun 16 '06 #8

Keith Thompson wrote:
"lovecreatesbeauty" <lo***************@gmail.com> writes:
But something like
"x++ += x++ < MAX_VALUE" is a rather bad idea. It's not just a bad idea, it's illegal (more precisely, it's a
constraint violation). "x++" does not yield an lvalue, so it can't
appear on the left side of an assignment.


Thank you for the reminder. Yes, it will fail at compilation. And more
accurate, `x++' doesn't yield a modified lvalue :)
(Even if that weren't the
case, of course, modifying x twice between sequence points would
invoke undefined behavior.)


Thanks for the reminder again. I was going to express myself on this
point in my previous post but I failed. You know me :)

lovecreatesbeauty

Jun 16 '06 #9

<sp****@gmail.com> wrote
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?


Compileable gibberish.

At very worst make it x += (x < MAX_VALUE) ? 1 : 0;

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm

Jun 17 '06 #10
Malcolm wrote:
<sp****@gmail.com> wrote
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?


Compileable gibberish.

At very worst make it x += (x < MAX_VALUE) ? 1 : 0;


It's already obscured, why obscure it further with the useless ?:
operator? All that can do is permit the compiler to generate
useless code.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jun 17 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: HolaGoogle | last post by:
hi there, i've 2 questions for you guys.... 1: is there any way to "force" a session_onend(), session timeout or at least call my logout method when a user leaves the application window without...
19
by: CMAR | last post by:
I have the following markup. The problem is that the browser, e.g., IE6, inserts several lines of blank space between the <div> and the following table. Is there a way to minimize that vertical...
7
by: Sharon | last post by:
Hiya I have a small question, I saw this piece of code somewhere (it's for creating a customized context menu) and I was wondering: Why is it that the STYLE and SCRIPT-tags are broken up into...
4
by: Nigel Molesworth | last post by:
I've Googled, but can't find what I need, perhaps I asking the wrong question! I want a "FAQ" page on a web site, I hate those pages that scroll you to the answer so and I figured that a good...
1
by: amerar | last post by:
Hi All, I posted a question about style sheets, and why certain email clients were ignoring them. Someone suggested placing them inline. I did this and get better results, but not what I...
8
by: George | last post by:
I need help with the code listed below. See the line below the comment-// *** This displays the error *** I want to be able to have the event handler call the function based on the reference...
2
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row...
5
by: | last post by:
I want to define a style that automatically associates a specific image with certain IMG elements. Is there a way to do this with ASP.NET? If I use an ASP.NET image control and specify a class...
2
by: Ken Fine | last post by:
I want to add the security question and answer security feature to the ChangePassword control. I am aware that this functionality is built into the PasswordRecovery tool. I have implemented the...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.