August 1, 2014
Despite its technical reputation, coding is a creative endeavor and offers a wealth of flexibility in solving problems. On the other hand, the language adheres to certain standards. Since a machine interprets code, you must follow certain protocols or risk non-working code. Even minor errors and typos can throw everything off and leave your code inoperable. Thankfully these are often small mistakes and quick changes. Today we will cover five of the most common errors.
The most common error you receive is a notification that you are attempting to use an undeclared variable. Alternatively, if you are using the same names with different capitalization (don’t do this!) you could inadvertently use the wrong variable in your functions.
The easiest way to avoid this problem is to use a standardized style. Most often this is accomplished by using camel case where the first letter of each word in the variable is capitalized, such as clicksToday. You could also use under scores, such as clicks_today. As long as you keep up the habit it will become natural and avoid many of these issues.
Brackets are similar to semi-colons, except they are used to encapsulate chunks of code, most often when building functions. When a script calls the function, everything within the brackets is executed. This has many benefits for both organization and code control but can present problems if you misplace them.
The two most common errors are missing and misplaced brackets. If you forget a bracket, the script may misinterpret the code within the function or may not run at all, giving you a missing bracket error. One of the benefits of the AdWords scripting interface is color-coding your brackets. If you click on a bracket that does not have a matching open or closing bracket, it will be highlighted in red. If it does have a closing bracket, it will be highlighted in green along with its partner.
Mixed Quotation Marks
The other likely scenario is if you are trying to include quotes in a string variable. For example you want to insert quotation marks into ad copy; to do that simply switch between them. Open with single quotes, insert double quotes and the text, and then close with single quotes. Just like this, ‘ “This is a quote example” ‘. This will interpret the double quotes as part of the text, rather than part of the code.
While working in the editor just take a look at the coloring. The string variable should be a consistent color and different than the rest of the code. If everything is highlighted like a string, or not enough, you made an error with a quotation.
Once you get beyond the basics of manipulating variables you’ll want to start playing with functions. Functions are confined pieces of codes that both organize code and help you with repeatable operations. For example rather than manually calculate ROAS or CPA you could define a function that you could simply reuse when needed, rather than write it out each time.
The easiest way to understand is that as the scope gets more specific, variables and data outside the scope are inherited but it does not necessarily work in reverse. If you initialize a variable inside a function, it will be private to that function. Not that you can’t use it somewhere else but you need to either return to another variable outside of the function. If you aren’t careful you may end up with results you are not expecting.