Do you start reading code?
  v11.0 Posted at 30/07/2019 10:19 AM by Tiago Araujo

“Aim for simplicity. I want to code to read like poetry”
- Terje Sandstrom

Good code​​

  • Is clear and easy to read
  • Has consistent and meaningful names for everything
  • Has no repeated or redundant code
  • Has neat formatting
  • Explains "why" when you read down, and "how" when you read left to right
public IEnumerable<Customer> GetSupplierCustomersWithMoreThanZeroOrders(int supplierId)
    var supplier = _repository.Suppliers.Single(s => s.Id == supplierId);

    if (supplier == null)
        return Enumerable.Empty<Customer>();
    var customers = supplier.Customers
        .Where(c => c.Orders > 0);

    return customers;
Figure: This code explains what it is doing as you read left to right, and why it is doing it when you read top to bottom.

Tip: Read the book Clean Code: A Handbook of Agile Software Craftsmanship by Robert. C. Martin.

Good code is declarative​​

For example, I want to show all the products where the unit price less than 20, and also how many products are in each category.

Dictionary<string, ProductGroup> groups = new Dictionary<string, ProductGroup>();
foreach (var product in products)
    if (product.UnitPrice >= 20)
        if (!groups.ContainsKey(product.CategoryName))
            ProductGroup productGroup = new ProductGroup();
            productGroup.CategoryName = product.CategoryName;
            productGroup.ProductCount = 0;
            groups[product.CategoryName] = productgroup;
}var result = new List<ProductGroup>(groups.Values);
result.Sort(delegate(ProductGroup groupX, ProductGroup groupY)
        groupX.ProductCount > groupY.ProductCount ? -1 :
        groupX.ProductCount < groupY.ProductCount ? 1 :
Figure: Bad example - Not using LINQ. The yellow gives it away.

Tip: Resharper can automatically convert this code.

result = products
    .Where(product => product.UnitPrice >= 20)
    .GroupBy(product => product.CategoryName)
    .OrderByDescending(group => group.Count())
    .Select(group => new { CategoryName = group.Key, ProductCount = group.Count() });
Figure: Good example - using LINQ

Tip: For more information on why declarative programming (aka LINQ, SQL, HTML) is great, watch the TechDays 2010 Keynote by Anders Hejlsberg.Anders explains why it's better to have code "tell what, not how".

Clean front-end code - HTML (This one is questionable as HTML is generally a designer issue)​​

Anyone who creates their own HTML pages today should aim to make their markup semantically correct. For more information on semantic markup, see http://www.webdesignfromscratch.com/html-css/semantic-html/.

For example:

  • <p> is for a paragraph, not for defining a section.
  • <b> is for bolding, not for emphasizing (<strong> and <em>) do that.

Clean Front-End code​

Clean code and consistent coding standards are not just for server-side code.  It is important that you apply your coding standards to your front-end code as well e.g. JavaScript, TypeScript, React, Angular, Vue, CSS, etc.

You should use a linter and code formatter like Prettier to make development easier and more consistent.

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address: