I have a method like:
private static string AmpRemove(string str) { int index = str.IndexOf('&'); if (index > 0) str = str.Substring(0, index); return str; }
Here I am trying to get text from a string until the &
character is found. My elder changed this method to
private static string AmpRemove(string str) { if (str.IndexOf('&') > 0) str = str.Substring(0, str.IndexOf('&')); return str; }
Therefore, instead of storing index
he will calculate it twice, and his argument was that, since this method will be called in several threads, there may be an invalid value stored in index
.
My understanding of the stream is limited, but I believe that each stream will have its own stack, where the str
and index
parameters will be set. I tried to reason with him that this is reentrant code, and there is no way for multiple threads to modify a local variable in this method.
So my question is: do I believe that caching saving index
is the best solution, since it will not include calculating the index twice and since it is a local variable and str
is a parameter local to the method, there is no way for multiple threads to change / change str
and index
?
Is it correct?
source share