Is it better coding practice to define variables outside a foreach even though more verbose?

前端 未结 12 2244
栀梦
栀梦 2020-12-10 10:39

In the following examples:

  • the first seems more verbose but less wasteful of resources
  • the second is less verbose bu
12条回答
  •  误落风尘
    2020-12-10 11:33

    I'm not sure what you gain by defining the string variable outside of the loop. Strings are immutable so they are not reused. Whenever you assign to them, a new instance is created.

提交回复
热议问题