Tip number 1 for future Alessandro

· 1 minute read

This is a public service announcement: I got burned by a coding mistake that I failed to notice in a ~250 lines function, I am writing this down for future Alessandro to remember what happens (and some public shaming too, I guess that is well deserved here). And maybe it’ll be helpful to someone else, who knows!

Here comes the question:

What if you forget to assign a value to a variable which gets printed in the middle of a function being executed?

The answer is: absolutely nothing, and you get burned if you don’t notice it.

Let’s have a quick example:

1
2
3
4
5
burn <- function() {
  x <- 1
  x + 1
  x
}

Can you tell what value will fun() return when called? If you do, hats off: I could guess, but I wasn’t so sure and I had to check it out. If you don’t, here is the answer:

1
burn()
## [1] 1

The missed assignment is lost in the ether, and nothing is printed when executing x + 1 as all prints are suppressed by default within a function.

Future Alessandro: if you read this, you let me down. How could you repeat the same silly mistake and end up here all over again?