Developers' guide for establishing and adhering to Coding Standards

Developers' guide for establishing and adhering to Coding Standards

Coding Standards

What is the purpose of having Coding Standards?

  • Coding standard makes sure that all the codes written by different engineers have a proper and consistent appearance.
  • It enhances code’s readability, maintainability and reduces complexity.
  • It makes it easier to use the same code multiple times and find mistakes quickly.

Also Read: 8 Advantages of Custom PHP Development

1. General

General code standards
  1. Programming Code should be easily understandable.
  2. Use Meaningful and understandable variables & function name.
  3. The program should have clear notes so it can be understood easily. When people write down their thoughts on what they’re trying to do with the code, it helps others understand it better.
  4. Must use middleware in a project, Every input/output request must be passed through middleware.

2. Lines & Spaces

Lines and Spaces
  1. After putting a comma between two function arguments, make sure there is a gap before the next part starts.Lines & Spaces
  2. Make sure there is no gap between the “( ” and “)”.
  3. 2.1 & 2.2 rules are same for “for loop”, “foreach loop”, “while loop”, “if condition” etc.
  4. Make sure that every section inside another section is lined up with proper spaces and margins.spaces and margins loop
  5. When you use braces, start & end them on a new line.use braces, start & end them on a new line.
  6. Must put only two blank line gaps between two functions.Must put only two blank line gaps between two functions.
  7. Split Code in Multiple Lines
    It is a good idea to break up long lines of code into smaller sections. This means that there might be many difficult rules inside many brackets. You can break that into many lines.
  8. The beginning and ending tags need to be on the same line.

3. Variables & Function

Variables & Function
  1. Always careful when you have to use the globals variable. Limited use of globals variable. It directly affects on Project’s security.
  2. Use Meaningful and understandable variables & function names.
  3. It is recommended to use camel case lettering when naming local variables, with the first letter being lowercase.. (e.g. carType)
  4. Begin the names of global variables with a capital letter.. (e.g. CarType)
  5. Capital letters exclusively should be used to create constant names. (e.g. CARTYPE)
  6. It’s better not to use numbers in variable names..
  7. The names of the function should be written in lowercase with the underscore to separate the words.
  8. When you create a function, choose a name that explains what the function does in a simple and clear way.
  9. The length of a function should not be very large, split the function into multiple functions.
  10. There are no rules of use single & double quotes. To maintain consistency use a single quote for a string.
  11. Self-Explanatory Flag Values for Function Arguments

Read More: Top 10 Dominating Web Development Trends to Look for in 2022

4. Comments

comments code standards

The instructions written in the code should be explained clearly so people can understand them easily. When people make comments about the statements in the code, it makes it easier to understand.

  1. While coding, one should always try to describe the purpose is of each block of code.
  2. Must add a comment for file, function & importance variable.
  3. Add Company Name, Author Name & Purpose of Controller file, Model file, etc. in starting of file.

    Code must use comments
  4. Add Company Name, Name of function, Author Name, Purpose of function, Input arguments details & Response Details in a comment before the declaration of a function.

  5. Use only single-line comment (“//”) in the body area of function.
  6. When you are commenting on any developed code, Add the purpose & developer name in a comment above the commented code.
  7. Remove all unwanted commented code before moving on code of the live server.

5. Database & Query

Database & Query
  1. Use underscore( _ ) to separate the word for the Database name, Table name & Column name when you are designing the database structure. Ex: user_address
  2. Always try to a defined group for the table name. Ex. all user-related tables start with user_****, Order related tables start with order_***, etc.
  3. Each column data type must be select based on its purpose. Ex. email : varchar(100), description : text, short_description : varchar(250) etc.
  4. Every foreign key column (child) must have the same data type as the column it refers to (parent).
  5. Always try to a defined group for the stored procedure function.
    Ex.
    User-related admin SP start with admin_user_****, 
    Order related SP start with admin_order_***, etc.

    User-related api SP start with api_user_****, 
    Order related SP start with api_order_***, etc.

    User-related function start with user_****,
    Order-related function start with order_****, etc.

  6. Do not use SQL reserved keywords. Ex. ADD, KEY, GENERAL, etc.
  7. Add the purpose & Usage of a column in the comment section.

Also Read: A Comparison Between MySQL and MongoDB

6. Routes

Routes

If you give clear and easy-to-understand names, creating an API design will become simpler.

  1. Don’t use capital words in URL & PARAM.
  2. Two words should be divided by dash(-) in URL. Ex: /help-topic/
  3. Two words should be divided by underscore(_) in PARAM. Ex: order_number
  4. You’re defining on a user API, you might include the following endpoint: /user/
  5. API’s URL must be based on the usage and always maintain consistency between different modules.
    Ex
    Add User: /user/add
    Edit User: /user/edit/{id}
    Get User: /user/get/{id}
    Delete User:  /user/delete/{id}
  6. Parent module name must be included in the child module’s APIs. If you want to get a list of user’s addresses. then should be like below:
     /user/address/get/{id}
    In this example, User module is the parent module & Address module is the child module.
    Other Exp:
     /user/address/get/{id}
     /user/address/add
     /user/address/edit/{id}
    /product/addon/add
     /product/addon/edit/{id}
  7. Data must be Send & Receive in the below format:
    String Data Request & Response must be Send & Received in JSON format.
    File Data (Like Image, file, etc) Send in form-data.
  8. If your developing APIs for both mobile and admin panel. then always group both APIs by using any keyword like admin, mobile, api, etc.
    Ex:
    Admin APIs:
    admin/login
    admin/user/add
    admin/user/get/{id}
    admin/user/get/{id}


    Mobile APIs:
    api/login
    api/user/add
    api/user/get/{id}

Conclusion

There are many other standards that should be followed while developing any project. Every professional coder are following coding standards and that habit will help him/her while the project enhancement. It will make tasks faster and save valuable time.

SpryBit is having years of experience in providing optimized CodeIgniter Solutions. We have defined a set of processes to follow the coding standards to make sure your project is getting developed by following the latest trends and all the possible standards. With the help of such processes, we can accelerate in the project execution and can make sure of the project quality. Get in touch with us today to know what would be the best fit solution for your business project.

CodeIgniter,General,PHP,Web Design,Web Development
, , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Specially Thank you! for visiting.

Any Project on your Mind?

Contact us OR call us to get FREE estimate