Skip to content

Avoid significant logic inside of for loops #19

@cmatKhan

Description

@cmatKhan

OK -- not quite accurate, since using map or apply is just a for loop under the hood.

But, the point is that for loops that have significant logic in them are difficult to debug.

What is better is to take a page from the book of functional programming and write the
logic which executes on any given iteration of the loop into a function (or a number of functions)
and then apply that function(s) over the input list.

So, plot_metrics, rather than this:

 for (i in 1:length(metric_dfs_by_net[[1]])) {
    metric_dfs[[i]] <- do.call(cbind, lapply(unname(metric_dfs_by_net), "[[", i))
  }

  # assemble y-axis labels
  to_plot <- c(sum, pe
  ... # it goes on for many lines ....

write the logic inside of that loop into a function

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions