471,122 Members | 1,098 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

String not modified as a warning?

OK if you have a string object and do the following:

string temp;

temp = "test this";

temp.Remove(0,4);

You should at LEAST get a warning that the string is not modified... I have
done this probably a dozen times when I meant to do the temp =
temp.Remove(0,4);, but I forgot. How can I turn that on to be a warning, or
an error. I am already running at the highest warning level and it doesn't
say a word about that...

I can't help but think junior programmers need that warning as well.

Jason
Nov 16 '05 #1
5 1146
Jason,

You do nothing illegal. What kind of warning do you expect?
String is just another class. Why the compiler should thread that class in a
different way.

If I write a class that is immutable how the compiler will know to report a
warning if one use it in some incorrect manner?

The only warning that the compiler can report is that Remove moethod returns
a value which is not handled. But if it does that I can only immaginen how
many warnings one will get for otherwise completely correct program.

--

Stoitcho Goutsev (100) [C# MVP]
"Jason Short" <du*****@keepthespam.com> wrote in message
news:40***********************@news.usenetguide.co m...
OK if you have a string object and do the following:

string temp;

temp = "test this";

temp.Remove(0,4);

You should at LEAST get a warning that the string is not modified... I have done this probably a dozen times when I meant to do the temp =
temp.Remove(0,4);, but I forgot. How can I turn that on to be a warning, or an error. I am already running at the highest warning level and it doesn't say a word about that...

I can't help but think junior programmers need that warning as well.

Jason

Nov 16 '05 #2
Hi Jason,

"Jason Short" <du*****@keepthespam.com> wrote in message
news:40***********************@news.usenetguide.co m...
OK if you have a string object and do the following:

string temp;

temp = "test this";

temp.Remove(0,4);

Although the code does nothing, it is perfectly legal to do so - and you
won't find
any warnings you can turn on to prevent it (AFAIK).

Better code right the first time ;-)

(If we're talking about half-nasty "bugs"; the first times it can be hard to
spot
never-ending recursive calls in properties, like:

public int Foo
{
get { return foo; }
set { Foo = value; }
}

)

--
Lars Wilhelmsen
http://www.sral.org/
Software Engineer
Teleplan A/S, Norway
Nov 16 '05 #3
>
You do nothing illegal. What kind of warning do you expect?
String is just another class. Why the compiler should thread that class in a different way.
We have a compiler that can detect "dead code" - Code that can never be
reached.
It can detect when locks and not released.
It can detect and repair memory leaks.

Why can't it tell you when one of IT's classes is not being used? This is
not my class... It is a Dot Net class.

If I write a class that is immutable how the compiler will know to report a warning if one use it in some incorrect manner?
That is different. You can then be responsible.
The only warning that the compiler can report is that Remove moethod returns a value which is not handled. But if it does that I can only immaginen how
many warnings one will get for otherwise completely correct program.


There used to be pragmas for turning on and off the most esoteric problems
in MSVC++ years ago. There were ones even to warn you when you included a
file twice... I find it hard to believe there is NO way to turn on a
warning for this...

Nov 16 '05 #4
We use this one on the code test for new hires :-)

Eric
public int Foo
{
get { return foo; }
set { Foo = value; }
}


Nov 16 '05 #5
You do nothing illegal. What kind of warning do you expect?
String is just another class. Why the compiler should thread that class in
a
different way.
We have a compiler that can detect "dead code" - Code that can never be
reached.
It can detect when locks and not released.
It can detect and repair memory leaks.


Yeah, there are such lint-like tools and they have been around forever (take
a look at FxCop for example). But they are tools. We are talking about the
compiler. The compiler has to be fast and to produce correct code (correct
with regards of the language's syntax and semantics). It has to warn you of
things that are 100% wrong or there is big chances for mistakes (again with
regards of the language's syntax and semantics).
In your case you don't do anything illegal.

Why can't it tell you when one of IT's classes is not being used? This is
not my class... It is a Dot Net class.

It is a library class it is not part of the language itself.
If I write a class that is immutable how the compiler will know to report a
warning if one use it in some incorrect manner?
That is different. You can then be responsible.

Why should I be responsible of how the progammers use that type of mine. I
provided the programmers with clear documentation and put notes in the
documentation about the immutability of that type. There is nothing I can do
more about it. As microsoft did. It is well documented and one of the first
things newbies learn is that strings are immutable.
The only warning that the compiler can report is that Remove moethod

returns
a value which is not handled. But if it does that I can only immaginen

how many warnings one will get for otherwise completely correct program.


There used to be pragmas for turning on and off the most esoteric problems
in MSVC++ years ago. There were ones even to warn you when you included a
file twice... I find it hard to believe there is NO way to turn on a
warning for this...


The problem is that if I turn it on it will be on for all of the types . And
that is going to be a lot of warnings for a single file.
If you want thorough check up use FxCop. I'm pretty sure it will report
those kind of mistakes
--
Regards,
Stoitcho Goutsev (100) [C# MVP]

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Senthilraja | last post: by
34 posts views Thread by Chad | last post: by
4 posts views Thread by kyle.tk | last post: by
21 posts views Thread by phpCodeHead | last post: by
20 posts views Thread by karthikbalaguru | last post: by
8 posts views Thread by puzzlecracker | last post: by
13 posts views Thread by Andreas Eibach | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.