|
| 1 | +# R Project Coding Conventions for easystats |
| 2 | + |
| 3 | +This document outlines the common coding conventions observed in the `easystats` R projects. |
| 4 | + |
| 5 | +## File Naming |
| 6 | + |
| 7 | +* Files are named with `snake_case` and should correspond to the main function they contain (e.g., `check_normality.R`). |
| 8 | + |
| 9 | +## Package versioning |
| 10 | + |
| 11 | +* Package versions follow Semantic Versioning conventions. |
| 12 | + |
| 13 | +* If pull requests include user-visible changes, the "developer" version number should be increased (e.g. from 0.10.1.5 to 0.10.1.6). This ensures that `easystats::install_latest()` will download the latest versions. |
| 14 | + |
| 15 | +## Code Style & Formatting |
| 16 | + |
| 17 | +* **Assignment:** Use the `<-` operator for assignment, not `=`. |
| 18 | +* **Spacing:** |
| 19 | + * Use spaces around all infix operators (`<-`, `==`, `+`, etc.). |
| 20 | + * Place a space after a comma, but not before. |
| 21 | +* **Curly Braces:** |
| 22 | + * For function definitions, the opening `{` should be on its own line. |
| 23 | + * For `if`/`else` statements, the opening `{` can be on the same line. |
| 24 | +* **Line Length:** Keep lines to a reasonable length (e.g., under 80-100 characters) to improve readability. |
| 25 | + |
| 26 | +## Function Naming |
| 27 | + |
| 28 | +* **Public Functions:** Use lower case, underscore separated if more than one verb, i.e. `snake_case` (e.g., `check_normality()`, `model_performance()`). These functions should be exported. |
| 29 | +* **Internal Functions:** Prefix with a dot (`.`) and may use `snake_case` or `camelCase` (e.g., `.safe()`, `.get_BIC()`, `.check_normality()`). These functions should not be exported. |
| 30 | +* **Naming**: Common prefix for functions that focus on specific "tasks" or workflows (e.g. in package "insight", `get_*()` to get data, `find_*()` to find information, or in package "performance", `performance_*()` to compute measures of model quality, `check_*()` to check model assumptions...). |
| 31 | + |
| 32 | +## Argument names |
| 33 | + |
| 34 | +* Lower case, underscore separated if more than one verb. |
| 35 | +* Arguments that refer to plot or table aesthetics (like size or alpha of geoms) should follow the pattern `aesthetics_geomtype`, e.g. `size_point`, `color_line` or `alpha_rope`. |
| 36 | +* "easystats" uses the argument name `by` to indicate grouping, not `group_by` or `at`. |
| 37 | +* Use `select` and `exclude` to select columns/variables, and `keep` or `drop` to select rows/observations. |
| 38 | +* Handling NA values, especially removing missing values, is done with `remove_na`. |
| 39 | + |
| 40 | +## Element / Column names (for returned data frames) |
| 41 | + |
| 42 | +1) First letter of the column name is capital, unless (6) applies (*example:* `Parameter`). |
| 43 | +2) First letter of nouns is capital, unless (6) applies (*example:* `ROPE_Percentage`, `Prior_Scale`). |
| 44 | +3) Using underscore rather than camelCase to separate words (*example:* `CI_high`). |
| 45 | +4) Multiple words: common/main part first and adjective/specifier/variational part after, unless (8) applies (*example:* `Median_standardized`, `ROPE_percentage`). |
| 46 | +5) Abbreviations: all uppercase (*example:* `ESS`, `MCSE`, `ROPE`). |
| 47 | +6) Keep conventions for reserved words (*example:* `p`, `pd`, `Rhat`). |
| 48 | +7) Adjectives / verbs: all lower case, unless (1) applies (*example:* `high` or `low` in `CI_high` or `CI_low`). |
| 49 | +8) In case of multiple occurrences of column names that indicate the same measure or content (like `CI_low` or `SE`), the common part is appended as suffix to the context specific part (*example:* `CI_low` and `Eta2_partial_CI_low`, and **not** `CI_low` and `CI_low_Eta2_partial`). |
| 50 | +9) The "squared" term in column names that refers to "common" statistics (`Eta2`, `Chi2`, `Omega2`, ...) should be written as `2`, not `sq`, `squared` or `pétit-deux` (*example:* `Chi2`, and **not** `Chisq`, `Eta2`, and **not** `Eta_squared`). This rule does **not** apply to function names. |
| 51 | +10) Converting between "easystats" style and "broom" style can be done with `insight::standardize_names()`. |
| 52 | + |
| 53 | +## Documentation (roxygen2) |
| 54 | + |
| 55 | +All exported functions must be documented using `roxygen2`-style comments (`#'`). The documentation should include: |
| 56 | + |
| 57 | +* **Title:** A concise, one-line summary of what the function does. |
| 58 | +* **Description:** A more detailed paragraph explaining the function's purpose. |
| 59 | +* **`@param`:** A description for each function parameter. |
| 60 | +* **`@return`:** A description of the value the function returns. |
| 61 | +* **`@export`:** Tag to make the function available to users. |
| 62 | +* **`@examples` or `@examplesIf`:** Code demonstrating how to use the function. |
| 63 | +* **`@seealso`:** (Optional) Links to related functions. |
| 64 | +* **`@details`:** (Optional) Further details on the methodology or implementation. |
| 65 | + |
| 66 | +## Dependencies |
| 67 | + |
| 68 | +* Use base-R wherever possible (to reduce hard dependencies) |
| 69 | +* Make sure R-version requirements are not too strict |
| 70 | +* **Package Functions:** Always use the `::` operator to call functions from other packages (e.g., `stats::shapiro.test`, `insight::model_info`). Do not use `library()` or `require()` at the top of a file (no full import, only selective import of functions). |
| 71 | +* **Conditional Checks:** Use `insight::check_if_installed("pkg_name")` to check if a package is available before using it, especially for optional ("Suggests") dependencies. |
| 72 | + |
| 73 | +## S3 Object System |
| 74 | + |
| 75 | +* The projects make extensive use of the S3 object-oriented system. |
| 76 | +* **Generic Functions:** Define generic functions using `UseMethod("function_name")`. |
| 77 | +* **Methods:** Implement methods for specific object classes using the `function_name.class_name` naming convention (e.g., `check_normality.default`, `model_performance.lm`). |
| 78 | + |
| 79 | +## Error Handling and Messaging |
| 80 | + |
| 81 | +* Use `tryCatch` for operations that might fail. The internal `.safe()` helper is a good example. |
| 82 | +* Use the `insight` package's functions for user-facing messages: |
| 83 | + * `insight::format_error()` |
| 84 | + * `insight::format_warning()` |
| 85 | + * `insight::format_alert()` |
| 86 | + * `insight::print_color()` |
0 commit comments