You should never use this.argument (formals) in constructors, what's the point of constructor arguments if you are going to assign it directly to properties?, what you should do in most cases is to use setters to validate the data passed to constructor, if you don't validate then using constructor arguments is a waste of time and just duplicated code:
classHuman{int_age;Human(intage){setAge(age);}voidsetAge(intvalue){//an age of 0 or less is invalid, 130 years or more is invalidif(value<=0||value>=130){//throw error }_age=value;}}
I wrote this quite a while back when I was just getting started with Dart and I have learnt quite a few things since then. Most of these articles are due for an update.
This is misleading. In domain/business entities yes (or other value object from this layer), but for example when you have db entity, you dont want validate anything here. It adds duplicit valiation for data, which were already validated and when you decide to change the validation rules, you have to change it on multiple places.
Another thing is that immutability is considered as good practice (again in domain/business layer; database entities can have some data related to performance improvements for example), that means you dont have any setter methods, just copyWith method, which calls constructor.. and in this case you can have you validation logic in constructor and there is no need for even private method. Sure you can split validation to multiple methods if the logic gets bigger.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You should never use this.argument (formals) in constructors, what's the point of constructor arguments if you are going to assign it directly to properties?, what you should do in most cases is to use setters to validate the data passed to constructor, if you don't validate then using constructor arguments is a waste of time and just duplicated code:
Hello. Thank you so much for the feedback.
I agree with you.
I wrote this quite a while back when I was just getting started with Dart and I have learnt quite a few things since then. Most of these articles are due for an update.
This is misleading. In domain/business entities yes (or other value object from this layer), but for example when you have db entity, you dont want validate anything here. It adds duplicit valiation for data, which were already validated and when you decide to change the validation rules, you have to change it on multiple places.
Another thing is that immutability is considered as good practice (again in domain/business layer; database entities can have some data related to performance improvements for example), that means you dont have any setter methods, just copyWith method, which calls constructor.. and in this case you can have you validation logic in constructor and there is no need for even private method. Sure you can split validation to multiple methods if the logic gets bigger.