String.equals() argument ordering

后端 未结 10 1474
太阳男子
太阳男子 2020-11-27 06:56

I recently received a downvote for using the following in a recent answer:

String word = ...;
if (\"s\".equals(word) || \"y\".equals(word)

10条回答
  •  -上瘾入骨i
    2020-11-27 07:24

    Yoda conditions (i.e. putting a constant before a variable in a comparison) can be considered bad practice as it makes the line of code less comprehensible. In this specific case however I would state that using a Yoda condition makes the code more comprehensible as you don't have to put a extra null check in front of it.

提交回复
热议问题