All the code examples from my Clean Code series are here.
Clean Code Youtube Playlist Link Click Here
Clean Code Book reference: https://amzn.to/3BVggeJ
All the code examples from my Clean Code series are here.
Clean Code Youtube Playlist Link Click Here
Clean Code Book reference: https://amzn.to/3BVggeJ
// Choose meaningful names | |
// ❌ Bad: | |
void save() { } | |
// ✅ Good: | |
void saveCustomerIntoDatabase() { } |
// The Length of a Name | |
// Variables | |
List<int> studentsGrades = [9, 8, 15, 21]; | |
var gradesSum = 0; | |
// the index can be called just "i" because the scope is small (one line) | |
for (int i = 0; i < studentsGrades.length; i++) { | |
gradesSum = gradesSum + studentsGrades [i]; | |
} | |
// Functions or Classes Name | |
// For Functions and Classes it's the opposite, for smaller scopes the function | |
// name should be big (very explanatory) | |
void updateUsersAvatarColor() { } | |
// For larger scopes the Function or Class should have a smaller name | |
User getUser() { } |
// Choose searchable names | |
// ❌ Bad: | |
var h = 300; | |
// ✅ Good: | |
var eiffelTowerHeight = 300; |
// Avoid negative names | |
// ❌ Bad (negative word): | |
if (cartIsNotEmpty) { | |
// ... | |
} else { | |
// ... | |
} | |
// ✅ Good (positive word): | |
if (cartIsFull) { | |
// ... | |
} else { | |
// ... | |
} |
// Avoid negative statements | |
// ❌ Bad (negative if): | |
if (!cartIsFull) { | |
// ... | |
} else { | |
// ... | |
} | |
// ✅ Good (positive word): | |
if (cartIsFull) { | |
// ... | |
} else { | |
// ... | |
} |
// Don’t use prefixes | |
// Choose names you can pronounce | |
// ❌ Bad: | |
var dateDMY; | |
// ✅ Good: | |
var dateDayMonthYearFormat; |
// Be careful with optical illusions | |
// ❌ Bad: | |
var textFieldNameController; | |
var textFieldUsernameController; | |
// ✅ Good: | |
var nameTextFieldController; | |
var usernameTextFieldController; |
// Make meaningful distinction between names | |
// ❌ Bad: | |
class Product { } | |
class ProductData { } | |
class ProductInfo { } | |
// ✅ Good: | |
class Product { } // Only one class for Product |
// There are some types of comments that are acceptable, like: | |
// ✅ copyrights | |
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} |
// Acceptable comments | |
// ✅ informative comments explaining a pattern (for example regEx) | |
void main() { | |
final email = '[email protected]'; | |
// Checks: | |
// if the email has at and dot signs | |
// if it has characters before and after the at sign | |
// if it has characters after the dot sign | |
final regExp = RegExp(r"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"); | |
if (regExp.hasMatch(email)) { | |
print('Email válido!'); | |
} else { | |
print('Email inválido!'); | |
} | |
} |
// Acceptable comments | |
// ✅ warning of consequences | |
void main() { | |
// DON'T RUN THESE TESTS UNLESS YOU HAVE SOME TIME TO KILL | |
test ('test 1', () {}); | |
test ('test 2', () {}); | |
test ('test 3', () {}); | |
test ('test 4', () {}); | |
// [...] | |
test ('test 20', () {}); | |
test ('test 21', () {}); | |
} |
// Acceptable comments | |
// ✅ TODO comments, but try to not leave them when you leave the code, they are probably going to be forgotten | |
// TODO: Call the API to get this data |
// Bad types of comments | |
// ❌ Bad (Redundant comments): | |
// This is the main function | |
void main() { } | |
// ❌ Bad: | |
// Comments to version control (use git instead) | |
// ❌ Bad: | |
// Commented code | |
// ❌ Bad: | |
// Comments about a code that is somewhere else | |
// But more importantly: you should only use comments when you fail to make your code express itself. |
Thank for sharing 🤗 @LeBaleiro