How to Maintain and Write better code

How to Maintain and Write better code

ยท

4 min read

Writing and maintaining code is necessary because you want to make it easily understandable for people. So here are some of my tips on how you can write better code and maintain it.


Avoid Global Code

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

Use meaningful structures

  • Structuring your application is very important, don't use complicated structures, always stick to simplicity.
  • Always split the four parts of any typical application apart from each other - HTML, CSS Templates/Layouts, JavaScript, PHP Code.

Use meaningful Names

  • Never use names like $k, %n, and $test for your variables.
  • Good code should be meaningful in terma of variable names, function/method names, and class names.
  • Some good examples of meaningful names are: $request, $dbResult and $tempFile (depending on your coding style guidelines these may use underscores, camelcase, or, pascal case).

Write useful comments

  • Write meaningful, single line comments for vague lines.
  • Write full parameter and functionality descriptions for functions and methods; for tricky logic blocks, describe the logic in words before it if necessary.
  • Don't forget, always keep your comments up to date!

Use a coding standard

  • It's easy to write bad, unorganized code, but it's hard to maintain such code.
  • Good code typically follows some standard for naming conventions, formatting, etc.

Use automated build tools

Refractor

  • Code refactoring is the eight habit of highly effective developers.
  • Believe it or not, you should be refactoring your code on a daily basis or your code is not in good health!
    • If your function or method is more than 20-25 lines, it's more likely that you're including too much logic inside it, and you can probably split it into two or smaller functions/methods.
    • If your method/function name is more than 20 characters, you should either rethink the name or rethink the whole function/method by reviewing the first rule.


Need Help

Need help in raising funds to buy a Mechanical Keyboard. This pandemic has affected my family badly so can't ask my DAD for it. Please Help Me.

1.png ๐Ÿ˜Ž Thank You For Reading | Happy Coding โšก

Did you find this article valuable?

Support Rahul by becoming a sponsor. Any amount is appreciated!