Sunday, December 16, 2012

Best practices to write php code

Today I will show you a few tricks how to write good php code. If you follow these tricks, your code is easy to read and easier to maintain.

1. The boy scout rule
2. Typehinting
3. Comment rules
4. Distinctive mark for crap
5. How should I begin to refactor?
6. KISS

The boy scout rule

The Boy Scouts have a rule: "Always leave the campground cleaner than you found it." If you find a mess on the ground, you clean it up regardless of who might have made the mess. 

That is the best approach. I can confirm this from years of experience. For more information visit the following link. 


Typehinting

Use typehinting whenever it is possible! PHP supports objects and the array type. For an example take a look at the manual. 


This have an advantage too. Your IDE knows then which class is assigned to the function variable! Your IDE does not support  a correct autocompletion? Then try out PhpStorm.


Comment rules

  • Write your name on each comment for feedback
  • Mark functions that no longer to be used as deprecated. Use the @deprecated tag, explain why and enter the right name of the function class.
  • Describe your functions only when they do "unexpected" or confusing. 
  • Prefers to invest time for a meaningful and clear identification name of the function. It keeps comments shorter.
  • Write always phpDocs! PhpStorm can do it for you quickly!

Distinctive mark for crap

  • A function has more then 6 parameters
    • Solution: Revise your design or use bit-flags
  • Optional parameters are not in last position
  • You use hard coded strings or numbers in switches or if-conditions
    • Solution: Use speeking class constants
    • Advantage: You can change them in one place and search for it easier
  • Code is repeating itself similar or equal
    • Solution: Summarize it into one function
    • Advantage: You need only maintain code in one place. Imagine if you had the code 4 timtes in a file. Then you might have to fix a bug 4 times.
  • Your code of the function or class is very long
    • Solution: Make it shorter into more functions or classes

How should I begin to refactor?

  1. Try to understand the logic. What is the point of this class or this function?
  2. Complete order. Give classes, variables and functions useful names speaking for themselves.
  3. Reduce duplicate code. Summarize them together to a central location.
  4. Looking for distinctive mark for crap
  5. Now is the time to think about whether this is the right architecture. Should I choose a more suitable design pattern?

KISS

Yes KISS! If you want to build a castle, then first build a great tower and expanding gradually. 

Finally, a well-intentioned advice

Just write code which you really need! Everything else is useless and a waste of time and possibly expensive.

10 comments:

  1. According to their potential they wish to be useful for finding the correct people to your company and maintain your workers devoted to your business eventually by supplying these with a work environment in which they can best create by interim hr.

    ReplyDelete
  2. While composing PHP code, software engineers have alternative to utilize either single statements (') or twofold cites ("). Yet, the designers can without much of a stretch upgrade the presentation of the PHP application by utilizing single statements rather than twofold cites.Why use Laravel

    ReplyDelete
  3. Reliable, fast and sensible assistance! This was my first-time exploitation of this specific service! I used to be frightened and skeptical of ordering meds on-line, DE Medz I'd without a doubt endorse completely. Thanks a lot

    ReplyDelete
  4. Application programs are intended to do explicit assignments to be executed through the PC and the working framework programs are utilized to deal with the inside elements of the PC to encourage utilization of use program. https://www.apkmacpc.com/itools-crack/

    ReplyDelete
  5. I guess I am the only one who came here to share my very own experience. Guess what!? I am using my laptop for almost the past 2 years, but I had no idea of solving some basic issues.
    https://free4crack.net/wp-admin/
    JetBrains PhpStorm Crack

    ReplyDelete
  6. keep it up good job JetBrains PhpStorm 2021.1.3 Crack

    ReplyDelete
  7. Thanks For making post and sharing the important data..keep it up!!!
    PhpStorm 2021.1.3 Crack

    ReplyDelete
  8. I genuinely believed you would probably have something useful to say. All I hear is a bunch of whining about something that you can fix if you were not too busy looking for attention. After all, I know it was my choice to read.. paper代写

    ReplyDelete