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

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

    To stop you from writing:

     if ( pMsg = NULL ) return rv;
    

    by mistake. A good compiler will warn you about this however, so most people don't use the "constant first" way, as they find it difficult to read.

提交回复
热议问题