trim(string) vs string.trim

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

I have an app that makes decisions based on string content. I need to make
sure that a string does not contain only spaces or newlines. I am using the
syntax 'Trim(String)" and it works fine. I thought I'd change it to the VB
..NET method "String.Trim" but that throws an object exception.

Which brings the question: is it compliant to use Trim(String), or is it
more within etiquette to use If Not String Is Nothing Then String.Trim?
 
Terry,
You should always check your parameters/variables for null references
whenever there is doubt.

You can use the negating logic if you prefer, its just not as straight
forward (it is here, I just avoid negating logic whenever possible)
If Not Is Nothing Then
stringVariable = stringVariable.Trim
End If

If stringVariable is nothing then
stringVariable = string.empty
Else
stringVariable = stringVariable.Trim
End If

Whatever you decide, just be consistient.
 
Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
easier to read imo.

If stringVariable IsNot Nothing Then
stringVariable = stringVariable.Trim
End If

/claes
 
Claes,
Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
easier to read imo.

Maybe for native English people, I find it terrible to read.

Cor
 
Terry said:
I have an app that makes decisions based on string content. I need to make
sure that a string does not contain only spaces or newlines. I am using the
syntax 'Trim(String)" and it works fine. I thought I'd change it to the VB
.NET method "String.Trim" but that throws an object exception.

Which brings the question: is it compliant to use Trim(String), or is it
more within etiquette to use If Not String Is Nothing Then String.Trim?

The advantage of using Trim instead of String.Trim is exactly that Trim
will recognize when a String is Nothing and return "" as a result. If
this is the logic of your application, then instead of doing:

If SomeStr Is Nothing then
Value = ""
'I personally preffer Value = String.Empty
Else
Value = String.Trim
End If

you could spare the effort and just use Value = Trim(SomeStr)

On the other hand, if you must know when a passed string is invalid
(Nothing) then probably checking for Nothing before calling String.Trim
is the way to go.

Regards,

Branco.
 
Cor said:
Claes,


Maybe for native English people, I find it terrible to read.

Cor,

I *am* English and I think the "Eye-Snot" operator is appalling.

I've been using something like

Function IsSomething( ByVal oThing As Object ) As Boolean
Return Not (oThing Is Nothing )
End Function

(in various guises) for more years than I like to think about and have
no intention of changing.

OK, there's an overhead in calling a function, but any half-decent
compiler would optimise that out.

Regards,
Phill W.
 
Phill,

In your sample it is a boolean.

My is told that English people seem to use IsNot Nothing when it is
Something.

I prefer "something" because I hate those possible constructions as it is in
VB.Net as
If Not (Not ObjectAddress IsNot Nothing) what is a valid instruction to
return a boolean if that address is something.

But as me is told is this for English people easy readable.

Not all spoken language are always algabraic correct do you know.

Cor
 
Ok, not being formally trained in the art of coding, what exactly is
apalling about IsNot? It seems like a perfectly valid and logical option to
me. Your method is like making 3 left turns to get to the store when a
single right turn would have gotten you there, simply because you don't like
right turns.
 
Cor Ligthert said:
Claes,


Maybe for native English people, I find it terrible to read.

Cor

Well, I'm Swedish and I find it a lot better than using 'If Not ... Is
Nothing...'. That just looks weird. Until they add support for 'If ... Is
Something' I'm sticking with the IsNot operator

/claes
 
Cor Ligthert said:
Phill,

In your sample it is a boolean.

My is told that English people seem to use IsNot Nothing when it is
Something.

I prefer "something" because I hate those possible constructions as it is
in VB.Net as
If Not (Not ObjectAddress IsNot Nothing) what is a valid instruction to
return a boolean if that address is something.

Now you're abusing the language :-)
(And you failed my code review ;-))

The above is eqvivalent to:
If ObjectAddress IsNot Nothing
or
If Not ObjectAddress Is Nothing

