patterncsharpModerate
Check if any of class properties is not null/empty/was assigned
Viewed 0 times
classpropertiesnullanyemptyassignedwaschecknot
Problem
I have a class that I use to display on a web page fieldset. However, all of the properties of this class are optionally filled in by the user, which means all of them could be null/default. In this case I would like not to render the fieldset at all, instead of displaying an empty one.
I wrote a simple
The
What do you think? Will this become potentially expansive as the class grows?
I wrote a simple
isEmpty() method that checks all property values using reflection, but I'm afraid this may become way too expansive.public class User
{
[Display(Name = "First Name:")]
public string FirstName { get; set; }
[Display(Name = "Last Name:")]
public string LastName { get; set; }
public bool isEmpty()
{
Type t = this.GetType();
var properties = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var prop in properties)
{
var value = prop.GetValue(this, null);
if (value != null && (string)value != "")
{
return false;
}
}
return true;
}
}The
isEmpty(), of course, assumes all properties are strings.What do you think? Will this become potentially expansive as the class grows?
Solution
Naming:
Don't use variable names such as
Class names, public fields and method names use
Use
The method:
Your code looks good. You could however make it cleaner by using LinQ. Loops won't be avoided but it looks cleaner. I'm not saying this is a perfect solution but here goes:
I could also have used the
You can take the comparison code and throw it in an extension method for reusability. It also makes your method-code cleaner, for example:
Since you only have two properties, performace won't be a problem. Even if you were to add a couple more properties. Hope this helps!
Don't use variable names such as
t. They're not meaningful, not for you, not for others. In this case, use type.Class names, public fields and method names use
PascalCase and not camelCase. So, isEmpty will become IsEmpty. But then again, this is also not a meaningful name. Boolean methods or properties in C# will most likely start with Is or Has. An example for the name of your method would be HasAllEmptyProperties.var keyword:Use
var to declare your variables instead of declaring them explicitly. The compiler will determine the type for you and your code looks cleaner.The method:
Your code looks good. You could however make it cleaner by using LinQ. Loops won't be avoided but it looks cleaner. I'm not saying this is a perfect solution but here goes:
public bool HasAllEmptyProperties()
{
var type = GetType();
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var hasProperty = properties.Select(x => x.GetValue(this, null))
.Any(y => y != null && !String.IsNullOrWhiteSpace(y.ToString()));
return !hasProperty;
}I could also have used the
All() method and positive comparison instead of Any(). But this is inefficient as all elements have to be checked against the condition whereas Any() will return as soon as an element satisfies the condition (like in your loop).You can take the comparison code and throw it in an extension method for reusability. It also makes your method-code cleaner, for example:
public static class Extensions
{
public static bool IsNullOrEmpty(this object obj)
{
return obj == null || String.IsNullOrWhiteSpace(obj.ToString());
}
}
public bool HasAllEmptyProperties()
{
var type = GetType();
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var hasProperty = properties.Select(x => x.GetValue(this, null))
.Any(x => !x.IsNullOrEmpty());
return !hasProperty;
}Since you only have two properties, performace won't be a problem. Even if you were to add a couple more properties. Hope this helps!
Code Snippets
public bool HasAllEmptyProperties()
{
var type = GetType();
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var hasProperty = properties.Select(x => x.GetValue(this, null))
.Any(y => y != null && !String.IsNullOrWhiteSpace(y.ToString()));
return !hasProperty;
}public static class Extensions
{
public static bool IsNullOrEmpty(this object obj)
{
return obj == null || String.IsNullOrWhiteSpace(obj.ToString());
}
}
public bool HasAllEmptyProperties()
{
var type = GetType();
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var hasProperty = properties.Select(x => x.GetValue(this, null))
.Any(x => !x.IsNullOrEmpty());
return !hasProperty;
}Context
StackExchange Code Review Q#70341, answer score: 13
Revisions (0)
No revisions yet.