Debugging Functions in Udemy Coding Lectures
Debugging Functions in Udemy Coding Lectures
There are three types of errors when we code in R Udemy System: Logic, Syntax and Structural
errors. In this file we will learn how we can debug the three. This document is valid for every
coding exercise, except the Functions coding exercise where we will create our own
functions.
Our example exercise will be:
Logic Errors:
- Logic errors are examples where the solution developed doesn’t match what is asked on the
question. This happens for a multitude of reasons and is completely part of the learning process.
- As an example, the exercise asks us for a vector with the elements 1,2,3,4 – imagine we would
provide this solution:
The solution states that our result[[1]] – meaning the first exercise (the 1 states the exercise
where we have the error) is not equal to what is expected. Let’s extend our exercise to two
questions:
- Now let’s correct our first exercise and make a Logic mistake on exercise 2:
- Note that our error now refers result[[2]] – which means that our exercise 2 had some kind of
trouble.
- Bottom line, with logic errors, you can guide yourself through Udemy feedback system and use
the part between [[]] to understand which exercise you are failing – the first exercise that you fail
will be the one in the output.
- The second part of the all.equal helps to guide you on the right solution – notice that this test
expects that the resulting vector on exercise 2 is c(2,4,6,8), hence, our vector multiplied by 2. You
can guide yourself using the second part of the all.equal function where you will have what is
expected from our exercise output.
Syntax Errors:
- Syntax errors are trickier. Udemy’s feedback is not perfect in providing good feedback when you
make a syntax mistake – particularly in the color syntax – sometimes if you go around the
function and make a syntax mistake adding something accidentally you will have a multitude of
errors in the platform, as an example – if we add an extra parenthesis on our vector function:
- If you find it difficult to deal with the Udemy feedback system, just copy your code and paste it
in R Studio – and execute it:
- The red colored lines help a lot in guiding you to the error and debugging the function
- It’s important that your code is enclosed between these two blocks of code– this is the
function that produces the outputs that will be fed to the test so don’t change these lines.
- Let’s see an example of what happens when we delete the function definition (don’t worry, this
will be much clearer and will make a lot of sense when we get to the Functions section ) – this
is the code we submit on Udemy, without the function definition:
- To avoid these types of errors don’t delete the last line of the function or the first function
definition. Only change the lines of code that have variables with 0’s assigned to them.
Again, thank you for enrolling in my course and if you have additional trouble, message me on LinkedIn
– Congratulations for having a go at the coding exercises – this is a super important part of the process!