Console.WriteLine(1.ToString() + "23");

Hi,
I see R# is suggesting for the above to remove .ToString().

I may be missing something here, but that 1.ToString() in this sample is actualy used in order to avoid boxing for the value type instance of 1.

Is then your suggestion on removing ToString() actually misleading? Should you check first if it is not a ValueType?

Sorry if discussed/answered already.

~Stanislav Dvoychenko.

4 comments
Comment actions Permalink

If I remember well, and someone please correct me if I'm wrong, but the compiler does a lot of stuff when it comes to string manipulation... In this case, I'm fairly sure the compiler actually adds ToString() to the assembly, thus why its redundant.

0
Comment actions Permalink

You see behind the scenes .net uses String.Concat(object, object) for the above to get a string to use as a parameter to Console.WriteLine. This leads to the boxing of 1.
Overall it means that R# blindly or lazily forces developers to use anti-pattern actually. I wonder if it would be that complex for R# team to be more smart on this, have an option like "suggest removing .ToString() on reference types only".
Only using R# for few weeks now, I find myself addicted to have no yellow in my sight, but this and var are killing me. This because there is no option/smartness for it, and var, just because R# decided to follow after populistic article instead of any OOD rationale.

Edited by: Stanislav Dvoychenko on Sep 7, 2008 2:09 PM

Edited by: Stanislav Dvoychenko on Sep 7, 2008 2:18 PM

0
Comment actions Permalink

I'm absolutely sure that performance degradation due to boxing can't be
registered with any tool. The internal complexity of string concatenation is
much more higher than value boxing. And, thus, keeping code clean is
preferrable.

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Stanislav Dvoychenko" <staninprague@gmail.com> wrote in message
news:14323217.113101220782068433.JavaMail.jive@app4.labs.intellij.net...

You see behind the scenes .net uses String.Concat(object, object) for the
above to get a string to use as a parameter to Console.WriteLine. This
leads to the boxing of 1.
Overall it means that R# blindly or lazily forces developers to use
anti-pattern actually. I wonder if it would be that complex for R# team to
be more smart on this, have an option like "suggest removing .ToString()
on reference types only".
Only using R# for few weeks now, I find myself addicted to have no yellow
in my sight, but this and var are killing me. This because there is no
option/smartness for it, and var, just because R# decided to follow after
populistic article instead of any OOD rationale.



0
Comment actions Permalink

How's that, just use memory profiler to see allocations? Use mono.cecil introspection to see if this is a value type? Read IL (ok, this is not a tool)?

I see the point anyway, dirty code that is actually clean :):)!

You say that if I intentionally introduced value type to call it in the loop:

for (int i = 0; i<100000; i++)
{
ValueType instance = new ValueType(i);
Console.WriteLine("Instance_" + i + ":" + instance) // That is what R# forces to do
}

Then you say I should not be afraid of doing the above as you say the performance impact is negligible compared to the complexity of String.Concat?
I don't see how complexity of String.Concat should justify me doing two extra allocations in this one line of code. Is the above "the clean code" in your opinion?

Edited by: Stanislav Dvoychenko on Sep 9, 2008 2:25 AM

Edited by: Stanislav Dvoychenko on Sep 9, 2008 2:41 AM

0

Please sign in to leave a comment.