r3bl-org/r3bl-open-core

[style-guide] Add section to code `STYLE_GUIDE.md` to favor using enums

Closed this issue · 1 comments

Contributor guide in r3bl-open-core repo

Make sure to update the https://github.com/r3bl-org/r3bl-open-core/blob/main/CONTRIBUTING.md document as well to reflect the changes below. Merge them so there is no duplication. Provide a link from one to the other.

Style guide in guidelines repo

We have a file called STYLE_GUIDE.md in r3bl-org/guidelines repo.

Add a section about favor using enums over booleans.

# Favor enums instead of booleans

1. Enums are more expressive: Boolean variables can only have two values, true and false. This can be limiting, especially when you are dealing with more than two possible states. For example, if you are tracking the status of a file, you might want to use an enum with values such as Open, Closed, Saved, and Unsaved. This makes it much clearer what the possible states of the file are, and it can also make the code more self-documenting.

2. Enums are easier to match on: When you are working with boolean variables, you often need to use a series of if-else statements to check the value of the variable and execute the appropriate code. This can make the code difficult to read and maintain. With enums, you can use a match expression to match on the value of the enum and execute the appropriate code. This can make the code more readable and easier to maintain.

3. Enums are safer: Boolean variables can be easily misused. For example, if you have a variable that is called isSaved, you might accidentally assign the value false to it when you mean to assign the value true. This can lead to bugs in your code. With enums, you are less likely to make these kinds of mistakes because the values of the enum are named.

4. Enums are more future-proof: If you are using boolean variables, and you later decide that you need more than two possible states, you will need to refactor your code. This can be time-consuming and error-prone. With enums, you can simply add a new value to the enum. This is much easier and less error-prone.

5. Enums make code more self-documenting: When you use enums, the possible states of a variable are explicitly declared in the code. This makes the code more self-documenting and easier to understand for other developers.

6. Enums can improve compiler warnings: The Rust compiler can provide more helpful warnings when you use enums instead of booleans. For example, if you try to use an enum value in a context where it is not expected, the compiler will warn you about this error.

In general, enums are a more powerful and expressive way to represent states than boolean variables. They can make your code more readable, maintainable, safer, and future-proof.

Here is an example of how to use an enum to represent the status of a file:

```Rust
enum FileStatus {
    Open,
    Closed,
    Saved,
    Unsaved,
}

You can then use this enum to track the status of a file:

let mut file = File::new();

file.open();

match file.status() {
    FileStatus::Open => println!("The file is open."),
    FileStatus::Closed => println!("The file is closed."),
    FileStatus::Saved => println!("The file is saved."),
    FileStatus::Unsaved => println!("The file is unsaved."),
}

#247 closed this issue