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

feat: Improve code quality by minimizing nested code (e.g. if/else conditional blocks) #1424

Open
1 task done
Gitsarry opened this issue Aug 14, 2024 · 0 comments
Open
1 task done
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Gitsarry
Copy link
Contributor

I confirm this feature has not been previously requested

  • I have searched the issues and this feature has not previously been requested

Example of current nested code

function ignore_msrs_always() {
    # Make sure the host has /etc/modprobe.d
    if [ -d /etc/modprobe.d ]; then
        # Skip if ignore_msrs is already enabled, assumes initramfs has been rebuilt
        if grep -lq 'ignore_msrs=Y' /etc/modprobe.d/kvm-quickemu.conf >/dev/null 2>&1; then
            echo "options kvm ignore_msrs=Y" | sudo tee /etc/modprobe.d/kvm-quickemu.conf
            sudo update-initramfs -k all -u
        fi
    else
        echo "ERROR! /etc/modprobe.d was not found, I don't know how to configure this system."
        exit 1
    fi
}

Describe the solution you'd like: Exit early, so no else clause needed, code gets simpler and more easy to read

function ignore_msrs_always() {
    # Make sure the host has /etc/modprobe.d
    if [ ! -d /etc/modprobe.d ]; then
        echo "ERROR! /etc/modprobe.d was not found, I don't know how to configure this system."
        exit 1
    fi

    # Skip if ignore_msrs is already enabled, assumes initramfs has been rebuilt
    grep -lq 'ignore_msrs=Y' /etc/modprobe.d/kvm-quickemu.conf >/dev/null 2>&1 && return

    # if all conditions met, write file
    echo "options kvm ignore_msrs=Y" | sudo tee /etc/modprobe.d/kvm-quickemu.conf
    sudo update-initramfs -k all -u
}

See e.g. Computer Programming/Coding Style/Minimize nesting “Flat is better than nested”

@flexiondotorg flexiondotorg added enhancement New feature or request good first issue Good for newcomers labels Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants