Coding Standards & Best Practices In .Net, Javascript, HTML etc.

Code is written to make some logic to work but its not necessary that it can be a called good quality code. It has to be clean, well written and as per the standards. Because there is a huge difference between COMPLETION & PERFECTION. Your code may be complete to perform some task but its developer's duty to make sure its close to perfect.





We will tell you about some standards and good practices to make use in your daily coding life. Lets start with some quick points:

Cleanliness 
  • The code should be clean in sense of readability, so that if some other developer tries to debug your code he/she doesn't cry a foul. 
  • Keep unwanted commented code at bay.
  • Try to invest sometime to refactor the code and make it as concise as possible but yet readable.
  • Make sure repetitive code should be avoided as much you can.
Keep It Short
  • Keep variable or function names short. No need to keep elaborate names like 'indexofnextpage', write 'iNextPg'. You can keep a comment at the point of declaration describing the variable name. Try to keep it short yet understandable.
Peer Reviews
  • Make it  a habit to get your code reviewed from your fellow teammates, they might correct you at some place which you somehow didn't notice.
  • There is no harm in getting corrected from someone else. Ultimately its all about team and your code quality.
Identifier Casing

There are three types of character casing used in coding:

  • camelCasing: In this case the first letter of the identifier is in lowercase and the first letter of subsequent word is in uppercase.
  • PascalCasing: In this case the first letter and the first letter of subsequent word is in uppercase. 
  • UPPERCASE: In this case all the letters are in uppercase.


Variable Identifiers
  • A variable should be declared using camelcasing(described above).
  • The identifier should contain a small representation of the type of the value it might contain like 'str' for string, 'i' for integer, 'b' for boolean, 'lst' for list etc. Its not mandatory but its good to have in respect of readability.
  • The identifier should be small and easy to understand. There might be a trade-off in between these two. Use common sense :)
//Examples Of Few Variable Identifiers:
string strName = "CheezyCode"; //Depicts Name
int iPage = "0";               //Depicts Page Index
var lstLinkedItems = null;     //Depicts List of Linked Items


Method/Function Identifiers
  • A variable should be declared using pascalcasing(described above).
  • Should be short.
  • It should clearly specify its intended use.

//Examples Of Few Method Identifiers:
function CalculateArea()       //In JavaScript
public int LoadMoreRecords()   //In .Net

Constant Identifiers
  • Constant variables should be declared using uppercase.
  • As we are using uppercase characters in identifiers, so we need something to separate the words but still keeping them together. So for constants we generally keep the following words using an '_' underscore.
//Examples Of Few Constant Identifiers:
var MAX_LENGTH = 10;                        //In JavaScript
const string KEY_ENCRYPT_MSG = "!@DFGJH";   //In .Net

For HTML & CSS
  • Keep css classes and html element id's in lowercase and separate the words using '-' hyphens.  
  • Try prefixing the element type in the element id, just to make it more identifiable. After all these little things will help you later.
These are our view and take on the best practices. You may choose to do it differently but when you do, make sure your team or your enterprise follows that accordingly. As its all about good code, quality products and a happy team. 

Feel free to tell us how we are doing, we are open for criticism as well :)

Happy Learning!!!

Comments

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example