Are static local variables bad practice?

前端 未结 2 367
遥遥无期
遥遥无期 2021-01-18 00:39

Related C++ question: Static local variables in methods a bad practice?

In VB.NET, when I want a simple counter or something that increments each time a method is ca

2条回答
  •  佛祖请我去吃肉
    2021-01-18 01:20

    Are static local variables bad practice?

    No. Static local variables differ in exactly one regard to non-local private variables: they have a smaller scope. Since you always want to keep scope as small as possible (= better encapsulation), local statics can be advantageous over private variables.

    On the flip-side, local static variables may be hard to initialise correctly. If a complex initialisation is required (for example, if you need to re-initialise a variable later on), local static variables may be unsuitable.

提交回复
热议问题