vb.net - Instance variables and local variables confusion -
please take @ sample1 below:
public class localvariable public sub run() dim testvariable integer testvariable = method1(testvariable) testvariable = method2(testvariable) testvariable = method3(testvariable) end sub private function method1(byval x integer) integer return x + 1 end function private function method2(byval x integer) integer return x + 2 end function private function method3(byval x integer) integer return x + 3 end function end class
and sample 2 below:
public class instancevariable dim testvariable integer public sub run() method1() method2() method3() end sub private sub method1() testvariable = testvariable + 1 end sub private sub method2() testvariable = testvariable + 2 end sub private sub method3() testvariable = testvariable + 3 end sub end class
the outcome same after each program runs i.e. testvariable=6. every example find online , @ work uses sample 1. surely misuse of instance variable testvariable should shared across functions? therefore instance variable should used.
the 2 samples don't mean same thing.
the difference happens if call run() more once on life of program. run() method in sample 2 never resets testvariable, continue larger , larger. in sample 1, result 6 because testvariable new variable each call function. more correct depends entirely on you're trying do.
there third option
all else being equal, recommend sample 1 approach 2 options. however, instance vs local variable not distinction. there's no reason sample 1 couldn't use instance variable method definitions. our third option this:
public class instancevariablewithsampleonefunctions dim testvariable integer public sub run() testvariable = method1(testvariable) testvariable = method2(testvariable) testvariable = method3(testvariable) end sub private function method1(byval x integer) integer return x + 1 end function private function method2(byval x integer) integer return x + 2 end function private function method3(byval x integer) integer return x + 3 end function end class
this uses instance variable sample 2 methods sample 1. i'll call sample 3.
this cuts better heart of question, because sample 3 has same behavior sample 2. whether should choose 1 or 2 depends on behavior need. whether should choose 2 or 3 depends on merits of coding style. both 2 , 3 rely on instance variable in run() method, 2 uses instance variable in additional methods, while 3 uses local variable.
i can @ point, comparing 2 , 3, prefer sample 3. methods sample 3 have more of functional style: accept input, return output. gives them higher level of abstraction, makes easier refactor sample 3 things move methods elsewhere... say, utility class 1 set of methods can shared both samples 1 , 3. since mentioned threading, typically style makes easier, not harder, multi-threading correctly.
one concrete example how method style better it's composable. attribute allows me re-write sample 3's run() method , confident of getting same results:
public sub run() testvariable = method3(method2(method1(testvariable))) end sub
Comments
Post a Comment