Coding Best Practices

Advantages of following coding best practices

  • Code readability
  • Maintainability
  • Dependability
  • Efficiency
  • Usability

Key Points

  • Coding Style and Standard
  • Code Correctness, fragmented code & conciseness of code
  • Low Coupling and High Cohesion is expected
  • Object oriented programming
  • Abstraction, encapsulation and information hiding

Oops

Encapsulation is when a group of related methods, properties, and other members are treated as a single object. (public, private, protected)

Inheritance is the ability to receive (“inherit”) methods and properties from an existing class. (Extension)

Polymorphism is when each class implements the same methods in varying ways, but you can still have several classes that can be utilized interchangeably.

Abstraction is the process by which a developer hides everything other than the relevant data about an object in order to simplify and increase efficiency. (Interfaces)


Key Rules

  • Code Structures should meaningful

Structuring your application is very important; don’t use complicated structures, always stick to simplicity.

  • One class should and must have single purpose.
  • Break down the complex logic into simple one.
  • One method might have single purpose.
  • Avoid long code block
  • Line break is necessary for code readability
  • Remove unnecessary and unused code
  • Fragment code regularly


  • Use proper naming conventions

Good code should be meaningful in terms of variable names, function/method names, and class names.

  • Never use names like k, m, and test for your variables.
  • Use Pascal Casing for naming Class (e.g. PeopleClass), Method, Property (public), Events, Constants
  • Use Camel Casing for Method Parameters, Local Variables (e.g. dbResult)
  • Use Camel Casing for Private/Protected member variables and can use underscore (e.g. _MyClassMember)
  • No “Hungarian Notation” (e.g. strFirstName)
  • Favour readability over brevity


  • Add comments and summary

  • Comments are crucial specially for class, methods, properties etc.
  • Useful comments make life easier for yourself and those after you who have to maintain your code.
  • Must add summary for every public methods, as those are accessible from other assembly.
  • Never forget to keep your comments up to date if you change any functionality.

  • Useful Coding practices

  • Try not to use hardcoding
  • Avoid using nested try catch blocks
  • Prefer collection over arrays
  • Declare variables when needed
  • Never use nested ternary operator
  • Code refactor
  • Remove unused “Using”
  • Never returns null from methods
  • Use null checks

  • Avoid using global code

  • Global variables and loops are a mess and can prove problematic when your application grows to millions of lines of code (which most do
  • Think twice before you pollute the global namespace with variables, functions, loops, etc.

  • DRY principle

DRY stands for Don't Repeat Yourself. Also known as DIE: Duplication is Evil.

The principle states:

"Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."

The purpose for most applications (or computers in general) is to automate repetitive tasks. This principle should be maintained in all code, even web applications. The same piece of code should not be repeated over and over again.


  • Code Grouping

More often than not, certain tasks require a few lines of code. It is a good idea to keep these tasks within separate blocks of code, with some spaces between them.


// get list of forums

forums = ForumObject.GetForums();

 

// load the templates

LoadTemplate('header');

LoadTemplate('footer');


  • Other points

  • Use code review tools (like resharper, SonarQube)
  • Use Code Documenters
  • File and Folder Organization


Comments

Popular posts from this blog

Sitecore Powershell Extension (SPE)