c# - returning an assignment expression -


while working on of older code in c#, encountered code irked me.

without ado, goes this:

private string foo(string _text) {    /* manipulation on _text */    return _text = server.htmldecode(_text); } 

it's last line irks me; i'm c background, , can understand code in effect trying return decoded _text variable. value of assignment operator left operand, can see it.

yet still find irksome.

is ordinate practice in c# need accustomed to?

to me last line should be

return server.htmldecode(_text); 

and not assignment expression. there deeper c# feature i'm not aware of?

while working on of older code in c#, encountered code irked me.

there numerous irksome problems here. let's list them all.

private string foo(string _text) {    /* manipulation on _text */    return _text = server.htmldecode(_text); } 

it's last line irks me

the comment irksome. local variables cheap. there no need erase original value of _text. instead, make new local variable , manipulate that. way, when debugging at point in method can know original argument was. (remember, original argument might eligible garbage collection moment variable overwritten, , therefore lost forever.)

do not write on formal parameter without reason. makes harder debug.

the value of assignment operator left operand, can see it.

that correct in case subtly wrong in general; value of assignment operator in c# value of right operand after being converted type associated left hand side. remember, left hand side might not have value; write-only property.

is ordinate practice in c# need accustomed to?

there standard practice here, yes. bizarre usage (1) variable chosen formal, , (2) assignment combined return.

it standard practice in c# say:

string decoded = server.htmldecode(_text); return decoded; 

now might wonder compelling benefit of on suggest:

return server.htmldecode(_text); 

the answer is: before visual studio 2013, there no facility in debugger examine returned value of method call! therefore if wanted see value returned htmldecode while debugging had following choices:

  • debug @ assembly level , @ contents of eax
  • step htmldecode , examine state
  • step out of current method , examine whatever return value assigned to
  • assign result otherwise useless local variable , examine local in debugger

since first 3 horrid , last 1 easy, that's many c# programmers got in habit of doing.

if , not use resulting local, c# compiler knows common practice , deliberately suppresses "you wrote local never read from" warning. gives warning if local had constant written it, in case knew @ compile time , don't typically need examine in debugger.

hopefully vs2013 supports frequently-requested feature, sort of pattern gradually disappear.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -