Is there any benefit to returning the result of assigning a value to a local variable rather than the value directly?

前端 未结 6 1079
死守一世寂寞
死守一世寂寞 2021-01-17 09:01

I am doing a java code inspection. Here is a function (snippet):

String getValue() {
     String res;
     StringBuilder strBuilder = new StringBuilder();

         


        
6条回答
  •  臣服心动
    2021-01-17 09:53

    That sort of code can sometimes result from incomplete removal of debug artifacts:

    String getValue() {
    
         String res;
         StringBuilder bs = new StringBuilder();
         //
         // More code here that sets sb
    
         res = bs.toString();
         // Test and/or display res here
         return res; 
    }
    

    It certainly seems like a good candidate for the next round of refactoring and clean-up.

提交回复
热议问题