- Introduction
- Naming
- Variables
- Functions
- Objects and Data Structures
- Classes
- SOLID
- Testing
- Concurrency
- Error Handling
- Formatting
- Comments
Software engineering principles, from Robert C. Martin's book Clean Code, adapted for .NET and .NET Core. This is not a style guide. It's a guide to producing readable, reusable, and refactorable software in .NET and .NET Core.
Not every principle herein has to be strictly followed, and even fewer will be universally agreed upon. These are guidelines and nothing more, but they are ones codified over many years of collective experience by the authors of Clean Code.
Inspired from clean-code-javascript and clean-code-php
Naming it hard and it takes time but worth it. Choosing good names takes time but saves more than it takes and it will help everyone who reads your code (including you) will be happier if you do. Naming should reflect about what it does, what is the context.
Bad:
int d;
Good:
int daySinceModification;
Programmers must avoid naming with disinformation name and we should name variable to reflect what we want to do with it.
Bad:
var dataFromDb = db.GetFromService().Tolist();
Good:
var listOfEmployee = _employeeService.GetEmployeeListFromDb().Tolist();
What happends if we cant pronoun variables, function, etc... It will take us a lot of time (some time make us like an idiot to discuss about it) to investigate what meaning of that variables, what is use.
Bad:
public class Employee
{
public Datetime sWorkDate { get; set; } // what the heck is this
public Datetime modTime { get; set; } // same here
}
Good:
public class Employee
{
public Datetime StartWorkingDate { get; set; }
public Datetime ModificationTime { get; set; }
}
Use Camelcase Notation for variable and parameter function
Bad:
var employeephone;
public double CalculateSalary(int workingdays, int workinghours)
{
// some logic
}
Good:
var employeePhone;
public double CalculateSalary(int workingDays, int workingHours)
{
// some logic
}
People who read your code is also programmers. So naming right will help everyone on the same page because we dont want to take time to explain for everyone what that variable for, what the function for. We can name the variable or function to reflect the pattern, algorithm names and so forth.
Good
public class SingleObject
{
//create an object of SingleObject
private static SingleObject _instance = new SingleObject();
//make the constructor private so that this class cannot be
//instantiated
private SingleObject() {}
//Get the only object available
public static SingleObject GetInstance()
{
return _instance;
}
public string ShowMessage()
{
return "Hello World!";
}
}
public static void main(String[] args)
{
// illegal construct
// var object = new SingleObject();
// Get the only object available
var singletonObject = SingleObject.GetInstance();
// show the message
singletonObject.ShowMessage();
}
Use meaningful and pronounceable variable names 📄
Bad:
var ymdstr = DateTime.UtcNow.ToString("MMMM dd, yyyy");
Good:
var currentDate = DateTime.UtcNow.ToString("MMMM dd, yyyy");
Use the same vocabulary for the same type of variable 📄
Bad:
GetUserInfo();
GetUserData();
GetUserRecord();
GetUserProfile();
Good:
GetUser();
Use searchable names (part 1) 📄
We will read more code than we will ever write. It's important that the code we do write is readable and searchable. By not naming variables that end up being meaningful for understanding our program, we hurt our readers. Make your names searchable.
Bad:
// What the heck is data for?
var data = new { Name = "John", Age = 42 };
var stream1 = new MemoryStream();
var ser1 = new DataContractJsonSerializer(typeof(object));
ser1.WriteObject(stream1, data);
stream1.Position = 0;
var sr1 = new StreamReader(stream1);
Console.Write("JSON form of Data object: ");
Console.WriteLine(sr1.ReadToEnd());
Good:
var person = new Person
{
Name = "John",
Age = 42
};
var stream2 = new MemoryStream();
var ser2 = new DataContractJsonSerializer(typeof(Person));
ser2.WriteObject(stream2, data);
stream2.Position = 0;
var sr2 = new StreamReader(stream2);
Console.Write("JSON form of Data object: ");
Console.WriteLine(sr2.ReadToEnd());
Use searchable names (part 2) 📄
Bad:
var data = new { Name = "John", Age = 42, PersonAccess = 4};
// What the heck is 4 for?
if (data.PersonAccess == 4)
{
// do edit ...
}
Good:
public enum PersonAccess : int
{
ACCESS_READ = 1,
ACCESS_CREATE = 2,
ACCESS_UPDATE = 4,
ACCESS_DELETE = 8
}
var person = new Person
{
Name = "John",
Age = 42,
PersonAccess= PersonAccess.ACCESS_CREATE
};
if (person.PersonAccess == PersonAccess.ACCESS_UPDATE)
{
// do edit ...
}
Use explanatory variables 📄
Bad:
const string Address = "One Infinite Loop, Cupertino 95014";
var cityZipCodeRegex = @"/^[^,\]+[,\\s]+(.+?)\s*(\d{5})?$/";
var matches = Regex.Matches(Address, cityZipCodeRegex);
if (matches[0].Success == true && matches[1].Success == true)
{
SaveCityZipCode(matches[0].Value, matches[1].Value);
}
Good:
Decrease dependence on regex by naming subpatterns.
const string Address = "One Infinite Loop, Cupertino 95014";
var cityZipCodeWithGroupRegex = @"/^[^,\]+[,\\s]+(?<city>.+?)\s*(?<zipCode>\d{5})?$/";
var matchesWithGroup = Regex.Match(Address, cityZipCodeWithGroupRegex);
var cityGroup = matchesWithGroup.Groups["city"];
var zipCodeGroup = matchesWithGroup.Groups["zipCode"];
if(cityGroup.Success == true && zipCodeGroup.Success == true)
{
SaveCityZipCode(cityGroup.Value, zipCodeGroup.Value);
}
Avoid nesting too deeply and return early 📄
Too many if else statemetns can make your code hard to follow. Explicit is better than implicit.
Bad:
public bool IsShopOpen(string day)
{
if (string.IsNullOrEmpty(day))
{
day = day.ToLower();
if (day == "friday")
{
return true;
}
else if (day == "saturday")
{
return true;
}
else if (day == "sunday")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Good:
public bool IsShopOpen(string day)
{
if (string.IsNullOrEmpty(day))
{
return false;
}
var openingDays = new string[] {
"friday", "saturday", "sunday"
};
return openingDays.Any(d => d == day.ToLower());
}
Bad:
public long Fibonacci(int n)
{
if (n < 50)
{
if (n != 0)
{
if (n != 1)
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
else
{
return 1;
}
}
else
{
return 0;
}
}
else
{
throw new System.Exception("Not supported");
}
}
Good:
public long Fibonacci(int n)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
if (n > 50)
{
throw new System.Exception("Not supported");
}
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
Avoid Mental Mapping 📄
Don’t force the reader of your code to translate what the variable means. Explicit is better than implicit.
Bad:
var l = new[] { "Austin", "New York", "San Francisco" };
for (var i = 0; i < l.Count(); i++)
{
var li = l[i];
DoStuff();
DoSomeOtherStuff();
// ...
// ...
// ...
// Wait, what is `li` for again?
Dispatch(li);
}
Good:
var locations = new[] { "Austin", "New York", "San Francisco" };
foreach (var location in locations)
{
DoStuff();
DoSomeOtherStuff();
// ...
// ...
// ...
Dispatch(location);
}
Don't add unneeded context 📄
If your class/object name tells you something, don't repeat that in your variable name.
Bad:
public class Car
{
public string CarMake { get; set; }
public string CarModel { get; set; }
public string CarColor { get; set; }
//...
}
Good:
public class Car
{
public string Make { get; set; }
public string Model { get; set; }
public string Color { get; set; }
//...
}
Use default arguments instead of short circuiting or conditionals 📄
Not good:
This is not good because breweryName
can be NULL
.
This opinion is more understandable than the previous version, but it better controls the value of the variable.
public void CreateMicrobrewery(string name = null)
{
var breweryName = !string.IsNullOrEmpty(name) ? name : "Hipster Brew Co.";
// ...
}
Good:
public void CreateMicrobrewery(string breweryName = "Hipster Brew Co.")
{
// ...
}
Magic strings are string values that are specified directly within application code that have an impact on the application’s behavior. Frequently, such strings will end up being duplicated within the system, and since they cannot automatically be updated using refactoring tools, they become a common source of bugs when changes are made to some strings but not others.
Bad
if(userRole == "Admin")
{
// logic in here
}
Good
string ADMIN_ROLE = "Admin"
if(userRole == ADMIN_ROLE)
{
// logic in here
}
Using this we only have to change in centralize place and others will adapt it.
Limiting the amount of function parameters is incredibly important because it makes testing your function easier. Having more than three leads to a combinatorial explosion where you have to test tons of different cases with each separate argument.
Zero arguments is the ideal case. One or two arguments is ok, and three should be avoided. Anything more than that should be consolidated. Usually, if you have more than two arguments then your function is trying to do too much. In cases where it's not, most of the time a higher-level object will suffice as an argument.
Bad:
public void CreateMenu(string title, string body, string buttonText, bool cancellable)
{
// ...
}
Good:
pubic class MenuConfig
{
public string Title { get; set; }
public string Body { get; set; }
public string ButtonText { get; set; }
public bool Cancellable { get; set; }
}
var config = new MenuConfig();
config.Title = "Foo";
config.Body = "Bar";
config.ButtonText = "Baz";
config.Cancellable = true;
public void CreateMenu(MenuConfig config)
{
// ...
}
This is by far the most important rule in software engineering. When functions do more than one thing, they are harder to compose, test, and reason about. When you can isolate a function to just one action, they can be refactored easily and your code will read much cleaner. If you take nothing else away from this guide other than this, you'll be ahead of many developers.
Bad:
public void SendEmailToListOfClients(string[] clients)
{
foreach (var string client in clients) {
clientRecord = db.Find(client);
if (clientRecord.IsActive()) {
Email(client);
}
}
}
Good:
public void SendEmailToListOfClients(string[] clients)
{
var activeClients = ActiveClients(clients);
// Do some logic
}
public List<Client> ActiveClients(string[] clients)
{
return IsClientActive(clients);
}
public List<Client> IsClientActive(string client)
{
var clientRecord = db.Find(client).Where(s => s.Status = "Active");
return clientRecord;
}
Bad:
public class Email
{
//...
public void Handle()
{
SendMail(this._to, this._subject, this._body);
}
}
var message = new Email(...);
// What is this? A handle for the message? Are we writing to a file now?
message.Handle();
Good:
public class Email
{
//...
public void Send()
{
SendMail(this._to, this._subject, this._body);
}
}
var message = new Email(...);
// Clear and obvious
message.Send();
When you have more than one level of abstraction your function is usually doing too much. Splitting up functions leads to reusability and easier testing.
Bad:
public string ParseBetterJSAlternative(string code)
{
var regexes = [
// ...
];
var statements = explode(" ", code);
var tokens = [];
foreach (var regex in regexes) {
foreach (var statement in statements) {
// ...
}
}
var ast = [];
foreach (var token in tokens) {
// lex...
}
foreach (var node in ast) {
// parse...
}
}
Bad too:
We have carried out some of the functionality, but the ParseBetterJSAlternative()
function is still very complex and not testable.
public string Tokenize(string code)
{
var regexes = [
// ...
];
var statements = explode(" ", code);
var tokens = [];
foreach (var regex in regexes) {
foreach (var statement in statements) {
tokens[] = /* ... */;
}
}
return tokens;
}
public string Lexer(string[] tokens)
{
var ast = [];
foreach (var token in tokens) {
ast[] = /* ... */;
}
return ast;
}
public string ParseBetterJSAlternative(string code)
{
var tokens = Tokenize(code);
var ast = Lexer(tokens);
foreach (var node in ast) {
// parse...
}
}
Good:
The best solution is move out the dependencies of ParseBetterJSAlternative()
function.
class Tokenizer
{
public string Tokenize(string code)
{
var regexes = [
// ...
];
var statements = explode(" ", code);
var tokens = [];
foreach (var regex in regexes) {
foreach (var statement in statements) {
tokens[] = /* ... */;
}
}
return tokens;
}
}
class Lexer
{
public string Lexify(string[] tokens)
{
var ast = [];
foreach (var token in tokens) {
ast[] = /* ... */;
}
return ast;
}
}
class BetterJSAlternative
{
private string _tokenizer;
private string _lexer;
public BetterJSAlternative(Tokenizer tokenizer, Lexer lexer)
{
_tokenizer = tokenizer;
_lexer = lexer;
}
public string Parse(string code)
{
var tokens = _tokenizer->Tokenize(code);
var ast = _lexer.Lexify(tokens);
foreach (var node in ast) {
// parse...
}
}
}
Flags tell your user that this function does more than one thing. Functions should do one thing. Split out your functions if they are following different code paths based on a boolean.
Bad:
public void CreateFile(string name, bool temp = false)
{
if (temp) {
Touch("./temp/" + name);
} else {
Touch(name);
}
}
Good:
public void CreateFile(string name)
{
Touch(name);
}
public void CreateTempFile(string name)
{
Touch("./temp/" + name);
}
A function produces a side effect if it does anything other than take a value in and return another value or values. A side effect could be writing to a file, modifying some global variable, or accidentally wiring all your money to a stranger.
Now, you do need to have side effects in a program on occasion. Like the previous example, you might need to write to a file. What you want to do is to centralize where you are doing this. Don't have several functions and classes that write to a particular file. Have one service that does it. One and only one.
The main point is to avoid common pitfalls like sharing state between objects without any structure, using mutable data types that can be written to by anything, and not centralizing where your side effects occur. If you can do this, you will be happier than the vast majority of other programmers.
Bad:
// Global variable referenced by following function.
// If we had another function that used this name, now it'd be an array and it could break it.
var name = 'Ryan McDermott';
public string SplitIntoFirstAndLastName()
{
return name.Split(" ");
}
SplitIntoFirstAndLastName();
Console.PrintLine(name); // ['Ryan', 'McDermott'];
Good:
public string SplitIntoFirstAndLastName(string name)
{
return name.Split(" ");
}
var name = 'Ryan McDermott';
var newName = SplitIntoFirstAndLastName(name);
Console.PrintLine(name); // 'Ryan McDermott';
Console.PrintLine(newName); // ['Ryan', 'McDermott'];
Polluting globals is a bad practice in many languages because you could clash with another
library and the user of your API would be none-the-wiser until they get an exception in
production. Let's think about an example: what if you wanted to have configuration array.
You could write global function like Config()
, but it could clash with another library
that tried to do the same thing.
Bad:
public string[] Config()
{
return [
"foo" => "bar",
]
}
Good:
class Configuration
{
private string[] _configuration = [];
public Configuration(string[] configuration)
{
_configuration = configuration;
}
public string[] Get(string key)
{
return (_configuration[key]!= null) ? _configuration[key] : null;
}
}
Load configuration and create instance of Configuration
class
var configuration = new Configuration([
"foo" => "bar",
]);
And now you must use instance of Configuration
in your application.
Singleton is an anti-pattern. Paraphrased from Brian Button:
- They are generally used as a global instance, why is that so bad? Because you hide the dependencies of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a code smell.
- They violate the single responsibility principle: by virtue of the fact that they control their own creation and lifecycle.
- They inherently cause code to be tightly coupled. This makes faking them out under test rather difficult in many cases.
- They carry state around for the lifetime of the application. Another hit to testing since you can end up with a situation where tests need to be ordered which is a big no for unit tests. Why? Because each unit test should be independent from the other.
There is also very good thoughts by Misko Hevery about the root of problem.
Bad:
class DBConnection
{
private static DBConnection _instance;
private DBConnection($dsn)
{
// ...
}
public static GetInstance()
{
if (_instance == null) {
_instance = new DBConnection();
}
return _instance;
}
// ...
}
var singleton = DBConnection.GetInstance();
Good:
class DBConnection
{
public DBConnection(IOptions<DbConnectionOption> options)
{
// ...
}
// ...
}
Create instance of DBConnection
class and configure it with Option pattern.
var options = <resolve from IOC>;
var connection = new DBConnection(options);
And now you must use instance of DBConnection
in your application.
Bad:
if (article.state == "published") {
// ...
}
Good:
if (article.IsPublished()) {
// ...
}
Bad:
public bool IsDOMNodeNotPresent(string node)
{
// ...
}
if (!IsDOMNodeNotPresent(node))
{
// ...
}
Good:
public bool IsDOMNodePresent(string node)
{
// ...
}
if (IsDOMNodePresent(node)) {
// ...
}
This seems like an impossible task. Upon first hearing this, most people say,
"how am I supposed to do anything without an if
statement?" The answer is that
you can use polymorphism to achieve the same task in many cases. The second
question is usually, "well that's great but why would I want to do that?" The
answer is a previous clean code concept we learned: a function should only do
one thing. When you have classes and functions that have if
statements, you
are telling your user that your function does more than one thing. Remember,
just do one thing.
Bad:
class Airplane
{
// ...
public double GetCruisingAltitude()
{
switch (_type) {
case '777':
return GetMaxAltitude() - GetPassengerCount();
case 'Air Force One':
return GetMaxAltitude();
case 'Cessna':
return GetMaxAltitude() - GetFuelExpenditure();
}
}
}
Good:
interface IAirplane
{
// ...
public double GetCruisingAltitude();
}
class Boeing777 : IAirplane
{
// ...
public double GetCruisingAltitude()
{
return GetMaxAltitude() - GetPassengerCount();
}
}
class AirForceOne : IAirplane
{
// ...
public double GetCruisingAltitude()
{
return GetMaxAltitude();
}
}
class Cessna : IAirplane
{
// ...
public double GetCruisingAltitude()
{
return GetMaxAltitude() - GetFuelExpenditure();
}
}
Bad:
public Path TravelToTexas(object vehicle)
{
if (vehicle.GetType() == typeof(Bicycle))
{
(vehicle as Bicycle).PeddleTo(new Location("texas"));
}
else if (vehicle.GetType() == typeof(Car))
{
(vehicle as Car).DriveTo(new Location("texas"));
}
}
Good:
public Path TravelToTexas(Traveler vehicle)
{
vehicle.TravelTo(new Location("texas"));
}
or
// pattern matching
public Path TravelToTexas(object vehicle)
{
if (vehicle is Bicycle bicycle)
{
bicycle.PeddleTo(new Location("texas"));
}
else if (vehicle is Car car)
{
car.DriveTo(new Location("texas"));
}
}
Bad:
public int Combine(dynamic val1, dynamic val2)
{
int value;
if (!int.TryParse(val1, out value) || !int.TryParse(val2, out value)) {
throw new Exception('Must be of type Number');
}
return val1 + val2;
}
Good:
public int Combine(int val1, int val2)
{
return val1 + val2;
}
Dead code is just as bad as duplicate code. There's no reason to keep it in your codebase. If it's not being called, get rid of it! It will still be safe in your version history if you still need it.
Bad:
public void OldRequestModule(string url)
{
// ...
}
public void NewRequestModule(string url)
{
// ...
}
var request = NewRequestModule(requestUrl);
InventoryTracker("apples", request, "www.inventory-awesome.io");
Good:
public void RequestModule(string url)
{
// ...
}
var request = RequestModule(requestUrl);
InventoryTracker("apples", request, "www.inventory-awesome.io");
In C# / VB.NET you can set public
, protected
and private
keywords for methods.
Using it, you can control properties modification on an object.
- When you want to do more beyond getting an object property, you don't have to look up and change every accessor in your codebase.
- Makes adding validation simple when doing a
set
. - Encapsulates the internal representation.
- Easy to add logging and error handling when getting and setting.
- Inheriting this class, you can override default functionality.
- You can lazy load your object's properties, let's say getting it from a server.
Additionally, this is part of Open/Closed principle, from object-oriented design principles.
Bad:
class BankAccount
{
public double Balance = 1000;
}
var bankAccount = new BankAccount();
// Fake buy shoes...
bankAccount.Balance -= 100;
Good:
class BankAccount
{
private double _balance = 0.0D;
public BankAccount(balance = 1000)
{
_balance = balance;
}
public double WithdrawBalance(int amount)
{
if (amount > _balance) {
throw new \Exception('Amount greater than available balance.');
}
_balance -= amount;
}
public void DepositBalance(int amount)
{
_balance += amount;
}
public double getBalance()
{
return _balance;
}
}
var bankAccount = new BankAccount();
// Buy shoes...
bankAccount.WithdrawBalance(price);
// Get balance
balance = bankAccount.GetBalance();
Bad:
class Employee
{
public string Name { get; set; }
public Employee(name)
{
Name = name;
}
}
var employee = new Employee('John Doe');
Console.WriteLine(employee.Name) // Employee name: John Doe
Good:
class Employee
{
public string Name { get; }
public Employee(string name)
{
Name = name;
}
}
var employee = new Employee('John Doe');
Console.WriteLine(employee.GetName());// Employee name: John Doe
This pattern is very useful and commonly used in many libraries. It allows your code to be expressive, and less verbose. For that reason, use method chaining and take a look at how clean your code will be.
Good:
public static class ListExtensions
{
public static List<T> FluentAdd<T>(this List<T> list, T item)
{
list.Add(item);
return list;
}
public static List<T> FluentClear<T>(this List<T> list)
{
list.Clear();
return list;
}
public static List<T> FluentForEach<T>(this List<T> list, Action<T> action)
{
list.ForEach(action);
return list;
}
public static List<T> FluentInsert<T>(this List<T> list, int index, T item)
{
list.Insert(index, item);
return list;
}
public static List<T> FluentRemoveAt<T>(this List<T> list, int index)
{
list.RemoveAt(index);
return list;
}
public static List<T> FluentReverse<T>(this List<T> list)
{
list.Reverse();
return list;
}
}
internal static void ListFluentExtensions()
{
List<int> list = new List<int>() { 1, 2, 3, 4, 5 }
.FluentAdd(1)
.FluentInsert(0, 0)
.FluentRemoveAt(1)
.FluentReverse()
.FluentForEach(value => value.WriteLine())
.FluentClear();
}
As stated famously in Design Patterns by the Gang of Four, you should prefer composition over inheritance where you can. There are lots of good reasons to use inheritance and lots of good reasons to use composition. The main point for this maxim is that if your mind instinctively goes for inheritance, try to think if composition could model your problem better. In some cases it can.
You might be wondering then, "when should I use inheritance?" It depends on your problem at hand, but this is a decent list of when inheritance makes more sense than composition:
- Your inheritance represents an "is-a" relationship and not a "has-a" relationship (Human->Animal vs. User->UserDetails).
- You can reuse code from the base classes (Humans can move like all animals).
- You want to make global changes to derived classes by changing a base class. (Change the caloric expenditure of all animals when they move).
Bad:
class Employee
{
private string Name { get; set; }
private string Email { get; set; }
public Employee(string name, string email)
{
Name = name;
Email = email;
}
// ...
}
// Bad because Employees "have" tax data.
// EmployeeTaxData is not a type of Employee
class EmployeeTaxData extends Employee
{
private string Name { get; }
private string Email { get; }
public EmployeeTaxData(string name, string email, string ssn, string salary)
{
// ...
}
// ...
}
Good:
class EmployeeTaxData
{
public string Ssn { get; }
public string Salary { get; }
public EmployeeTaxData(string ssn, string salary)
{
Ssn = ssn;
Salary = salary;
}
// ...
}
class Employee
{
public string Name { get; }
public string Email { get; }
public string TaxData { get; }
public Employee(string name, string email)
{
Name = name;
Email = email;
}
public void SetTax(string ssn, double salary)
{
TaxData = new EmployeeTaxData(ssn, salary);
}
// ...
}
SOLID is the mnemonic acronym introduced by Michael Feathers for the first five principles named by Robert Martin, which meant five basic principles of object-oriented programming and design.
- S: Single Responsibility Principle (SRP)
- O: Open/Closed Principle (OCP)
- L: Liskov Substitution Principle (LSP)
- I: Interface Segregation Principle (ISP)
- D: Dependency Inversion Principle (DIP)
As stated in Clean Code, "There should never be more than one reason for a class to change". It's tempting to jam-pack a class with a lot of functionality, like when you can only take one suitcase on your flight. The issue with this is that your class won't be conceptually cohesive and it will give it many reasons to change. Minimizing the amount of times you need to change a class is important. It's important because if too much functionality is in one class and you modify a piece of it, it can be difficult to understand how that will affect other dependent modules in your codebase.
Bad:
class UserSettings
{
private User User;
public UserSettings (User user)
{
User = user;
}
public void ChangeSettings(Settings settings)
{
if (verifyCredentials()) {
// ...
}
}
private bool VerifyCredentials()
{
// ...
}
}
Good:
class UserAuth
{
private User User;
public UserSettings (User user)
{
User = user;
}
public bool VerifyCredentials()
{
// ...
}
}
class UserSettings
{
private User User;
private UserAuth Auth;
public UserSettings(User user)
{
User = user;
Auth = new UserAuth(user);
}
public function changeSettings(Settings settings)
{
if (Auth.VerifyCredentials()) {
// ...
}
}
}
As stated by Bertrand Meyer, "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification." What does that mean though? This principle basically states that you should allow users to add new functionalities without changing existing code.
Bad:
abstract class AdapterBase
{
protected string Name;
public string GetName()
{
return Name;
}
}
class AjaxAdapter : AdapterBase
{
public AjaxAdapter()
{
Name = 'ajaxAdapter';
}
}
class NodeAdapter : AdapterBase
{
public NodeAdapter()
{
Name = 'nodeAdapter';
}
}
class HttpRequester : AdapterBase
{
private AdapterBase Adapter;
public HttpRequester(AdapterBase adapter)
{
Adapter = adapter;
}
public void Fetch(string url)
{
var adapterName = Adapter.GetName();
if (adapterName === 'ajaxAdapter') {
return MakeAjaxCall(url);
} elseif (adapterName === 'httpNodeAdapter') {
return MakeHttpCall(url);
}
}
private bool MakeAjaxCall(string url)
{
// request and return promise
}
private bool MakeHttpCall(string url)
{
// request and return promise
}
}
Good:
interface IAdapter
{
bool Request(string url);
}
class AjaxAdapter : IAdapter
{
public bool Request(string url)
{
// request and return promise
}
}
class NodeAdapter : IAdapter
{
public bool Request(string url)
{
// request and return promise
}
}
class HttpRequester
{
private IAdapter Adapter;
public HttpRequester(IAdapter adapter)
{
Adapter = adapter;
}
public bool Fetch(string url)
{
return Adapter.Request(url);
}
}
This is a scary term for a very simple concept. It's formally defined as "If S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.)." That's an even scarier definition.
The best explanation for this is if you have a parent class and a child class, then the base class and child class can be used interchangeably without getting incorrect results. This might still be confusing, so let's take a look at the classic Square-Rectangle example. Mathematically, a square is a rectangle, but if you model it using the "is-a" relationship via inheritance, you quickly get into trouble.
Bad:
class Rectangle
{
protected double Width = 0;
protected double Height = 0;
public Drawable Render(double area)
{
// ...
}
public void SetWidth(double width)
{
Width = width;
}
public void SetHeight(double height)
{
Height = height;
}
public double GetArea()
{
return Width * Height;
}
}
class Square : Rectangle
{
public double SetWidth(double width)
{
Width = Height = Width;
}
public double SetHeight(double height)
{
Width = Height = Height;
}
}
Drawable RenderLargeRectangles(Rectangle rectangles)
{
foreach (rectangle in rectangles) {
rectangle.SetWidth(4);
rectangle.SetHeight(5);
var area = rectangle.GetArea(); // BAD: Will return 25 for Square. Should be 20.
rectangle.Render(area);
}
}
var rectangles = [new Rectangle(), new Rectangle(), new Square()];
RenderLargeRectangles(rectangles);
Good:
abstract class Shape
{
protected double Width = 0;
protected double Height = 0;
abstract public function getArea();
public Drawable Render(double area)
{
// ...
}
}
class Rectangle : Shape
{
public void SetWidth(double width)
{
Width = width;
}
public void SetHeight(double height)
{
Height = height;
}
public double GetArea()
{
return Width * Height;
}
}
class Square : Shape
{
private double Length = 0;
public double SetLength(double length)
{
Length = length;
}
public double GetArea()
{
return Math.Pow(Length, 2);
}
}
Drawable RenderLargeRectangles(Rectangle rectangles)
{
foreach (rectangle in rectangles) {
if (rectangle instanceof Square) {
rectangle.SetLength(5);
} elseif (rectangle instanceof Rectangle) {
rectangle.SetWidth(4);
rectangle.SetHeight(5);
}
var area = rectangle.GetArea();
rectangle.Render(area);
}
}
var shapes = [new Rectangle(), new Rectangle(), new Square()];
RenderLargeRectangles(shapes);
ISP states that "Clients should not be forced to depend upon interfaces that they do not use."
A good example to look at that demonstrates this principle is for classes that require large settings objects. Not requiring clients to setup huge amounts of options is beneficial, because most of the time they won't need all of the settings. Making them optional helps prevent having a "fat interface".
Bad:
public interface IEmployee
{
void Work();
void Eat();
}
public class Human : IEmployee
{
public void Work()
{
// ....working
}
public void Eat()
{
// ...... eating in lunch break
}
}
public class Robot : IEmployee
{
public void Work()
{
//.... working much more
}
public void Eat()
{
//.... robot can't eat, but it must implement this method
}
}
Good:
Not every worker is an employee, but every employee is an worker.
public interface IWorkable
{
void Work();
}
public interface IFeedable
{
void Eat();
}
public interface IEmployee : IFeedable, IWorkable
{
}
public class Human : IEmployee
{
public void Work()
{
// ....working
}
public void Eat()
{
//.... eating in lunch break
}
}
// robot can only work
public class Robot : IWorkable
{
public void Work()
{
// ....working
}
}
This principle states two essential things:
- High-level modules should not depend on low-level modules. Both should depend on abstractions.
- Abstractions should not depend upon details. Details should depend on abstractions.
This can be hard to understand at first, but if you've worked with PHP frameworks (like Symfony), you've seen an implementation of this principle in the form of Dependency Injection (DI). While they are not identical concepts, DIP keeps high-level modules from knowing the details of its low-level modules and setting them up. It can accomplish this through DI. A huge benefit of this is that it reduces the coupling between modules. Coupling is a very bad development pattern because it makes your code hard to refactor.
Bad:
public abstract class Employee
{
public void Work()
{
// ....working
}
}
public class Robot : Employee
{
public void Work()
{
//.... working much more
}
}
public class Manager
{
private Employee Employee;
public Manager(Employee employee)
{
Employee = employee;
}
public void Manage()
{
Employee.Work();
}
}
Good:
public interface Employee
{
void Work();
}
public class Human : Employee
{
public void Work()
{
// ....working
}
}
public class Robot : Employee
{
public void Work()
{
//.... working much more
}
}
public class Manager
{
private Employee Employee;
public Manager(Employee employee)
{
Employee = employee;
}
public void Manage()
{
Employee.Work();
}
}
Try to observe the DRY principle.
Do your absolute best to avoid duplicate code. Duplicate code is bad because it means that there's more than one place to alter something if you need to change some logic.
Imagine if you run a restaurant and you keep track of your inventory: all your tomatoes, onions, garlic, spices, etc. If you have multiple lists that you keep this on, then all have to be updated when you serve a dish with tomatoes in them. If you only have one list, there's only one place to update!
Oftentimes you have duplicate code because you have two or more slightly different things, that share a lot in common, but their differences force you to have two or more separate functions that do much of the same things. Removing duplicate code means creating an abstraction that can handle this set of different things with just one function/module/class.
Getting the abstraction right is critical, that's why you should follow the SOLID principles laid out in the Classes section. Bad abstractions can be worse than duplicate code, so be careful! Having said this, if you can make a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself updating multiple places anytime you want to change one thing.
Bad:
public List<EmployeeData> ShowDeveloperList(Developers developers)
{
foreach (var developers in developer) {
var expectedSalary = developer.CalculateExpectedSalary();
var experience = developer.GetExperience();
var githubLink = developer.GetGithubLink();
var data = {
expectedSalary,
experience,
githubLink
};
render(data);
}
}
public List<ManagerData> ShowManagerList(Manager managers)
{
foreach (var manager in managers) {
var expectedSalary = manager.CalculateExpectedSalary();
var experience = manager.GetExperience();
var githubLink = manager.GetGithubLink();
var data = {
expectedSalary,
experience,
githubLink
};
render(data);
}
}
Good:
public List<EmployeeData> ShowList(Employee employees)
{
foreach (var employee in employees) {
var expectedSalary = employees.CalculateExpectedSalary();
var experience = employees.GetExperience();
var githubLink = employees.GetGithubLink();
var data = {
expectedSalary,
experience,
githubLink
};
render(data);
}
}
Very good:
It is better to use a compact version of the code.
public List<EmployeeData> ShowList(Employee employees)
{
foreach (var employee in employees) {
render([
employee.CalculateExpectedSalary(),
employee.GetExperience(),
employee.GetGithubLink()
]);
}
}
Testing is more important than shipping. If you have no tests or an inadequate amount, then every time you ship code you won't be sure that you didn't break anything. Deciding on what constitutes an adequate amount is up to your team, but having 100% coverage (all statements and branches) is how you achieve very high confidence and developer peace of mind. This means that in addition to having a great testing framework, you also need to use a good coverage tool.
There's no excuse to not write tests. There's plenty of good JS test frameworks, so find one that your team prefers. When you find one that works for your team, then aim to always write tests for every new feature/module you introduce. If your preferred method is Test Driven Development (TDD), that is great, but the main point is to just make sure you are reaching your coverage goals before launching any feature, or refactoring an existing one.
Bad:
import assert from 'assert';
describe('MakeMomentJSGreatAgain', () => {
it('handles date boundaries', () => {
let date;
date = new MakeMomentJSGreatAgain('1/1/2015');
date.addDays(30);
assert.equal('1/31/2015', date);
date = new MakeMomentJSGreatAgain('2/1/2016');
date.addDays(28);
assert.equal('02/29/2016', date);
date = new MakeMomentJSGreatAgain('2/1/2015');
date.addDays(28);
assert.equal('03/01/2015', date);
});
});
Good:
import assert from 'assert';
describe('MakeMomentJSGreatAgain', () => {
it('handles 30-day months', () => {
const date = new MakeMomentJSGreatAgain('1/1/2015');
date.addDays(30);
assert.equal('1/31/2015', date);
});
it('handles leap year', () => {
const date = new MakeMomentJSGreatAgain('2/1/2016');
date.addDays(28);
assert.equal('02/29/2016', date);
});
it('handles non-leap year', () => {
const date = new MakeMomentJSGreatAgain('2/1/2015');
date.addDays(28);
assert.equal('03/01/2015', date);
});
});
Use Action/Func, not delegation
Callbacks aren't clean, and they cause excessive amounts of nesting. With .NET latest versions use Action/Func keywords. Use them!
Bad:
import { get } from 'request';
import { writeFile } from 'fs';
get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', (requestErr, response) => {
if (requestErr) {
console.error(requestErr);
} else {
writeFile('article.html', response.body, (writeErr) => {
if (writeErr) {
console.error(writeErr);
} else {
console.log('File written');
}
});
}
});
Good:
import { get } from 'request';
import { writeFile } from 'fs';
get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin')
.then((response) => {
return writeFile('article.html', response);
})
.then(() => {
console.log('File written');
})
.catch((err) => {
console.error(err);
});
Name | Description | Exceptions |
---|---|---|
Avoid async void | Prefer async Task methods over async void methods | Event handlers |
Async all the way | Don't mix blocking and async code | Console main method (C# <= 7.0) |
Configure context | Use ConfigureAwait(false) when you can |
Methods that require context |
To Do This ... | Instead of This ... | Use This |
---|---|---|
Retrieve the result of a background task | Task.Wait or Task.Result |
await |
Wait for any task to complete | Task.WaitAny |
await Task.WhenAny |
Retrieve the results of multiple tasks | Task.WaitAll |
await Task.WhenAll |
Wait a period of time | Thread.Sleep |
await Task.Delay |
There's a lot to learn about async and await, and it's natural to get a little disoriented. Here's a quick reference of solutions to common problems.
Solutions to Common Async Problems
Problem | Solution |
---|---|
Create a task to execute code | Task.Run or TaskFactory.StartNew (not the Task constructor or Task.Start ) |
Create a task wrapper for an operation or event | TaskFactory.FromAsync or TaskCompletionSource<T> |
Support cancellation | CancellationTokenSource and CancellationToken |
Report progress | IProgress<T> and Progress<T> |
Handle streams of data | TPL Dataflow or Reactive Extensions |
Synchronize access to a shared resource | SemaphoreSlim |
Asynchronously initialize a resource | AsyncLazy<T> |
Async-ready producer/consumer structures | TPL Dataflow or AsyncCollection<T> |
Read the Task-based Asynchronous Pattern (TAP) document. It is extremely well-written, and includes guidance on API design and the proper use of async/await (including cancellation and progress reporting).
There are many new await-friendly techniques that should be used instead of the old blocking techniques. If you have any of these Old examples in your new async code, you're Doing It Wrong(TM):
Old | New | Description |
---|---|---|
task.Wait |
await task |
Wait/await for a task to complete |
task.Result |
await task |
Get the result of a completed task |
Task.WaitAny |
await Task.WhenAny |
Wait/await for one of a collection of tasks to complete |
Task.WaitAll |
await Task.WhenAll |
Wait/await for every one of a collection of tasks to complete |
Thread.Sleep |
await Task.Delay |
Wait/await for a period of time |
Task constructor |
Task.Run or TaskFactory.StartNew |
Create a code-based task |
Source https://gist.github.com/jonlabelle/841146854b23b305b50fa5542f84b20c
Thrown errors are a good thing! They mean the runtime has successfully identified when something in your program has gone wrong and it's letting you know by stopping function execution on the current stack, killing the process (in Node), and notifying you in the console with a stack trace.
Doing nothing with a caught error doesn't give you the ability to ever fix
or react to said error. Throwing the error
isn't much better as often times it can get lost in a sea of things printed
to the console. If you wrap any bit of code in a try/catch
it means you
think an error may occur there and therefore you should have a plan,
or create a code path, for when it occurs.
Bad:
try {
FunctionThatMightThrow();
} catch (Exception ex) {
throw ex;
}
Good:
try {
FunctionThatMightThrow();
} catch (error) {
NotifyUserOfError(error);
// Another option:
ReportErrorToService(error);
}
Capitalization tells you a lot about your variables, functions, etc. These rules are subjective, so your team can choose whatever they want. The point is, no matter what you all choose, just be consistent.
Bad:
int DAYS_IN_WEEK = 7;
int daysInMonth = 30;
List<string> songs = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
List<string> Artists = ['ACDC', 'Led Zeppelin', 'The Beatles'];
bool EraseDatabase() {}
bool Restore_database() {}
class animal {}
class Alpaca {}
Good:
int DAYS_IN_WEEK = 7;
int DAYS_IN_MONTH = 30;
List<string> SONGS = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
List<string> ARTISTS = ['ACDC', 'Led Zeppelin', 'The Beatles'];
bool EraseDatabase() {}
bool Restore_database() {}
class Animal {}
class Alpaca {}
If a function calls another, keep those functions vertically close in the source file. Ideally, keep the caller right above the callee. We tend to read code from top-to-bottom, like a newspaper. Because of this, make your code read that way.
Bad:
class PerformanceReview {
private Employee Employee;
public PerformanceReview(Employee employee) {
Employee = employee;
}
List<PeersData> LookupPeers() {
return db.lookup(Employee, 'peers');
}
List<ManagerData> LookupManager() {
return db.lookup(Employee, 'manager');
}
GetPeerReviews() {
var peers = LookupPeers();
// ...
}
PerfReview() {
GetPeerReviews();
GetManagerReview();
GetSelfReview();
}
GetManagerReview() {
var manager = LookupManager();
}
GetSelfReview() {
// ...
}
}
var review = new PerformanceReview(employee);
review.PerfReview();
Good:
class PerformanceReview {
private Employee Employee;
public PerformanceReview(Employee employee) {
Employee = employee;
}
PerfReview() {
GetPeerReviews();
GetManagerReview();
GetSelfReview();
}
GetPeerReviews() {
vonst peers = LookupPeers();
// ...
}
LookupPeers() {
return db.lookup(Employee, 'peers');
}
GetManagerReview() {
var manager = LookupManager();
}
LookupManager() {
return db.lookup(Employee, 'manager');
}
GetSelfReview() {
// ...
}
}
var review = new PerformanceReview(employee);
review.PerfReview();
Comments are an apology, not a requirement. Good code mostly documents itself.
Bad:
public string HashIt(string inputData) {
// The hash
var hash = 0;
// Length of string
const length = data.length;
// Loop through every character in data
for (var i = 0; i < length; i++) {
// Get character code.
const char = data.charCodeAt(i);
// Make the hash
hash = ((hash << 5) - hash) + char;
// Convert to 32-bit integer
hash &= hash;
}
}
Good:
public string hashIt(string inputData) {
var hash = 0;
const length = data.length;
for (var i = 0; i < length; i++) {
const char = data.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
// Convert to 32-bit integer
hash &= hash;
}
}
Version control exists for a reason. Leave old code in your history.
Bad:
doStuff();
// doOtherStuff();
// doSomeMoreStuff();
// doSoMuchStuff();
Good:
doStuff();
Remember, use version control! There's no need for dead code, commented code,
and especially journal comments. Use git log
to get history!
Bad:
/**
* 2016-12-20: Removed monads, didn't understand them (RM)
* 2016-10-01: Improved using special monads (JP)
* 2016-02-03: Removed type-checking (LI)
* 2015-03-14: Added combine with type-checking (JR)
*/
public int Combine(int a,int b) {
return a + b;
}
Good:
public int Combine(int a,int b) {
return a + b;
}
They usually just add noise. Let the functions and variable names along with the proper indentation and formatting give the visual structure to your code.
Bad:
////////////////////////////////////////////////////////////////////////////////
// Scope Model Instantiation
////////////////////////////////////////////////////////////////////////////////
var model = {
menu: 'foo',
nav: 'bar'
};
////////////////////////////////////////////////////////////////////////////////
// Action setup
////////////////////////////////////////////////////////////////////////////////
void Actions() {
// ...
};
Good:
var model = {
menu: 'foo',
nav: 'bar'
};
void Actions() {
// ...
};