patterncsharpMinor
Dealing with non-required user input
Viewed 0 times
nonwithuserdealinginputrequired
Problem
I'm working on a page where a user can create new records, but not all values are required. When creating the SqlCommand with potentially null values, I'm wondering what's the best way, from a code readability standpoint, to check if there's any valid/useful information in a given field. If there is, add that information to the command, if there's isn't, add a DBNull.
Let's say we have a textbox,
That seems like a lot of unnecessary typing. But trying to get clever and condense it into a single statement:
That seems a little too condensed, harder to parse and understand. Not too difficult to figure out, but (at least to me), also not immediately apparent what's going on. So a trade-off:
(Or the above in a
Another option that just occurred as I wrote this question:
Are any of these methods prefered? I am going about this completely the wrong way? Am I just way over-thinking this? It's the latter, isn't it?
Let's say we have a textbox,
Address, which is non-compulsory (comm is the SqlCommand):if(String.IsNullOrEmpty(Address.Text))
{
comm.Parameters.AddWithValue("@Address", (Object) DBNull.Value)
}
else
{
comm.Parameters.AddWithValue("@Address", (Object) Address.Text);
}That seems like a lot of unnecessary typing. But trying to get clever and condense it into a single statement:
comm.Parameters.AddWithValue("@Address",
String.IsNullOrWhiteSpace(Address.Text) ?
(Object) DBNull.Value :
(Object) Address.Text
);That seems a little too condensed, harder to parse and understand. Not too difficult to figure out, but (at least to me), also not immediately apparent what's going on. So a trade-off:
Object addressParam =
String.IsNullOrEmpty(Address.Text) ?
(Object) DBNull.Value :
(Object) Address.Text;
comm.Parameters.AddWithValue("@Address", addressParam);(Or the above in a
if/else block).Another option that just occurred as I wrote this question:
Object addressParam = (Object) DBNull.Value;
if(!String.IsNullOrWhiteSpace(Address.Text))
{
addressParam = (Object) Address.Text;
}Are any of these methods prefered? I am going about this completely the wrong way? Am I just way over-thinking this? It's the latter, isn't it?
Solution
I personally prefer your 3rd example
It easily reads that you are evaluating and initializing your Object and directly after you're using your newly instantiated Object. It also doesn't take up as much space as your if-statements and it doesn't really slow you down when reading the code -> you see an Object being initialized and then used, and when needed you can always take an extra second to see what you're evaluating.
Object addressParam =
String.IsNullOrEmpty(Address.Text) ?
(Object) DBNull.Value :
(Object) Address.Text;
comm.Parameters.AddWithValue("@Address", addressParam);It easily reads that you are evaluating and initializing your Object and directly after you're using your newly instantiated Object. It also doesn't take up as much space as your if-statements and it doesn't really slow you down when reading the code -> you see an Object being initialized and then used, and when needed you can always take an extra second to see what you're evaluating.
Code Snippets
Object addressParam =
String.IsNullOrEmpty(Address.Text) ?
(Object) DBNull.Value :
(Object) Address.Text;
comm.Parameters.AddWithValue("@Address", addressParam);Context
StackExchange Code Review Q#32764, answer score: 5
Revisions (0)
No revisions yet.