Sunday, March 16, 2008

Coding better C++

I've prepared a short guideline for writing better codes in C++. I've prepared the list from my C++ experiences and studies. Each item of the list needs good amount of explanation which is outside of the scope of this post. I'll try to explain them in the future posts in my blog. For now, if you need more information on an suggested item, you can look up on the internet or in the books.

Here it goes:
  1. C++ is not C. Stop mixing C and C++ unless it is required. Start thinking in C++.
  2. Have a C++ coding standard defined or follow the coding standard defined for your project or organization.
  3. Design from outside, design the interfaces first.
  4. Avoid using assignments in constructors, use initializer list to initialize members.
  5. Know the need of copy constructor and define when needed.
  6. Use virtual destructor when your class is used as a base class.
  7. Never call a destructor (unless you're using a placement new). If you do, you may need to revise your software design.
  8. Avoid using placement new.
  9. Avoid using Macros for functions. Use Inline functions instead.
  10. Avoid using Array, unless you have to. Use standard library containers like Vector, etc.
  11. Avoid C style cast, use the C++ casts.
  12. Use references when you can and pointers when you have to.
  13. Use composition when you can and private inheritance when you have to.
  14. Use const for function parameters when you can.
  15. Be careful about static initializations, you might get unexpected behavior if the static variables/objects has dependencies.
  16. Be careful about multiple inheritance. Understand the 'dreaded diamond'.
  17. Be careful about floats and doubles. They may give you unexpected behaviors.
  18. Avoid constant literals in the code. Separate them in static inline functions or macros.
Each suggestion, mentioned above, will take time to get used to (if you're not already). For example, item 1 says "C++ is not C. Stop mixing C and C++ unless it is required. Start thinking in C++.". Now if you were a C programmer, it is not easy to stop using C, for example: it may become difficult for you to stop using stdio functions and using iostream instead. It'll take time and practice. (Please note, stdio functions might have advantages over iostream but that's not the point here).

Once you get used to all of the suggested points, you'll find that your code is much much better than many others and of course, more optimized and less error prone. Let me emphasize, 'these are golden suggestions, try to follow them'.

Happy coding :).

How to begin web programming with PHP

I am often asked, 'how to begin web programming with PHP?'. Following is a response that I had given few weeks ago. Hope this will be useful for anyone willing to start learning PHP.

Things you need to know to begin web programming with PHP:

  • Good knowledge of HTML
  • Some knowledge of CSS
  • Some knowledge of JavaScript
  • Good knowledge of MySQL Database, SQL (as most php applications are database driven)
  • Good knowledge of PHP
  • Using and configuring Apache
Tutorial Links:

Here are the links to tutorials that may help you to accelerate your learning process.

Recommended Setup:

Recommendations:

  • Keep the references handy/near to you.
  • Search in Google for more tutorials
  • Look into well known PHP frameworks (search for: top php frameworks).
  • Look into open source projects to learn how real life applications are done.
  • Practice well :)