Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Reducing repeated code for optimal readability?

Asked by 4 years ago

I have this code:

function ToggleInventory()
    if InventoryOpen == false then
        if CraftingOpen then
            ToggleCrafting()
        elseif TribeOpen then
            ToggleTribe()
        end
        UpdateInventory()
        UI.Topbar.InventoryButton.Image = "rbxgameasset://Images/backpackactive"
        Blur.Enabled = true
        UI.Inventory.Visible = true
        InventoryOpen = true
    else
        UI.Topbar.InventoryButton.Image = "rbxgameasset://Images/backpack (5)"
        Blur.Enabled = false
        UI.Inventory.Visible = false
        InventoryOpen = false
    end
end

function ToggleCrafting()
    if CraftingOpen == false then
        if InventoryOpen then
            ToggleInventory()
        elseif TribeOpen then
            ToggleTribe()
        end
        UpdateCrafting()
        UI.Topbar.CraftingButton.Image = "rbxgameasset://Images/craftactive"
        Blur.Enabled = true
        UI.Crafting.Visible = true
        CraftingOpen = true
    else
        UI.Topbar.CraftingButton.Image = "rbxgameasset://Images/craft"
        Blur.Enabled = false
        UI.Crafting.Visible = false
        CraftingOpen = false
    end
end

function ToggleTribe()
    if TribeOpen == false then
        if InventoryOpen then
            ToggleInventory()
        elseif CraftingOpen then
            ToggleCrafting()
        end
        UI.Topbar.TribeButton.Image = "rbxgameasset://Images/tribeactive"
        Blur.Enabled = true
        TribeOpen = true
    else
        UI.Topbar.TribeButton.Image = "rbxgameasset://Images/tribe"
        Blur.Enabled = false
        TribeOpen = false
    end
end

There is a lot of repeating and I was wondering if there is a more code efficient way of writing this, avoiding repeat lines (if possible)

I think string manipulation such as (for example); UI.Topbar:FindFirstChild(CurrentUI.."Button")... may be useful

0
I don't think it's that bad personally, you have things enclosed in functions that do specific things. If we try to optimize we might be able to get the line count down but we might actually make it less readable. CeramicTile 847 — 4y
0
Loss in readability is okay shadow7692 69 — 4y

Answer this question