Personally I find the first form easier to read (and I'm not English). A
'Something' keyword would be nice though...

/claes
 
Cor said:
Claes,


Maybe for native English people, I find it terrible to read.

Cor

The term closely follows SQL. Is SQL, the IS operator supports NOT, as
opposed to =, which does not support NOT.

Thus A = B and NOT (A = B), but A NOT = B is invalid.

However, IS does support it so: A IS NULL, NOT A IS NULL, and A IS NOT
NULL, are all valid.

Personally, i welcome the IsNot operator. Being "Is" is a special case
(checking the meta-data as opposed to the data itself) having it's own
negativity operator is fine, especially when it sort of follows a well
known SQL standard (for checking meta-data).

B.
 
Brian, Terry, Claes,

The "IsNot" is one of my Carthago's.

Even this would be better in my idea.

\\\
Dim IsNotYetInstanced As Object
Dim myarray As ArrayList
If myarray Is IsNotYetInstanced Then
MessageBox.Show("I am not something")
End If
///

This can every kid make, the same as probably is done.

With Jay I would like if there was something as "Something" to show that the
address part of an object was not empty.

Cor
 
With extension methods in VB9, you will be able to do something like:

<Extension()> _
Public Module CustomExtensions
<Extension()> _
Public Function IsSomething(byVal target as Object) as Boolean
Return Not target Is Nothing
End Function
End Module

Then to consume it, as long as your CustomExtensions are in scope, you will
be able to take any object and call it's extension method IsSomething as
follows:

dim foo as Object
If foo.IsSomething
'Do Work
End If

Note, VB9 is still under development and the syntax is subject to change
before release in Orcas, but it does give you an idea of what you can do.
Be careful, extension methods can be is a very big gun, with which you can
easily shoot yourself in the foot.

Jim Wooley
http://devauthority.com/blogs/jwooley
Brian, Terry, Claes,

The "IsNot" is one of my Carthago's.

Even this would be better in my idea.

\\\
Dim IsNotYetInstanced As Object
Dim myarray As ArrayList
If myarray Is IsNotYetInstanced Then
MessageBox.Show("I am not something")
End If
///
This can every kid make, the same as probably is done.

With Jay I would like if there was something as "Something" to show
that the address part of an object was not empty.

Cor
Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
Jim,

I don't want IsSomething, that are solutions thought by developers inside
their own community closing the doors for the world. The same for me as not
using the "+" in programs but creating a function for that IsPlus(ValueA,
ValueB).

It has to be

If myObjectAdress is Something

Cor
 
Terry,
Please note before leaving your code in place that the Trim function is
implemented in the Microsoft.VisualBasic namespace for backward
compatability. If you use Reflector you'll find that the internal
implementation begins with the following check.

If ((str Is Nothing) OrElse (str.Length = 0)) Then
Return ""
End If

Now, I realize this call does the "work" for you, but, assume you (or
someone else)wants to convert the project to another .net language. C# for
instance does not implement a global Trim() method. The developers porting
your code are then forced** to change every reference to Trim() to either a
utility function or to the native framework methods. It's best to just
conform and avoid the backward compatible functions.


** The conversion utility may make this change for you.
 
Jared said:
Terry,
Please note before leaving your code in place that the Trim function
is implemented in the Microsoft.VisualBasic namespace for backward
compatability. If you use Reflector you'll find that the internal
implementation begins with the following check.

If ((str Is Nothing) OrElse (str.Length = 0)) Then
Return ""
End If

Now, I realize this call does the "work" for you, but, assume you (or
someone else)wants to convert the project to another .net language.
C# for instance does not implement a global Trim() method. The
developers porting your code are then forced** to change every
reference to Trim() to either a utility function or to the native
framework methods. It's best to just conform and avoid the backward
compatible functions.


** The conversion utility may make this change for you.

:

Alright, I've been seeing people rant and rave about no using the VisualBasic namespace in a VB application and that it is there for
backward compatibility (which I don't agree with. I will agree that VisualBasic.Compatibility is). My question is this, why chose
VB as a language and then go out of your way to avoid using the features built into the language via the VisualBasic namespace. I
just don't get it. If one wants to write a c# app, why not just do it in c#? Perhaps I don't get it because I have been writing VB
apps using MS dialects of Basic since 1982. The day I stop using the VisualBasic Namespace (excluding the .Compatibility) is the
day I stop writing in VB.

Just My $0.02.
 
Jared said:
Please note before leaving your code in place that the Trim function is
implemented in the Microsoft.VisualBasic namespace for backward
compatability.

No it's not. Microsoft.VisualBasic.*Compatibility* contains all the
archaic stuff, liked fixed-length Strings, that we really /ought/ to
live without these days, but Microsoft.VisualBasic is part and parcel of
the [Visual Basic] language. Just try removing this assembly from your
system and see how many VB.Net applications still run, even those where
you've managed to /avoid/ using any of its methods yourself. (Don't
bother, the answer is none. Build anything in VB.Net and is will be
dependent on MS.VB).
Now, I realize this call does the "work" for you, but, assume you (or
someone else)wants to convert the project to another .net language.
C# for instance does not implement a global Trim() method.

It's /not/ a Global Method.
It's a Framework method, just like any other; it's just that it doesn't
happen to be part of the System.* hierarchy.
And there's nothing to stop you Using Microsoft.VisualBasic in a C#
program as well.
(well; at least not on a Windows machine, anyway) ... ;-)

Regards,
Phill W.
 
Jared,

Just to say it in one line as Phill already wrote, all Visual Basic
namespace methods (not language parts) works as good as every other Net
namespace method in C#.

It has only to be referenced in C#, because the namespaces it is not
standard referenced in the designer as it is in VB.Net.

However because that diehard C++ developers want to avoid everything from VB
as a plague is that seldom be done.

Most VBNet programmers take everything that gives the quickest results. It
is just a way of thinking. The quality of those results is in this case the
same.

Just as addition,

Cor
 
I think you guys are taking me for someone who is advocating that all should
use C#. I’m not trying to convert anyone, I simply stated that when coding,
in whatever language you choose, one should try to conform to the standards
the industry has put in place.

I can appreciate your frustration, as until recently I developed solely in
VB. I understand your points, and yes I know that I can use the
Microsoft.VisualBasic namespace in C# applications, I’ve never disputed this,
nor was it ever the topic of discussion.

My points, with the exception of my backwards compatibility comment, are
language agnostic and follow the best practices laid out in books such as
Code Complete and Pragmatic Programmer. I simple stated that you should use
a utility class and perform your trim there, even if you use the Trim method
from the Microsoft.VisualBasic namespace and later I decide to convert it I
only have to change it in a single location. This saves me from adding an
import/using statement to each and every object that is using the
Microsoft.VisualBasic Trim function. Isn’t the goal to keep the class closed
for modifications? Why go back and re-work numerous classes for such a small
change?

On another similar issue:
Recently, I was working with a third party GIS mapping application. There
where few interfaces and/or they did not publicly expose some of the internal
structures. We were tasked with interacting with the application’s API to
perform search services through a web service interface. The problem arose
when we found that we could not gain access to some of the properties using
C#. Upon inspection of sample services gathered from the applications
creators we found they were using the late binding features of Visual Basic
to perform nearly all of the operations. While this wasn’t a huge obstacle,
we were forced to use create a Visual Basic project to interact with the
application, either that or use reflection to instantiate internal/friend
classes of the framework.

This is the type of thing I’m trying to avoid in the future, the designers
of the application had the same mentality, it’s easy enough to do using a
particular languages feature, why change the COM object, we can just force
them to use Visual Basic or a more elaborate workaround.

I would love to hear you constructive comments on these subjects. Again,
try not to turn this into a language discussion.

Jared
 
Back
Top