Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stimulate to use early return #26

Open
ThierryO opened this issue Jul 2, 2021 · 1 comment
Open

stimulate to use early return #26

ThierryO opened this issue Jul 2, 2021 · 1 comment

Comments

@ThierryO
Copy link

ThierryO commented Jul 2, 2021

You can rewrite this as below. This makes the program flow easier to understand since the shortest branch is handled first and it end the function. Hence you don't need to write the second part as an else statement.

if (nrow(df_cleaned_data) == 0)) {
    warning("All observations are judged imprecise or suspected.")
    return(NULL)
}
df_with_cellcode <- assign_eea_cell(df = df_cleaned_data,
                                        longitude_colname = longitude_colname,
                                        latitude_colname = latitude_colname)
df_n_obs_cell <- get_n_obs_per_cell(df = df_with_cellcode)
df_cells_with_obs <- cells_with_obs(grid_cells = grid_cells,
                                        n_obs_per_cell = df_n_obs_cell)
visualize_obs_cells(sf_df = df_cells_with_obs,
                        species = species,
                        year = year,
                        palette = palette,
                        fill_color_opacity = fill_color_opacity)

if (nrow(df_cleaned_data > 0)) {

In this case I'd rather throw an error than a warning.

assert_that(nrow(df_cleaned_data) > 0, msg = "All observations are judged imprecise or suspected.")
@florisvdh
Copy link
Member

florisvdh commented Jul 2, 2021

If you'd like NULL to not appear in the console, you can return it invisibly.

dummy_function <- function() {
    cat("Some side effect.")
    return(invisible(NULL))
}
dummy_function()
#> Some side effect.

Created on 2021-07-02 by the reprex package (v2.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants