nomagic_style {roger}R Documentation

Validation of Magic Numbers

Description

Check the absence of magic numbers in code.

Usage

nomagic_style(srcData, ignore = c(-1, 0, 1, 2, 100),
              ignore.also = NULL)

Arguments

srcData

a list as returned by getSourceData.

ignore

vector of numbers not considered as magic.

ignore.also

vector of numbers not considered as magic in addition to those in ignore.

Details

Good coding practices dictate to avoid using “magic numbers” (unnamed or insufficiently documented numerical constants) in code. For example, in the expression y <- x + 42, 42 is a magic number.

When scanning the code for magic numbers, the following numerical constants are ignored:

“Simple” assignments serve to assign magic numbers to objects. Such expressions should contain at most three levels of sub-expressions and hold on a single line of code. The following expressions contain one, two and three levels of sub-expressions, respectively: MAX <- 4294967295, MAX <- 4294967296 - 1, MAX <- 2^32 - 1.

Value

Boolean. When FALSE, a message indicates the nature of the error and the faulty lines, and the returned value has the following attributes:

lines

faulty line numbers;

message

text of the error message.

Examples

## Keep parse data in non interactive sessions.
if (!interactive())
    op <- options(keep.source = TRUE)

fil <- tempfile(fileext = ".R")
cat("MAX <- 2^6 - 1",
    "MIN.INT <- 42L",
    "99 -> ERROR__",
    "size <- 42",
    "x <- rnorm(MAX)",
    "runif(123)",
    "x[1]",
    "x[1] * 7 + 2",
    "x[33]",
    "x * 100",
    "x <- numeric(0)",
    "y <- logical(5)",
    file = fil, sep = "\n")

## Default list of ignored magic numbers
nomagic_style(getSourceData(fil))

## Additional exceptions
nomagic_style(getSourceData(fil), ignore.also = c(5, 42))

[Package roger version 1.5-1 Index]