PC Review


Reply
Thread Tools Rate Thread

Can this code be written in a shorter way

 
 
Tony Johansson
Guest
Posts: n/a
 
      21st Apr 2011
Here is a working code but I just wonder if it's possible to write the code
so the amount of code is shorter ?

int IComparer<PointF>.Compare(PointF point1, PointF point2)
{
if (currentMode == Mode.X)
{
if (point1.X > point2.X)
return 1;
else if (point1.X < point2.X)
return -1;
else
return 0;
}
else
{
if (point1.Y > point2.Y)
return 1;
else if (point1.Y < point2.Y)
return -1;
else
return 0;
}
}

//Tony


 
Reply With Quote
 
 
 
 
Des
Guest
Posts: n/a
 
      21st Apr 2011
On 21/04/2011 09:12, Tony Johansson wrote:
> Here is a working code but I just wonder if it's possible to write the code
> so the amount of code is shorter ?
>
> int IComparer<PointF>.Compare(PointF point1, PointF point2)
> {
> if (currentMode == Mode.X)
> {
> if (point1.X> point2.X)
> return 1;
> else if (point1.X< point2.X)
> return -1;
> else
> return 0;
> }
> else
> {
> if (point1.Y> point2.Y)
> return 1;
> else if (point1.Y< point2.Y)
> return -1;
> else
> return 0;
> }
> }
>


I think it could be written in 1 line using a conditional operator, if
you wish.
Below is a pointer.

Math.Sign(point1.X - point2.X)

 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      21st Apr 2011
Tony Johansson wrote:
> Here is a working code but I just wonder if it's possible to write the code
> so the amount of code is shorter ?


Shorter for sure. But what are you trying to accomplish?

> int IComparer<PointF>.Compare(PointF point1, PointF point2)

{
return currentMode == Mode.X
? point1.X.CompareTo(point2.X)
: point1.Y.CompareTo(point2.Y);
}


Marcel
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      21st Apr 2011
Des wrote:
> Math.Sign(point1.X - point2.X)


Note that this will throw an exception for singular values like NaN. A
comparer normally should not throw.


Marcel
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      21st Apr 2011
Hallo,

Peter Duniho wrote:
> It will also return the wrong value for certain inputs. For example:
>
> Point p1 = new Point(int.MinValue, 0),
> p2 = new Point(1, 0);
>
> p1 should compare as less than p2, but the "clever" approach will claim
> that p2 is greater than.


the OP used floting point values (PointF). So this will not happen.


> When you want a comparison, _write a comparison_.


Anyway, indeed.


Marcel
 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      21st Apr 2011
"Tony Johansson" <(E-Mail Removed)> wrote in message
news:iooot9$c8p$(E-Mail Removed)...

> Here is a working code but I just wonder if it's possible to write the
> code so the amount of code is shorter ?
>
> int IComparer<PointF>.Compare(PointF point1, PointF point2)
> {
> if (currentMode == Mode.X)
> {
> if (point1.X > point2.X)
> return 1;
> else if (point1.X < point2.X)
> return -1;
> else
> return 0;
> }
> else
> {
> if (point1.Y > point2.Y)
> return 1;
> else if (point1.Y < point2.Y)
> return -1;
> else
> return 0;
> }
> }


Yes, but any kind of shortening will result in pretty much the same
generated IL and will make the code less clear. The fact is that you have
hit on something so fundamental that you really can't simplify it further. I
know the general gut reaction is "There's got to be another way," but if you
think about it, eventually you can only get code simplified so far until you
hit "the basics," and that's what you've got right there: if statements and
returns. Basic, basic, basic.


 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      21st Apr 2011

"Jeff Johnson" <(E-Mail Removed)> skrev i meddelandet
news:iopg0j$uob$(E-Mail Removed)...
> "Tony Johansson" <(E-Mail Removed)> wrote in message
> news:iooot9$c8p$(E-Mail Removed)...
>
>> Here is a working code but I just wonder if it's possible to write the
>> code so the amount of code is shorter ?
>>
>> int IComparer<PointF>.Compare(PointF point1, PointF point2)
>> {
>> if (currentMode == Mode.X)
>> {
>> if (point1.X > point2.X)
>> return 1;
>> else if (point1.X < point2.X)
>> return -1;
>> else
>> return 0;
>> }
>> else
>> {
>> if (point1.Y > point2.Y)
>> return 1;
>> else if (point1.Y < point2.Y)
>> return -1;
>> else
>> return 0;
>> }
>> }

>
> Yes, but any kind of shortening will result in pretty much the same
> generated IL and will make the code less clear. The fact is that you have
> hit on something so fundamental that you really can't simplify it further.
> I know the general gut reaction is "There's got to be another way," but if
> you think about it, eventually you can only get code simplified so far
> until you hit "the basics," and that's what you've got right there: if
> statements and returns. Basic, basic, basic.
>


This code is much shorter. I mean the generated IL code might not be shorter
but the main point is
that the code that I write is short.
This code is also easy to understand.
int IComparer<PointF>.Compare(PointF point1, PointF point2)
{
return currentMode == Mode.X
? point1.X.CompareTo(point2.X)
: point1.Y.CompareTo(point2.Y);
}

//Tony


 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      21st Apr 2011
"Tony Johansson" <(E-Mail Removed)> wrote in message
news:iopqht$t69$(E-Mail Removed)...

> This code is much shorter. I mean the generated IL code might not be
> shorter but the main point is
> that the code that I write is short.
> This code is also easy to understand.
> int IComparer<PointF>.Compare(PointF point1, PointF point2)
> {
> return currentMode == Mode.X
> ? point1.X.CompareTo(point2.X)
> : point1.Y.CompareTo(point2.Y);
> }


I'll take simple code that doesn't use method calls over simple code that
does any day.


 
Reply With Quote
 
Des
Guest
Posts: n/a
 
      21st Apr 2011
On 21/04/2011 15:51, Peter Duniho wrote:
> On 4/21/11 7:36 AM, Marcel Müller wrote:
>> [...]
>>> p1 should compare as less than p2, but the "clever" approach will
>>> claim that p2 is greater than.

>>
>> the OP used floting point values (PointF). So this will not happen.

>
> Ah, yes. Should have looked more closely. Thanks.
>
> Even so, other bad things can happen with overflowed values (such as the
> exception you mentioned…not sure why I didn't put 2 and 2 together
> there; tunnel-vision, I guess).
>
> Subtractions aren't comparisons; they should only be used as such if
> there is both a VERY good reason for doing so (e.g. the values have to
> be subtracted anyway and performance is for some reason _critical_),
> _and_ one can guarantee the values stay within a small range that
> guarantees the result of the subtraction remains valid for comparison
> purposes.
>
> It is exceedingly rare for both of those conditions to hold. Heck, the
> first condition alone is exceedingly rare.
>
> Even ignoring the correctness issue, the other big problem with being
> "clever" like that is that it makes the code much harder to understand.
> If you write a comparison, it's obvious you're comparing things. If you
> try to implement a comparison using something other than a comparison,
> now every time someone looks at that code, they have to figure out
> whether the "other than a comparison" code is actually performing a
> comparison.
>
> Just say "no".
>
> Pete


Thanks guys for the input. I'm new to c# and didn't even know there was
a CompareTo.

Not so sure of your 'cleverness' jibe though.

 
Reply With Quote
 
Des
Guest
Posts: n/a
 
      21st Apr 2011
On 21/04/2011 10:17, Marcel Müller wrote:
> Des wrote:
>> Math.Sign(point1.X - point2.X)

>
> Note that this will throw an exception for singular values like NaN. A
> comparer normally should not throw.
>
>
> Marcel


Ok, I am just learning c#, just finding my way around vs.
So what would I expect the comparer to return, NaN makes sense to me.
Surely if it returns NaN it is different to the OPs code (although what
I expect he wanted).


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
is there shorter way to code this rodchar Microsoft C# .NET 9 17th Mar 2009 10:36 AM
Shorter Code Sandy Microsoft Excel Programming 2 7th Jul 2007 08:47 PM
Shorter code Adrie Rahanra Microsoft Excel Programming 3 23rd Mar 2004 12:05 PM
Shorter code Stu Microsoft Excel Programming 13 29th Sep 2003 11:27 PM
Re: Shorter code Stu Microsoft Excel Programming 0 28th Sep 2003 07:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:22 PM.