c# - what is better: passing args between functions or working with static objects? -
in clean code book there's opinion best practice work functions no args passed , from. question makes more sense, work functions pass args each other, or void functions share , manipulate static objects?
examples:
option 1.
public list<mything> functiona(list <mything> mythings){ mythings.add(someblah); return mythings; } public void functionb(){ list<mything> mythings = new list<mything>(); mythings = initiatethingsorwhatever(); list<mything> mychangedthings = functiona(mythings); }
option 2:
private static list<mything> _mythings = new list<mything>(); public static list<mything> mythings{ { return _mything; } set { _mything = value; } } public void somefunction(){ functiona(); functionb(); } public void functiona(){ mythings = yadayadastuff(); } public void functionb(){ var showmychangedthings = mythings; }
passing arguments better using shared static data.
i believe you've misunderstood (or read directly) recommendation. there no recommendation use static shared data instead of passing arguments in book knowledge.
the recommendation minimize number of arguments passed member functions. can transform "a lot of arguments" class , provide method no arguments execute original function.
sample of transforming arguments object:
int multiplysome(int arg1, int arg2, int arg3) { return arg1 * arg2 * arg3; }
can transformed object method takes less arguments:
class multiplysomeclass { public int first; public int second; int multiplyby(int arg3) { return first * second * arg3;} }
and call changed from:
var result = multiplysome(4,5,6); var multiplyhelper = new multiplysomeclass { first = 4, second = 5 }; var result = multiplyhelper.multiplyby(6);
Comments
Post a Comment