String.Format refactoring suggestions.

A couple of suggestions for you!


1) When the same variable is repeated in the format string resharper should consolidate these instead of repeating them.

2) When you have a format string with a list of parameters and then delete or insert one resharper should handle the renumbering of the other parameter place holders automatically.

4 comments
Comment actions Permalink

Hello Martin,

A couple of suggestions for you!

1) When the same variable is repeated in the format string resharper
should consolidate these instead of repeating them.


Could you please clarify what you mean under 'consolidate'?


2) When you have a format string with a list of parameters and then
delete or insert one resharper should handle the renumbering of the
other parameter place holders automatically.


An example would be useful.


Regards,
Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0
Comment actions Permalink

POINT 1
+ int var1 = 1;
int var2 = 2;

string sTest = var1.ToString() + "xyz" + var2.ToString() + var1.ToString();+

When using resharper to convery this to String.Format it converts the code to the following
+
string sTest = string.Format("xyz", var1.ToString(), var2.ToString(), var1.ToString());+ I think it would be better to convert it to this + string sTest = string.Format("xyz", var1.ToString(), var2.ToString());+ POINT 2 I have the following code. + string.Format( "_csUserId = , _lkUserId = , _badLoginAttempts = , _lkGroupId = , _lkUserName = , _password = , _lkIsAdministrativeUser = ",
_csUserId, _lkUserId, _badLoginAttempts, _lkGroupId, _lkUserName, _password, _lkIsAdministrativeUser);+

If I no longer want to use the _csUserId variable I need to delete it from the parameter list then subtract one from all the place holders to take account of this.

This is quite tedious and seems to be crying out as a refactoring task.

0
Comment actions Permalink

Hello Martin,

it seems for me that what you're looking for is not easy.

1) Yes, there is a certain rationale in re-using the same expression in string.Format,
however, the expression being reused may not be free of side effects causing
the rewrite to be not
equivalent to the original code. Even ToString () may cause side effects
if overriden appropriately.
As ReSharper generally has no way to determine whether the expression has
side effects,
the optimization you're proposing is not safe.

2) The problem with such a refactoring is that ReSharper will need to detemine
somehow what part
of the format string should be deleted along with obsolete argument. It's
unclear how to do that for
a generic format string - there can be no natural delimiters like commas
in your sample.

Thanks,
Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

POINT 1
+ int var1 = 1;
int var2 = 2;
string sTest = var1.ToString() + "xyz" + var2.ToString() +
var1.ToString();+

When using resharper to convery this to String.Format it converts the
code to the following
+
string sTest = string.Format("xyz", var1.ToString(), > var2.ToString(), var1.ToString());+ > I think it would be better to convert it to this > + > string sTest = string.Format("xyz", var1.ToString(), > var2.ToString());+ > POINT 2 > > I have the following code. + > > string.Format( > "_csUserId = , _lkUserId = ,
_badLoginAttempts = , _lkGroupId = , _lkUserName = ,
_password = , _lkIsAdministrativeUser = ",
_csUserId, _lkUserId, _badLoginAttempts,
_lkGroupId, _lkUserName, _password, _lkIsAdministrativeUser);+
If I no longer want to use the _csUserId variable I need to delete it
from the parameter list then subtract one from all the place holders
to take account of this.

This is quite tedious and seems to be crying out as a refactoring
task.



0
Comment actions Permalink

I can see for point 1 there may be a few exceptional cases where the refactoring could make a difference.

On point 2 however it would seem relatively straight forward for resharper to detect that _csUserId, had been deleted from the parameter list and offer a refactor light bulb to adjust numberings as below

+"_csUserId = , _lkUserId = ,
_badLoginAttempts = , _lkGroupId = , _lkUserName = ,
_password = , _lkIsAdministrativeUser = "+

Obviously any further tidying up would need to be done by the programmer but it would save them some time

0

Please sign in to leave a comment.