|
-
December 30th, 2008, 04:13 AM
#31
Re: Best way to check if a string is empty
 Originally Posted by dee-u
We should agree that it boils down to the personal inclination of a developer or organization, some would prefer performance over readability and readable for some can be not that readable to others, do you agree?
Except that in either case, 'performance' doesn't come into it. It's another premature optimisation If you're worried about performance differences between String.IsNullOrEmpty and string.Length == 0, you're worrying about completely the wrong things. What you should be worried about is whether or not you want to allow a null or empty string. If you don't, you should use String.IsNullOrEmpty.
Of course, the source of your string should be part of your 'view', which should be completely disconnected from your model and controller (MVC pattern), so your model will be doing all the validation, which means you can't rely on the source of your data being a TextBox, which means you can't rely on it being non-null.
Finally, near in mind that you validate before you store. Validation will done a hell of a lot less than the variable will be used, so even if you do 'mega optimise' your validation methods you're not going to get a real world performance boost.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
December 30th, 2008, 03:50 PM
#32
Re: Best way to check if a string is empty
I am not worried about the performance, its just that using .Length has its merits and should not be avoided like a plague, isn't it? And I am not worrying at all, it has become a second nature and to be honest I have not yet encountered a scenarion wherein a string has the value of null, I was not even aware of it until it has been mentioned in this thread.
-
December 30th, 2008, 04:32 PM
#33
Re: Best way to check if a string is empty
 Originally Posted by dee-u
I am not worried about the performance, its just that using .Length has its merits and should not be avoided like a plague, isn't it?
I'd avoid it (or any other approach that uses a string instance method to determine whether the string is valid).
As I and others have mentioned earlier, you may consider validating the string data before storing it into the string variable. Then all your checks can be reduced to String.IsNullOrEmpty calls.
To illustrate, consider a situation where a string is valid if it contains alpha/numeric chars, no leading or trailing spaces, is not null and cannot start with a 'z' character.
If you validate (and/or trim) a string such as this prior to storing it into a variable, it just makes life easier.
Would you rather perform a validity check like this:
Code:
if( String.IsNullOrEmpty( str ) )
{
//...
}
or
Code:
if( str != null && str.Trim( ).Length != 0 && str.Trim( )[ 0 ] != 'z' )
{
//...
}
-
December 30th, 2008, 04:59 PM
#34
Re: Best way to check if a string is empty
Hmmmnnn... That makes me wonder, if we need to pass something to a Business Logic Layer which is supposed to validate rules, are we supposed to validate them first in the GUI before sending them over to the BLL? I usually just do this in the GUI:
Code:
asset_bl.BLOCK_NO = textBLOCK_NO.Text;
and just let my BLL do the validation. Do you mean to say I need to validate the contents of textBLOCK_NO.Text first before setting them to asset_bl.BLOCK_NO?
-
December 30th, 2008, 05:24 PM
#35
Re: Best way to check if a string is empty
It depends based on the type of feedback you wish to give your user.
If you validate in the UI, the user can get immediate feedback which is often quite nice.
Validating in the business object is good too; especially if the business object is accessed by something other than the UI.
So it seems maybe the logic should be in both places?
Well not really. Consider using the validation components in the enterprise library. These allow you to create validation attributes on the business object properties and then perform validation external to the business object. For UI's (like WinForms and WPF) this validation can be automatically connected so the user receives immediate feedback.
The validators can also be called on the business object level. With the entlib validator approach the validation logic is only in one spot but the validation can occur on different layers.
One last comment: You might consider following the C# .Net style guidelines/naming conventions. This will make your code easier to read for folks familiar with C#.
-
December 31st, 2008, 04:21 AM
#36
Re: Best way to check if a string is empty
 Originally Posted by marceln
Code:
if(String.IsNullOrEmpty(str))
{
//...
}
It is only for C# 3. not earlier versions. At least not for Framework 1.1.
-
December 31st, 2008, 04:02 PM
#37
Re: Best way to check if a string is empty
 Originally Posted by nabeelisnabeel
It is only for C# 3. not earlier versions. At least not for Framework 1.1.
It's available on .Net 2.0 and above. Hopefully no one is forced to code in 1.1 or earlier.
-
January 1st, 2009, 01:30 AM
#38
Re: Best way to check if a string is empty
Pointing above mentioned point was necessary because we do not know in which framework version the topic starter is developing his application.
-
January 1st, 2009, 01:40 AM
#39
Re: Best way to check if a string is empty
.NET version 1.1 is depricated, so I think we can safely assume that anyone posting here is using version 2.0 or higher. Going from 1.1 to 2.0 would not break anyone's code.
-
January 1st, 2009, 02:04 AM
#40
Re: Best way to check if a string is empty
 Originally Posted by BigEd781
.NET version 1.1 is depricated, so I think we can safely assume that anyone posting here is using version 2.0 or higher. Going from 1.1 to 2.0 would not break anyone's code.
Correction:
.NET version 1.1 is in the process of depreciation, not depreciated yet.
-
January 1st, 2009, 09:34 AM
#41
Re: Best way to check if a string is empty
 Originally Posted by nabeelisnabeel
Correction:
.NET version 1.1 is in the process of depreciation, not depreciated yet.
So..if you walked into a hotel with cracks in the walls and stained rugs, and no one had any intention of fixing it, would you say it was deprecated, or in the process of deprecation? In practice, it is an outdated API that no one should be using, but it is true that it has not yet been fully left behind in the dust (yet).
-
January 1st, 2009, 10:03 AM
#42
Re: Best way to check if a string is empty
 Originally Posted by nabeelisnabeel
Correction:
.NET version 1.1 is in the process of depreciation, not depreciated yet.
Officially:
 Originally Posted by Microsoft Product LifeCycle
Retired .NET Framework 1.1 7/10/2003 -10/14/2008
Over two months past EOS
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 1st, 2009, 01:05 PM
#43
Re: Best way to check if a string is empty
i think writing code base on .Net 1.1 is a horrible mistake because there are radical differences between 1.1 and 2.0; but not so many diffrence between 2 and 3.5
Please rate my post if it was helpful for you.  Java, C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|