String.equals() argument ordering

后端 未结 10 1518
太阳男子
太阳男子 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条回答
  •  忘掉有多难
    2020-11-27 07:09

    There are several reasons not to do it like that, however in the end it depends on you (or the team working on your product) if you think this is bad coding style. Arguments against it are:

    • Strings are rarely null (and you shouldn't make APIs where they are because people don't expect it)
    • It feels weird to put the value you are comparing to first
    • Code style uniformity is important, because this way is the exception, you should only do it, if everyone in your team does it.

    As said I don't think these arguments are very strong, nor is the reason to do it like you. So it is mostly important to just agree on one way as your coding style and stick to that.

提交回复
热议问题