3.6 Common Programming Errors

Syntax Errors

As a programmer, it is important for you to become comfortable with error messages. Even the most experienced computer programmers will have a typo or forget a quotation mark regularly. When you first start, error messages will seem incomprehensible. Later, they will help you fix the problem quickly.

Gem of Wisdom

When you get an error message, do not just try the program again. This may work with your toaster, where you just unplug it and plug it back in, but with software development, it rarely, if ever, works. Figure out the problem, make a correction, and then see if it works.

Try the following command in irb:

irb(main):001:0> x = 1 + "hello"
TypeError: String can't be coerced into Fixnum
     from (irb):1:in '+'
     from (irb):1

Syntax errors refer to code that Ruby cannot actually execute. Since it does not make sense for Ruby to add an integer to a string, it stops execution and informs you of the location where it had to stop.

Let’s move on to a more realistic example. When entering strings, we are likely to forget quotes once in a while.

irb(main):002:0> x = hello
NameError: undefined local variable or method 'hello' for
main:Object
from (irb):2

Wait! Why does the Ruby interpreter not just tell us that we forgot quotes? Ruby must guess what we intended. All Ruby knows is that we tried assigning hello to x. Since any string not contained in quotes is the name of a variable or a command, Ruby tries to find the value stored in a variable named hello. Since Ruby cannot find any variable named hello, it prints an error. It is important to start viewing code in terms of how Ruby might interpret it, but this is a skill that will take time to develop. Meanwhile, realize that errors will happen, and expect to be learning from them.

Logic Errors

Errors that Ruby cannot catch are logic errors. They are problems with a program that cause it to return the wrong or undesired result. For example, if you are trying to find the area of a rectangle, but you multiplied the height by itself instead of by the width, that would be a logic error.

The Ruby interpreter, the program that executes the Ruby instructions input by the user, does not try to guess that we meant to use a different variable. Logic errors are often much harder to trace than syntax errors because of this. To avoid them, it is often useful to look at your program and “play computer.” That is, pretend that you are the Ruby interpreter, and follow the steps that will be implemented as the program is executed. From this point on, we’ll use the words Ruby and Ruby interpreter interchangeably.

Gem of Wisdom

Truncation refers to limiting the number of significant digits by discarding extra digits. Unlike rounding, truncation simply throws the extra digits away.

A more common error involves integer division. Arithmetic expressions that operate on integers will always evaluate to integer form. This happens even with division, where the result would otherwise be a decimal. Ruby does this by truncating the arithmetic result (chopping off the decimal place and all the digits to the right of it). For example, 5 divided by 2 will evaluate to 2 instead of to 2.5.

irb(main):003:0> 5 / 2
=> 2

If you get a wrong value using this computation in a program, then your final result will likely be erroneous, too.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.93.12