Is there a way to make this line shorter?
bool pass = d != null && d["k"] != null && (bool)d["k"];
Note: "k" is actually "a longer id"; I replaced it to make it more readable in this post. Many of your suggestions don't check whether d is null.
Is there a way to make this line shorter?
bool pass = d != null && d["k"] != null && (bool)d["k"];
Note: "k" is actually "a longer id"; I replaced it to make it more readable in this post. Many of your suggestions don't check whether d is null.
I would question why you are trying to make it shorter.
Shorter code doesn't benefit anybody. It makes it far harder for someone else to read it.
Personally, I would be asking "how can I make this code easier to read", and the answer to that would be split it up into multiple lines, and only do 1 thing on each line.
The only 'null shorthand' available in C# is the null-coalescing operator, which allows you to define a default value for a null object. But for your code, I'd recommend aiming for readability, not line size.
I would question why you are casting d["k"] to a bool. Either it is already a bool or it should be compared to something to get a bool. This would make things clearer.
Anyway, I suppose this might work:
bool pass = (d != null) && d["k"] ?? false
Try this:
bool pass = d != null && (bool)(d["k"] ?? false);
Yes, extract a function from it and call it something like doTheValuesInTheFooBarDictionaryPassTheRebarTest
.
That's much easier to read. Of course, the method will still contain your line above, but the reader of the code (from here 'till the end of time) will have a MUCH more pleasant experience when skimming the code.
By making code shorter, what are you hoping to achieve? Quicker to read? (it'll take longer for the brain to parse it!). Quicker to type? (you type this stuff once and either modify or read it again and again (and again!)). Generally, longer, more descriptive variables are better, and if you can wrap them in an encapsulating method, then do that too.
You can get rid of the first expression by initializing d in the constructor. I recommend you to use generic dictionary i.e.
Dictionary<string,bool> d = new Dictionary<string,bool>();
That way you don't have to cast the value. And use the ContainsKey method as opposed to directly accessing the value. So your final expression
bool pass = d.ContainsKey("k") && d["k"];
This is not shorter, but more reliable (and perhaps a bit 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 into a function.
Yes. Make d strongly typed (e.g. use Dictionary<> instead of Hashtable)
bool pass = d != null && d["k"];
It would help if we knew the type of d!
You could replace it with a function call that takes in a dictionary and a key...
I would not try to shorten it further, I would try to give it a nice name. Writing an extension method or modifying the class (if you have access to the code) sounds good to me.
// 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");
I would much prefer to see something like this:
bool pass = false;
if(d != null && d["k"] != null)
pass = (bool)d["k"];
or if wanted to write a handy extension method for System.Object
that would make it look like this:
bool pass = false;
if(!d.IsNull && !d["k"].IsNull)
pass = (bool)d["k"]
I find this much more understandable than the original version.