What's the reasoning behind putting constants in if statements first?

前端 未结 8 2061
孤城傲影
孤城傲影 2021-02-03 16:53

I was looking at some example C++ code for a hardware interface I\'m working with and noticed a lot of statements along the following lines:

if ( NULL == pMsg )          


        
相关标签:
8条回答
  • 2021-02-03 17:40

    I forget the article, but the quote went something like: "Evidently its easier remembering to put the constant first, than it is remembering to use ==" ;))

    0 讨论(0)
  • 2021-02-03 17:45

    When the constant is first, the compiler will warn you if you accidentally write = rather than == since it's illegal to assign a value to a constant.

    0 讨论(0)
提交回复
热议问题