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.
:filters
,:drillTo
) as Dict keys, as they are hashed much faster than strings. – Halm(filters = ... , drillto = ...)
or a struct would use much less memory than a Dict likeDict(:filters => ..., :drillTo => ...)
– Nl