C
C#15mo ago

❔ picking the right patterns

class Human{
public int age { /*custom get n set*/ }
}

Human h = new Human();
h.age++;
class Human{
public int age { /*custom get n set*/ }
}

Human h = new Human();
h.age++;
vs
class Human{
public int age { get; protected set; }
public grow() { age++; }
}

Human h = new Human();
h.grow();
class Human{
public int age { get; protected set; }
public grow() { age++; }
}

Human h = new Human();
h.grow();
14 Replies
fæ
OP15mo ago
basically, method vs setter
Jimmacle
Jimmacle15mo ago
but that's not the main difference between these
Mayor McCheese
Mayor McCheese15mo ago
Those are two different concepts; the first one you're capable of setting however you want, even negative numbers. The second is more closely controlled and tied to a behavior; grow always increments by one. They're not even mutually exclusive
fæ
OP15mo ago
so if my grow() method is ONLY concerned with incrementing the age, better to just use the setter? and if i had more things like aging, gaining height, etc, i use a method instead?
Mayor McCheese
Mayor McCheese15mo ago
It depends, that's not really what I said; in pointed out they do two different things
fæ
OP15mo ago
Why isn't age++ considered tied to a behavior It seems semantically meaningful enough compared to grow()
JakenVeina
JakenVeina15mo ago
he means that making it a publicly settable property isn't "tied to a behavior" cause making it a settable property allows one to do a LOT more with it than just ++
Jimmacle
Jimmacle15mo ago
^ by making the age setter protected and putting the operation inside the grow() method, that allows the class to control exactly what can happen to the age property the difference is between saying "you can increment age" and "you can set age to anything you want" to code outside of the class
Omnissiah
Omnissiah15mo ago
you are comparing a data class (which could be anything: an entity, an event, a message) with a business logic class
JakenVeina
JakenVeina15mo ago
indeed
DΣX
DΣX15mo ago
second one follows "tell, don't ask". it hides implementation details (like having the value in a property) behind a method interface and thus limits the complexity of the interface which in the end increases maintainability as you can change the internal implementation without breaking the interface. Good Thing! Second one is the winner of clean-code enthusiasts 😄
fæ
OP15mo ago
okay thank you
Mayor McCheese
Mayor McCheese15mo ago
It's not really a clean code issue
class HumanCommand {}
class GrowCommand { protected GrowCommand(int growth, TimeSpan duration); } }
class SingleYearGrowCommand : GrowCommand { SingleYearGrowCommand : base(1, TimeSpan.FromYears(1)); } }
class HumanCommand {}
class GrowCommand { protected GrowCommand(int growth, TimeSpan duration); } }
class SingleYearGrowCommand : GrowCommand { SingleYearGrowCommand : base(1, TimeSpan.FromYears(1)); } }
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server