Skip to content

stimulate to use early return #26

Description

@ThierryO

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.")

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