How to Simplify your Code

Clarify your objective/Understand the code

  • Context is everything

Remove Redundant Code

  • Use TODO comments
    • Adds to a TODO list for the file
  • Analyze Project Errors
  • Fix errors and warnings
    • In PHP storm, look at top right corner of file for # of errors
  • Removed duplication
    • User methods
    • Reduce cyclomatic complexity (Path program takes)
    • Use foreach instead of for if it is possible
  • Reduce nesting
    • Use for each
    • Use functions to call code that is repeated
  • Commenting Principles
    • Only write comments when needed.
    • Add enough comment to make your code clear for another developer, regardless of their experience level.
  • Abstraction
    • Abstract a piece of code somewhere else
    • Place your function elsewhere, and only call the function in your piece of code
    • Seperate your code that does the work from the result required in your code
  • Bounded context
    • Domain driven design
    • It is good to segregate
    • The bounded context is how you communicate between different sections of your domain.
    • Purpose
      • Keep domains seperate but make it clear how they communicate with each other.
  • Repository pattern
    • The responsibility of a repository is to query the storage of that data and return it to the application and return it to the application in a form that makes sense within the application
    • Example.
      • Gravityforms uses form field ids. A repository can be used to interpret this into human understandable names.
  • Include code as close to where it relates to as possible.