- Joined
- Jan 6, 2010
- Messages
- 851
This is just a small list I have compiled from my experience of the do's and don't of PHP coding.
Do's:
Don't:
I am sure a few are asking about certain items above, let me explain them a bit more so everyone understands.
Always double check your code: Over the past 6 years I have been programming in PHP, something I learned a long time ago is to always double check my code. You can make a minor mistake, just a small keystroke such as ; or ' can easily break your code. Many developers find that double checking your code prevents this from happening but double checking your code also makes sure you are directing your queries, variables, etc to the right area.
Keep backups of everything no matter how small a change: When it comes to developing a huge script or even a small one, if you don't make a note of what you changed or make a backup of that file you take the chance of losing a lot of work or worse yet not knowing where you made the change that caused an error. Small changes sometimes can lead to big problems, so keeping a backup of what you change or an unchanged version of the file will prevent you from scratching your head or getting aggravated.
Think your code is safe online or externally: I have recently discovered that trusting an online source or even your own external hard drive can be a misplacement of trust. Online sources can have servers go down or worse yet hard drives go out causing loss of data. In some cases fires break out preventing any backup of data. External hard drives have a tendency to completely delete all data with no warning, as I have recently experienced. With this happening you lose everything unless your smart and create discs to backup everything on that external.
Openly post your code to various places: When posting your code to various places you take the chance of someone else seeing your code and stealing. This other person could easily then change it or do what they want to it then release it as their own. Sometimes they don't even change the code enough to tell the difference but how can you prove it was your code originally? Well something I have learned is you can make a signature in your code that is hidden to those in experienced in it. Signature's in code is new I'm sure to many but to those who know of it will tell you its easy, your basically hiding your signature in wide open space but no one notices it.
Let others do the code for you: Let's say you as a coder ask me to help out, you provide me the files and everything I need. I proceed to make changes and get it working, when you get the files back you have no idea what changes I made because I did not mark what I changed in the file, therefore if it breaks because of an error then you won't know what code is what causing you to delay fixes and in some cases, no fixes for a very long time.
Reinvent the Wheel: (Thank you Christopher) I think one of the biggest mistakes any developer can make (not specific to PHP) is reinventing the wheel. Database abstraction. Internationalization. Templating. Javascript. The list goes on and on.
One of the reasons PHP is such a great platform is because of the abundance of freely available software libraries at your disposal (Doctrine, Zend Framework, Twig, Swiftmailer, or for JS, jQuery, underscore etc). These libraries are almost always guaranteed to be better developed than your own replacements because of the community around them. They will be richer, better documented and better tested.
Focus on your own product and don't waste time reinventing peripherals.
I hope those reading have found this to be knowledgeable and make suggestions to add to the list. I want to provide a means for those new to PHP a way to avoid mistakes that us experience developers have made.
Do's:
- Always double check your code (Read More Below)
- Test, test, test, and then test again
- Gather tester's for heavy testing enviroments
- Keep backups of everything no matter how small a change (Read More Below)
- Ask for help if you need it or you are stuck
Don't:
- Assume your code is flawless
- Think your code is safe online or externally (Read More Below)
- Openly post your code to various places (Read More Below)
- Think you will have a bug free script/snippet
- Give up no matter how hard it seems
- Let others do the code for you (Read More Below)
- Reinvent the Wheel (Read More Below)
I am sure a few are asking about certain items above, let me explain them a bit more so everyone understands.
Always double check your code: Over the past 6 years I have been programming in PHP, something I learned a long time ago is to always double check my code. You can make a minor mistake, just a small keystroke such as ; or ' can easily break your code. Many developers find that double checking your code prevents this from happening but double checking your code also makes sure you are directing your queries, variables, etc to the right area.
Keep backups of everything no matter how small a change: When it comes to developing a huge script or even a small one, if you don't make a note of what you changed or make a backup of that file you take the chance of losing a lot of work or worse yet not knowing where you made the change that caused an error. Small changes sometimes can lead to big problems, so keeping a backup of what you change or an unchanged version of the file will prevent you from scratching your head or getting aggravated.
Think your code is safe online or externally: I have recently discovered that trusting an online source or even your own external hard drive can be a misplacement of trust. Online sources can have servers go down or worse yet hard drives go out causing loss of data. In some cases fires break out preventing any backup of data. External hard drives have a tendency to completely delete all data with no warning, as I have recently experienced. With this happening you lose everything unless your smart and create discs to backup everything on that external.
Openly post your code to various places: When posting your code to various places you take the chance of someone else seeing your code and stealing. This other person could easily then change it or do what they want to it then release it as their own. Sometimes they don't even change the code enough to tell the difference but how can you prove it was your code originally? Well something I have learned is you can make a signature in your code that is hidden to those in experienced in it. Signature's in code is new I'm sure to many but to those who know of it will tell you its easy, your basically hiding your signature in wide open space but no one notices it.
Let others do the code for you: Let's say you as a coder ask me to help out, you provide me the files and everything I need. I proceed to make changes and get it working, when you get the files back you have no idea what changes I made because I did not mark what I changed in the file, therefore if it breaks because of an error then you won't know what code is what causing you to delay fixes and in some cases, no fixes for a very long time.
Reinvent the Wheel: (Thank you Christopher) I think one of the biggest mistakes any developer can make (not specific to PHP) is reinventing the wheel. Database abstraction. Internationalization. Templating. Javascript. The list goes on and on.
One of the reasons PHP is such a great platform is because of the abundance of freely available software libraries at your disposal (Doctrine, Zend Framework, Twig, Swiftmailer, or for JS, jQuery, underscore etc). These libraries are almost always guaranteed to be better developed than your own replacements because of the community around them. They will be richer, better documented and better tested.
Focus on your own product and don't waste time reinventing peripherals.
I hope those reading have found this to be knowledgeable and make suggestions to add to the list. I want to provide a means for those new to PHP a way to avoid mistakes that us experience developers have made.