Busy with the ?: Operator

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1243 Views - Last Post: 11 July 2011 - 01:07 PM

#1 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Busy with the ?: Operator

Posted 06 July 2011 - 05:13 AM

How often do u use the [ ?: ] operator .

I am starting to use it alot and enjoy to use it .

e.g:

 if (ddlLocation.SelectedIndex != 0  && ddlUnitSize.SelectedIndex != 0)
         {
                    Response.Redirect("bookings.aspx?size=" + 
                                      chkNowSize.Checked + "?location=" + 
                                      ddlLocation.SelectedValue + "");
         }
         else
         {
           lblMessage.Text = "Selecting a value from " + 
                             (ddlLocation.SelectedIndex == 0 ? "location dropdown is required" : "unitsize is required");
         }



This works quite good , what do you thing about this operator ?

This post has been edited by marinus: 06 July 2011 - 05:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Busy with the ?: Operator

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Busy with the ?: Operator

Posted 06 July 2011 - 05:29 AM

Cuts down the amount of typing I've got to do really.

Since it applies to a few languages other than C#
<?php
$x = true;
echo "x is equal to " . ($x ? "true" : "false");
?>



Nested ternary operators truly can start to get confusing also so I won't over do it.
<?php
#if a number is less than 0 find the absolute value
#then divide by 2
#if the number is zero return 0
$n = -7;
echo ($n < 0 ? ($n * -1) / 2 : ($n == 0 ? 0 : $n /= 2));
?>


This post has been edited by codeprada: 06 July 2011 - 05:30 AM

Was This Post Helpful? 1
  • +
  • -

#3 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Busy with the ?: Operator

Posted 06 July 2011 - 05:43 AM

They can definitely be a nice tool in any programmer's toolbag, but as codeprada said, they can get confusing if you're trying to do too much with them, for instance nesting. I try to see them when I feel it will reduce the number of lines of code in a method without making it too difficult for someone else to read over the code and make sense of it.

// For instance...
if (x > 0)
{
    x = 1;
}
else
{
    x = -1;
}

// Can easily be shortened to use the ternary operator without taking too much away from
// the readability of the code
int y = x > 0 ? 1 : -1;


Was This Post Helpful? 1
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Busy with the ?: Operator

Posted 06 July 2011 - 06:16 AM

I use it a pretty good bit in code like what Kilorn showed. I also use it in data access code when testing against null values when reading data in..

// some code
using (SqlDataReader dr = cmd.ExecuteReader())
{
    if (dr.Read())
    {
        someDate = (dr["SomeDateColumn"] != DbNull.Value) ? Convert.ToDateTime(dr["SomeDateColumn"]) : DateTime.MinValue;
    }
}


Was This Post Helpful? 1
  • +
  • -

#5 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Busy with the ?: Operator

Posted 06 July 2011 - 06:39 AM

I think there is a fine line between efficiency and lack of readability that we as programmers have to keep in mind. Maybe this isn't such a big deal if you're the only programmer on the project, but if you're working on a team, especially if some members on the team are relatively new to programming, perhaps it would be best to limit using this operator as it seems that most entry level programmers have never even heard of it. It's another great weapon in our arsenal, but it has it's place in my opinion and I try to use it when it makes sense, but try to avoid overusing it.
Was This Post Helpful? 1
  • +
  • -

#6 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Busy with the ?: Operator

Posted 06 July 2011 - 07:48 AM

It can look very confusing if you overuse it, almost like the programming language BrainF***

but it can be a good thing to know ..

And it's something extra to add to you knowledge

Thanks for the input

This post has been edited by marinus: 06 July 2011 - 07:49 AM

Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,463
  • Joined: 08-June 10

Re: Busy with the ?: Operator

Posted 06 July 2011 - 07:50 AM

There's another little-known operator in C#: ??, the Null Coalescing Operator.

Example use:

var x = a ?? b;


That's the equivalent to:

var x;
if(a != null)
  x = a;
else
  x = b;

Was This Post Helpful? 3
  • +
  • -

#8 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Busy with the ?: Operator

Posted 06 July 2011 - 08:11 AM

The ~ operator is another that I'd imagine doesn't get much use, at least in C#. Basically, it is a bitwise complement operator and is used to find the opposite of whatever address in memory that you want to check.

// Directly from the MSDN library:
// cs_operator_bitwise_compl.cs
using System;
class MainClass
{
    static void Main() 
    {
        int[] values = { 0, 0x111, 0xfffff, 0x8888, 0x22000022};
        foreach (int v in values)
        {
            Console.WriteLine("~0x{0:x8} = 0x{1:x8}", v, ~v);
        }
    }
}

