C
C#8mo 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æ
8mo ago
basically, method vs setter
Jimmacle
Jimmacle8mo ago
but that's not the main difference between these
Mayor McCheese
Mayor McCheese8mo 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æ
8mo 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 McCheese8mo ago
It depends, that's not really what I said; in pointed out they do two different things
fæ
8mo ago
Why isn't age++ considered tied to a behavior It seems semantically meaningful enough compared to grow()
JakenVeina
JakenVeina8mo 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
Jimmacle8mo 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
WEIRD FLEX
WEIRD FLEX8mo ago
you are comparing a data class (which could be anything: an entity, an event, a message) with a business logic class
JakenVeina
JakenVeina8mo ago
indeed
DΣX
DΣX8mo 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æ
8mo ago
okay thank you
Mayor McCheese
Mayor McCheese8mo 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
Accord8mo 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.