Calling equals on string literal

后端 未结 8 1295
再見小時候
再見小時候 2020-12-29 04:32

I just was tidying my code a bit and there was this piece:

String saving = getValue();
if(saving != null && saving.equals(\"true\")){
   // do someth         


        
8条回答
  •  渐次进展
    2020-12-29 05:08

    Sorry but I cannot agree on that issue.

    If you use the constant first you support structures that veil programming errors. And one should only produce code either

    1. to add functionality,
    2. fix an programming error
    3. or trying to support structures to avoid programming errors (like design patterns).

    On top you have a weak assertion about the variable. Following code is always forced to check for null values as well.

    If you have to deal with null values from other modules your task is to map them into proper representations in your domain model as early as possible.

    In your own code avoid null as parameter or return value, so there won't be any null-checks neccessary anymore, the suggested piece of code included.

    You see, this subject is more about avoiding null than accepting it to conquer your code.

提交回复
热议问题