Featured post
c# - SRP: Why use instance field values instead of parameters? -
i've read srp, easy 123…, , of resonates me except 1 paragraph, in section named "cohesion" (i've claimed before "get" cohesion, talk of parameters vs instance fields gives me pause...):
take class. @ methods. have parameters or using instance fields? if using parameters, remove them. make them instance fields. end methods use 1 of 5 instances? warning of low cohesion exists between method , class.
is removal of parameters merely temporary exercise reveal methods approaching static-ability (low cohesion), idea being return use of parameters when you're finished?
or preference instance fields on parameters actual design technique maintain high cohesion?
have somehow taken quote out of context?
crud real common approach interface based programming. take 2 concrete classes implement crud interface: employee , building.
now imagine how code being parameter based:
employee employeeobj = new employee(); building buildingobj = new building(); string firstname = "bob"; employeeobj.create(firstname);
what building?
buildingtypes buildingtype = buildingtypes.one; building.create(buildingtype);
woops...how supposed implement crud interface different parameters? create overloads? more interfaces? 2 params (firstname lastname)?
this ugly fast....because use parameters crud interface have more 1 reason change, decreases design's cohesion.
let's try using our objects/instance based params...
employee empobj = new employee(); empobj.firstname = "bob"; empobj.create(); building buildingobj = new building(); buildingobj.buildingtype = buildingtypes.one; buildingobj.create();
with simple crud , no params 1 can sprinkle in polymorphism:
someobj.create();
this leads encapsulated composition, decoupling, srp, etc...
- Get link
- X
- Other Apps
Comments
Post a Comment