Avoiding Coding Errors

These techniques cover quite a broad range of problems and show some good practices for debugging common Magento problems. I am going to cover some common practices that I recommend to avoid common coding problems.

Most frequently, when there is a problem in the code of a competent developer, it is a simple problem. The individual has either missed a semicolon, mismatched their opening and closing XML tags mistyped a class or object name, left a comma, bracket, or parenthetical mark without a partner, etc. Because of their simple nature, these types of problems demand a quick solution. No one feels good debugging for an hour only to find out that they accidentally typed a “q” instead of an “a” or an “l” (lowercase L) instead of an “I” (upper case i), and they did not notice because they are rendered so similarly in the current font. Ultimately, this type of problem can be avoided by several simple practices that may seem obvious but can go a long way toward solving the problems you run into.

Recommended Coding Techniques

As with anything in life, there are good coding habits that can be formed that help prevents silly mistakes. When you consider how to avoid spelling, wording, or grammatical mistakes when publishing any comment, email, blog post, or any other piece of written material you will understand the type of practices I am referring to. Using a spell checker and always proof reading your content aloud before publishing it will help to eliminate a large percentage of the problems. In coding, there are similarly simple practices which can save you countless hours.

1. Whenever possible, copy and paste instead of retyping

I recommend this for a number of reasons, consider the case first mentioned where you mistype a character that is hard to spot. That never would happen if you had instead done a copy and paste. This can also save you a significant amount of time when reusing a code block. This will also prevent you from making simple, hard-to-find mistakes. These mistakes can include missing one character or mistyping a word and replace those errors with larger easier to find mistakes like accidentally leaving an entire extra line, multiple extra words, or an extra separator or control character. This type of mistake can be further minimized by becoming more precise in your copying and pasting. e.g. copying no more or less than the portion you need instead of just the entire line or paragraph.

2. Never stop in the middle of coding a section

Once again, this one may seem especially obvious but there have been numerous times where I have been coding something and someone asks a question or I get distracted by email or some other thing. When I am coding now, I do my best to make sure to finish the section of code before moving to something else even for a moment. If you do have to stop in the middle, use the same type of reminder of what you had left to do. One example of this is a TODO on the same line as your cursor.

3. Close opening tags immediately

I generally will close my tag as soon as I open them. This ensures that I don’t have to worry about closing them later. This is applicable to html/xml tags, function calls, class definitions, function definitions, loops, if statements, and more. Often times, editors will do this for you, and that is great. An IDE can also be helpful for checking if a tag is closed because it will usually highlight the companion brace, bracket, or parenthesis for you when your cursor is next to one of these elements.

4. Find a way to not forget semicolons (where applicable)

One possible solution to this challenge is to type the semicolon first and then move your cursor back a space and type the line of code. Another possible solution is to actively think about semicolons for a period of time while coding and building a good awareness and habit of typing them. This is generally more of a newbie mistake but if you do not build a good habit surrounding semicolons, this mistake could continue to require time spent debugging. Fortunately, most of the time, the error message associated with this problem is clear.

5. Follow established coding standards

There are numerous coding standards for programming languages, and each is slightly different stylistically. The key is to follow an established way of formatting your code. This makes spotting mistakes easier and it ensures that others will be able to read your code. It also is easier to do code review if formatting is consistent. It is very easy to get confused if your indentation does not match the actual nesting of code.

6. Make your code easy to understand

This is probably one of the most important aspects of writing bug-free code. Write code that you or anyone else who knows the language will be able to come read and understand at a later time. Double nested ternary operators may save you 4 lines of code and calling a chain of functions directly instead of assigning the value to a variable may seem easier, but a significant increase in legibility for the future is worth these minor inefficiencies/style changes.

7. Comment even slightly unclear code

Generally, when I write a short method I will comment on the method in the doc block. When I write a longer method, I comment inline above each logical section of code and create line breaks to signify the separation. This takes only a few seconds to do each time once you have made it a habit and it can save hours of untangling code later. Keep these comments accurate so that someone coming in later can read the comment and check the code for results. Unit tests take this concept a step further, but in the absence of unit testing, good comments can get you most of the way there for a fraction of the overall time requirement.

The list above is clearly not exhaustive, and some of the items in the list may not be applicable to everyone. I would also recommend the use of a good IDE; this will make some of the above suggestions easy or even unnecessary. The overall goal of the list is to emphasize that embracing good coding practices and habits helps to curb simple mistakes and is the best way to save time debugging. This will leave larger and more challenging bugs as the primary ones in need of attention. These are bugs that either you didn’t write which are buried in the core of the framework or application on which you are working (in our case, Magento) or problems that stem from incorrect or incomplete logic. These types of problems can often require a variety of techniques to debug, especially when the bug is in the code you neither wrote nor have even seen before. Feel free to add tips in the comments that you use regularly to avoid simple coding errors.

Share it

Topics

Related Posts

Google and Yahoo Have New Requirements for Email Senders

What ROAS Really Means

Everything You Need to Know About Updating to Google Analytics 4

Contact Us