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

前端 未结 8 2059
孤城傲影
孤城傲影 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:31

    So that you don't mix comparison (==) with assignment (=).

    As you know, you can't assign to a constant. If you try, the compliler will give you an error.

    Basically, it's one of defensive programming techniques. To protect yourself from yourself.

提交回复
热议问题