Should Local Variable Initialisation Be Mandatory?

前端 未结 17 2454
忘掉有多难
忘掉有多难 2021-02-14 01:26

The maintenance problems that uninitialised locals cause (particularly pointers) will be obvious to anyone who has done a bit of c/c++ maintenance or enhancement, but I still se

17条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-14 02:02

    If you think that an initialization is redundant, it is. My goal is to write code that is as humanly readable as possible. Unnecessary initialization confuses future reader.

    C compilers are getting pretty good at catching usage of unitialized variables, so the danger of that is now minimal.

    Don't forget, by making "fake" initialization, you trade one danger - crashing on using garbage (which leads to a bug that is very easy to find and fix) on another - program taking wrong action based on fake value (which leads to a bug that is very difficult to find). The choice depends on the application. For some, it is critical never to crash. For majority, it is better to catch the bug ASAP.

提交回复
热议问题