Null reduction in C #?

Is there any way to make this line shorter?

bool pass = d != null && d["k"] != null && (bool)d["k"]; 

Note: "k" is actually a "longer identifier"; I replaced it to make it more readable in this post. Many of your suggestions do not check if d is null.

+4
source share
13 answers

Try the following:

 bool pass = d != null && (bool)(d["k"] ?? false); 
+13
source

This is barely readable, so why make it shorter?

+32
source

I would question why you are trying to make it shorter.

A shorter code is not beneficial to anyone. It is even harder for someone else to read.

Personally, I would ask, โ€œHow can I make this code easier to read,โ€ and the answer to this will split it into several lines and do only 1 thing in each line.

The only "empty shorthand" available in C # is the null-coalescing operator , which allows you to specify the default value for a null object. But for your code, I would recommend focusing on readability rather than line size.

+8
source

Yes, extract a function from it and call it something like doTheValuesInTheFooBarDictionaryPassTheRebarTest .

It is much easier to read. Of course, this method will still contain your line above, but the code reader (from here "until the end of time" will have a more pleasant experience when viewing the code.

In short, what are you hoping to achieve? Faster to read? (the brain will need more time to disassemble it!). Faster to dial? (you type this material once and change or read it again and again (and again!)). Generally, longer, more descriptive variables are better, and if you can wrap them with encapsulation, do this too.

+5
source

You can get rid of the first expression by initializing d in the constructor. I recommend that you use a generic dictionary i.e.

 Dictionary<string,bool> d = new Dictionary<string,bool>(); 

This way you do not need to specify a value. And use the ContainsKey method, not direct access to the value. So your final expression

 bool pass = d.ContainsKey("k") && d["k"]; 
+4
source

I would not try to shorten it further, I will try to give it a nice name. I like to write an extension method or modify a class (if you have access to the code).

 // Better use a longer and more descriptive name here. public static Boolean Pass(this Foo foo, String bar) { return (foo != null) && (bar != null) // Maybe this test, too? && (foo[bar] is Boolean) && (foo[bar] as Boolean); } 

And use it like that.

 Boolean pass = d.Pass("k"); 
+3
source

A combination of operators with zero-conditional and zero coalescence should work here:

 bool pass = (bool) (d?["k"] ?? false); 

To clarify:

  • If d is null, d?["k"] is null
  • If d?["k"] is null, d?["k"] ?? false d?["k"] ?? false - false (marked as object )
  • Result: listing

An alternative could be passing in front of an operator with zero coalescing, but casting to a type with a zero value:

 bool pass = ((bool?) d?["k"]) ?? false; 

Note that the null condition operator was introduced in C # 6.

+3
source

I would ask why you chose d ["k"] in bool. Either this is a bool, or you need to compare it with something to get a bool. This will simplify the situation.

In any case, I suppose this might work:

 bool pass = (d != null) && d["k"] ?? false 
+2
source

Yes. Make d strongly typed (e.g. use Dictionary <> instead of Hashtable)

 bool pass = d != null && d["k"]; 

This would help if we knew type d!

+2
source

This is not shorter, but more reliable (and perhaps a little more efficient):

 bool pass = false; if (d != null) { object value; if (d.TryGetValue("k", out value)) { pass = value is bool && (bool)value; } } 

I would recommend moving it to a function.

+1
source

You can replace it with a function call that takes a dictionary and a key ...

+1
source

I would rather see something like this:

 bool pass = false; if(d != null && d["k"] != null) pass = (bool)d["k"]; 

or if you want to write a convenient extension method for System.Object that will make it look like this:

 bool pass = false; if(!d.IsNull && !d["k"].IsNull) pass = (bool)d["k"] 

I find this more understandable than the original version.

+1
source

In C # 6 and above you can use

 bool pass = (bool)(d?["k"] ?? false); 

If the compile time type d ["k"] is bool? you can omit the throw

 bool pass = d?["k"] ?? false; 
0
source

All Articles