Skip to main content

5 Tips To Code Like A Pro

Writing code for a web app or software is the most important part of Development Life Cycle. The coder needs to organize everything from the beginning – especially for enterprise level projets. If it’s not properly organized, the coding processes and code management afterwards may end up not just time consuming, but also a bit frustrating.

Well written code is maintainable, reusable, and testable. Following tips will help you to code like a pro:

1. Follow Coding Standards

It’s easy to unorganized code, but it’s hard to maintain such code. Good code typically follows some standard for naming conventions, formatting, etc. Such standards are nice because they make things deterministic to those who read your code afterwards, including yourself.

You can create your own coding standard, but it’s better to stick to one with wider-acceptance. Publicly maintained standards like Zend Framework Coding Standard or PSR-1 Coding Style Guide, it will be easier for others to adapt.

2. Write Useful Comments

Proper comments improve readability of the codes. This is a crucial part of standard coding and helps into maintenance of the code.

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. And don’t forget, always keep your comments up to date!

3. Use Meaningful Variable Names

Never use names like $x, $y, and $test for your variables. How do expect to read such code in the future? Good code should be meaningful in terms 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 PascalCase).

4. Refactor Code

Refactoring keeps your code healthy. You should be refactoring everything, from your architecture to your methods and functions, variables names, the number of arguments a method receives, etc. There is no standard rule for it but practising following rules makes code better and organized:

If your function or method is more than 20-25 lines, it’s more likely that you are including too much logic inside it, and you can probably split it into two or more 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.
If you have a lot of nested loops then you may be doing some resource-intensive processing without realizing it. In general, you should rethink the logic if you are nesting more than 2 loops. Three nested loops is just horrible!
Consider if there are any applicable design patterns your code can follow. You shouldn’t use patterns just for the sake of using patterns, but patterns offer tried-and-true ready-thought solutions that could be applicable.

5. Use Meaningful Structures

Structuring your application is very important. Keep it simple and don’t use complex structure. When naming directories and files, use a naming convention you agree upon with your team, or use one associated with your coding standard. Always split the four parts of any typical PHP application apart from each other – CSS, HTML Templates/Layouts, JavaScript, PHP Code – and for each try to split libraries from business logic. It’s also a good idea to keep your directory hierarchy as shallow as possible so it’s easier to navigate and find the code you’re looking for.

Comments

Popular posts from this blog

DataTable with PHP and MySQL

Data representation in form of tables and grid is one of the most import web component. Generally we need to perform sorting, pagination on it, which is very complex task to do. To solve this problem, many grid view framework are there. Well, among these frameworks, DataTable is most popular. Features like open source, light weighted, highly flexible and customizable, features like AutoFill, inline editor, sticky header, responsiveness, bootstrap support and foundation are the reasons behind its popularity. In basic initialization datatable provides pagination, sorting, instant searching by loading whole data records at once. It can be a performance issue fetching large amount of data from server side. It will be better if you integrate server side pagination, searching and sorting, so we can break large amount data in chunk, So performance will increase significantly. Let’s have a look about using and customizing it. We need to include plugin files (js & css) and fetch data f

Delete All Products at Once | Magento

Well, usually during development, you may need to add some test products (Sample Data) which needs to be removed before moving the site to production. If the volume is large, then it would be very difficult to delete it from the Product Grid. In this case, you may need some trick. Well, you can use the following method: 1. Log into MySQL server via CLI or phpMyAdmin 2. Run the following SQL Queries and it will do the job: SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE `cataloginventory_stock_item`; TRUNCATE TABLE `cataloginventory_stock_status`; TRUNCATE TABLE `cataloginventory_stock_status_idx`; TRUNCATE TABLE `cataloginventory_stock_status_tmp`; TRUNCATE TABLE `catalog_category_product`; TRUNCATE TABLE `catalog_category_product_index`; TRUNCATE TABLE `catalog_category_product_index_tmp`; TRUNCATE TABLE `catalog_compare_item`; TRUNCATE TABLE `catalog_product_bundle_option`; TRUNCATE TABLE `catalog_product_bundle_option_value`; TRUNCATE TABLE `catalog_product_bundle_price_index`; TR

Why PHP Is Best For Web Development

While working on any website of web application, the first question that comes in mind is which programming language is best. If you ask programmers, they might love or hate PHP as server side scripting language. Well the opinions may vary but there are several reasons which make PHP one of the best programming language for web development. Here are 5 best reasons to love PHP (Hypertext Preprocessor): Less Expensive It requires no licensing fee and it has less expensive hosting servers available. Its software are mostly free and open source which makes it less expensive. Faster Web apps which are developed on PHP are comparatively faster as it uses its own memory space to run. Database Flexibility Well, one of the best reasons to love PHP is its flexibility towards databases. It can connect to several databases the most commonly used is the MySQL.  MySQL can be used for free. Very Good Documentation PHP also has very good online documentation with a good framework of f