//  Output:
// ~0x00000000 = 0xffffffff
// ~0x00000111 = 0xfffffeee
// ~0x000fffff = 0xfff00000
// ~0x00008888 = 0xffff7777
// ~0x22000022 = 0xddffffdd



The output shows that the complement to 0x00000000 is 0xffffffff. If you know a little about hexadecimal, this would be obvious to you.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,463
  • Joined: 08-June 10

Re: Busy with the ?: Operator

Posted 06 July 2011 - 08:14 AM

That would be fairly rare in C#, since most of us don't do too much bitwise calculations.
Was This Post Helpful? 1
  • +
  • -

#10 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Busy with the ?: Operator

Posted 06 July 2011 - 08:18 AM

This is very true. In fact I don't think I've ever seen any reason to perform bitwise calculations in C# given the higher level nature of the language, but should the need ever arise for some strange reason, it is there.
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,463
  • Joined: 08-June 10

Re: Busy with the ?: Operator

Posted 06 July 2011 - 08:23 AM

Yeah, there's a lot of things C# is capable of doing that people just don't usually do. You can wrap a block in unsafe or prepend that to a method declaration and you can use pointers, but hardly anyone does.
Was This Post Helpful? 1
  • +
  • -

#12 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Busy with the ?: Operator

Posted 06 July 2011 - 08:25 AM

With C# there really isn't any need to use pointers that I can think of, but it is nice to know that the functionality is there should a programmer ever need it.
Was This Post Helpful? 0
  • +
  • -

#13 Momerath  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 995
  • View blog
  • Posts: 2,385
  • Joined: 04-October 09

Re: Busy with the ?: Operator

Posted 06 July 2011 - 09:15 PM

View PostKilorn, on 06 July 2011 - 08:25 AM, said:

With C# there really isn't any need to use pointers that I can think of
Speed! Pointers are faster than references when dealing with arrays and such.
Was This Post Helpful? 0
  • +
  • -

#14 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 256
  • View blog
  • Posts: 529
  • Joined: 07-July 10

Re: Busy with the ?: Operator

Posted 07 July 2011 - 10:23 AM

View PostKilorn, on 06 July 2011 - 08:39 AM, said:

Maybe this isn't such a big deal if you're the only programmer on the project, but if you're working on a team, especially if some members on the team are relatively new to programming, perhaps it would be best to limit using this operator as it seems that most entry level programmers have never even heard of it.

View Postmarinus, on 06 July 2011 - 09:48 AM, said:

It can look very confusing if you overuse it, almost like the programming language BrainF***


I don't think one should shy away from it. I feel it is elegant. Teaching myself C# for the past year, I understood the ?: operator before I grasped how Properties were different than Fields. I wouldn't shy away from Properties, and I won't shy away from the tertiary operator.


View PostCurtis Rutland, on 06 July 2011 - 09:50 AM, said:

There's another little-known operator in C#: ??, the Null Coalescing Operator.


I did not know about that operator. I will definitely use this in my coding.
Was This Post Helpful? 1
  • +
  • -

#15 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Busy with the ?: Operator

Posted 08 July 2011 - 07:26 PM

View PostRobin19, on 07 July 2011 - 05:23 PM, said:

View PostKilorn, on 06 July 2011 - 08:39 AM, said:

Maybe this isn't such a big deal if you're the only programmer on the project, but if you're working on a team, especially if some members on the team are relatively new to programming, perhaps it would be best to limit using this operator as it seems that most entry level programmers have never even heard of it.

View Postmarinus, on 06 July 2011 - 09:48 AM, said:

It can look very confusing if you overuse it, almost like the programming language BrainF***


I don't think one should shy away from it. I feel it is elegant. Teaching myself C# for the past year, I understood the ?: operator before I grasped how Properties were different than Fields. I wouldn't shy away from Properties, and I won't shy away from the tertiary operator.


View PostCurtis Rutland, on 06 July 2011 - 09:50 AM, said:

There's another little-known operator in C#: ??, the Null Coalescing Operator.


I did not know about that operator. I will definitely use this in my coding.


I have started using ?: operator alot , and becoming a fan of writing code in this style

like today in "Active Server Pages dot net"!!

e.g


 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsValidToHere()) { Response.Redirect("my-not-known-page.aspx"); }
        }

        public bool IsValidToHere()
        {

            string url = Request.Url.AbsoluteUri;

            return (url.Contains("location")) ? true : false;



        }

     


Works like a charm :)

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2