- 7 Tem 2013
- 8,221
- 4
- 709
well clean code principles are very important for create some projects they are a set of guidelines and best practices aimed at producing maintainable and efficient code. in this topic im going to show you these principles with examples in dart programming language
first you have to use descriptive names for variables and functions etc. its very important rule
rule -1
Kod:
//bad example
var a = 100;
//good example
var money = 100;
rule -2
each function or class should have a single responsibility, they should do one thing well for example
Kod:
//bad one
void userData(){
//code to handle user input, update UI, other things etc.
.. bla bla ITS BAD
//good example
void handleUserInput(){
//code to handle user input JUST FOR THIS
.... .....
void updateUI(){
//JUST CODE TO UPDATE UI
//good example
rule -3
do not repeat yourself you should avoid duplicating code basically
Kod:
//bad example
for text1
Textstyle: Colors.red,
for text2
Textstyle: Colors.red,
for text3
Textstyle: Colors.red,
//good example
for all textstyles : Theme.of(context).....textStyles.colors.red... etc
rule -4
code formatting is very important to read the code
Kod:
// Bad example
void calculateSum(){var a=5;var b=10;print(a+b);}
// Good examplevoid calculateSum() {
var a = 5;
var b = 10;
print(a + b);
}
well these rules can help you to code better I'll share other rules when I remember them
good luck
<3 Gauloran