Powershell “private” scope seems not useful at all

后端 未结 3 469
一向
一向 2020-11-27 07:45

I\'ve got the script below, from internet:

$private:a = 1
Function test  {
    \"variable a contains $a\"
    $a = 2
    \"variable a contains $a\"
}
test
         


        
3条回答
  •  春和景丽
    2020-11-27 08:17

    Good software design means minimized coupling (among other things). Within Powershell, that includes using private ON EVERY VARIABLE YOU CAN. If you want to make a value available in some subsequently called module, pass that information EXPLICITLY. There should be a very good EXCEPTION reason for not doing this, because each time you rely on implicit knowledge (e.g. the kind that happens in Powershell when you don't use private variables), you increase the chance something will go unexpectedly wrong later (maybe months later when the software has a lot more code in it).

提交回复
热议问题