
-----------------------------------
Martin
Thu Oct 07, 2004 12:00 am

Debugging
-----------------------------------
Think of this as advice on how to write better code. It will be an ongoing tutorial, updated as I get time.

-----------------------------------
Martin
Thu Oct 07, 2004 12:26 am


-----------------------------------
1. Comments
Comments are essential to programming. This is somewhat ironic, as comments do not actually effect the code in any way. A comment is just what it sounds like; it is a programmer's remark, or a note in the code.

In most languages, comments are tagged in one of two ways: there are single line comments, which are usually denoted by '//' and there are encasing comments, opened by '/*' and closed by '*/' . Instead of '//' to denote single line comments, turing uses '%'

Now, the question is: what does a comment do that is so useful that it can help me write programs that don't crash? It tells the computer to ignore a line of code. Below is an example of comments in a turing program:

put "hello"
% This line is commented, and does nothing
var x : int %you can put comments onto the end of lines of code
x := 5
% Everything after the comment sign (%) is ignored by the processor.
% put "This line is actually commented out, and doesn't ever get run"
for i: 1 .. 5
  /* multiple line comments
      can be encased in comments
       like these */
     put i
     % x := x + 1
end for

Now, quick quiz: what is the value of x at the end of this program? The correct answer is 5, as the line x := x + 1 is commented out.

Alright enough of the boring stuff, now onto why comments are useful. Programmers use comments to leave themselves, or other programmers, notes in their code. This can be for two purposes: either the section of code is confusing, and writing out what it does in english helps make it easier for you to understand, or to simply make it easy to start in the middle of the code and know what is happening. It doesn't matter how good of a programmer you think you are, well commented code is essential. Now, by well commented, I don't mean excessively commented. Lines that are obvious what they do don't deserve comments. As follows:

Uselessvar x: int %this is an integer
Anyone could have figured this out
Goodvar x: int %stores the number of sheep
Much better, this comment is short, and explains exactly what the variable does.

More specific uses of comments will be explained in later tutorials.

-----------------------------------
wtd
Thu Oct 07, 2004 2:08 am


-----------------------------------
Even better, use:

var numberOfSheep : int

No need for comments.  :)

-----------------------------------
shorthair
Thu Oct 07, 2004 10:57 am


-----------------------------------
would you rather type x every time , or numberofsheep


by commenting , you have to only write it once and you get hte point acroos, 

wtd your way  requires much more effort , the only way that i would use it would be

var  X ,NumberOfSheep : int
NumberOfSheep := X

 :D  :D  :D 

sorry had to be a little sarcastic there

-----------------------------------
Martin
Thu Oct 07, 2004 11:08 am


-----------------------------------
Damn it guys, that was coming next.

-----------------------------------
Martin
Thu Oct 07, 2004 11:26 am


-----------------------------------
2. Pre and Post Conditions
Pre and post conditions are simply a specific use of comments. The pre condition of a function is basically a statement of has to be true going into the function. The post condition of a function is what is true coming out of the function.

function divide (a, b : int) : real
   result a / b
end divide

So now we have to ask ourselves when this function will go wrong. The answer is, clearly, when b = 0, the program will crash and return a divide by zero error. What happens after the function is complete? It returns a/b. So, we put these facts in pre and post conditions:

function divide (a, b : int) : real
   %Pre: b is not equal to 0
   %Post: returns a divided by b
   result a / b
end divide

Now the problem with this example is that it is very obvious. Anyone who's used turing for a week could look at that and tell you what it does. The larger your function grows, the more difficult it becomes to figure out what exactly it does. Take this function, for example (in java, I didn't feel like translating it):

//pre: takes an ordered list of rowIndicies and colIndicies
	//post: returns the subMatrix with rows as numbered in rowIndices, and columns as numbered by colIndices, or null should there be a problem with rowIndices
	public Matrix subMatrix(int[] rowIndices, int[] colIndices) {
		int subrows=rowIndices.length;
		int subcols=colIndices.length;

		if (rowIndices[0]rows) {
			System.out.println("Row index out of bounds\n");
			return null;
		}
		for(int i=1; i