Refactor If / Elseif
Asked Answered
E

0

6

I have a function with a lot of if/elseif statements. I'm looking for a way to improve the code of this function, replacing IF/ELSEIF. I`m trying to use dictionaries, but I am not sure if it is the best way. Improve readability is important as well.

if lowestDim == "Campaign" || lowestDim == "Adgroup"
    if campaignType != "PERFORMANCE_MAX"
        drillThroughMap["DRILLLINK5"] = Dict(
            "filters" => lowestDim == "Campaign" ? Dict([
                SQLFilter("CAMPAIGN", "=", onlyCampaign), SQLFilter("SOURCE", "=", source)]) : Dict([
                    SQLFilter("CAMPAIGN", "=", onlyCampaign), SQLFilter("SOURCE", "=", source), SQLFilter("ADGROUP", "=", adgroup)]),
            "drillTo" => demographicDashboard
        )
    end
    drillThroughMap["DRILLLINK10"] = Dict(
        "filters" => Dict(),
        "drillTo" => campaignComparisonDashboard
    )
end

if presetDateRange != "Last 12+ Months" && (lowestDim != "Keyword" ) && !(source == "google" && (campaignType == "SMART" || campaignType == "PERFORMANCE_MAX"))
    drillThroughMap["DRILLLINK6"] = Dict(
        "filters" => Dict([SQLFilter("CAMPAIGN_FROM_DIAGNOSIS", "=", lowestDim == "Campaign" ? campaignStr : onlyCampaign * " ·· " * source)]),
        "drillTo" => adgroupComparisonDashboard
    )
end

if  presetDateRange != "Last 12+ Months" && lowestDim != "Campaign" && (campaignType == "SEARCH")
    drillThroughMap["DRILLLINK7"] = Dict(
        "filters" => Dict([SQLFilter("ADGROUP_FROM_DIAGNOSIS", "=", adgroup * " ·· " * onlyCampaign * " ·· " * source)]),
        "drillTo" => keywordComparisonDashboard
    )
elseif presetDateRange != "Last 12+ Months" && lowestDim == "Campaign" && (campaignType == "SEARCH")
    drillThroughMap["DRILLLINK7"] = Dict(
        "filters" => Dict([SQLFilter("CAMPAIGN_FROM_DIAGNOSIS", "=", onlyCampaign * " ·· " * source)]),
        "drillTo" => keywordComparisonDashboard
    )
end

Just a piece of the function code.

Eudy answered 22/11, 2022 at 13:5 Comment(6)
Looks pretty tame to me. How long does it go on for? Perhaps splitting each block into a separate function with a specific name for the condition treated in it.Halm
It is better to yse Symbols (i.e. :filters, :drillTo) as Dict keys, as they are hashed much faster than strings.Halm
Perhaps kmsquire.github.io/Match.jl/latest can help if you put the query items in a data structure of some kind.Lylalyle
@Eudy are you able to share the actual data? There's only one nested if statement, the rest are unnested, so without seeing the data, it seems quite hard to make it any simpler. Some things to try are: reducing the number of checks (i.e. is checking one column okay when you've checked two?), reducing the length of variables such as "ADGROUP_FROM_DIAGNOSIS", and/or moving the dicts somewhere else and/or into a dataframeAmmonium
For the pairing of filters and drillto, a Named Tuple like (filters = ... , drillto = ...) or a struct would use much less memory than a Dict like Dict(:filters => ..., :drillTo => ...)Nl
(Match.jl moved: juliaservices.github.io/Match.jl/latest )Nl

© 2022 - 2024 — McMap. All rights reserved.