Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about style

Reply
Thread Tools

Question about style

 
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-15-2006
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?

 
Reply With Quote
 
 
 
 
Mike S
Guest
Posts: n/a
 
      06-15-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-15-2006
Mike S wrote:

> (E-Mail Removed) 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.

 
Reply With Quote
 
Mike S
Guest
Posts: n/a
 
      06-16-2006

(E-Mail Removed) wrote:
> Mike S wrote:
>
> > (E-Mail Removed) 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

 
Reply With Quote
 
Arild Hystad
Guest
Posts: n/a
 
      06-16-2006
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


 
Reply With Quote
 
lovecreatesbeauty
Guest
Posts: n/a
 
      06-16-2006

(E-Mail Removed) 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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-16-2006
"lovecreatesbeauty" <(E-Mail Removed)> writes:
> (E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      06-16-2006

(E-Mail Removed) 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

 
Reply With Quote
 
lovecreatesbeauty
Guest
Posts: n/a
 
      06-16-2006

Keith Thompson wrote:
> "lovecreatesbeauty" <(E-Mail Removed)> 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

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      06-16-2006

<(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid header style inconsistent with sortable column style cedoucette@alum.rpi.edu ASP .Net 0 10-14-2005 12:13 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Javascript Style Switcher that remebers current site style in use Hardeep Rakhra HTML 8 01-15-2004 08:00 PM
Style sheets, include one style within another (not inheritance) foldface@yahoo.co.uk HTML 1 11-24-2003 01:37 PM



Advertisments