Module talk:Flags
Appearance
Code change
Hello I saw your module on it.wiki, and I would like to make some changes to the code:
- declare local all variables, they are more efficient and quick to access and you dont pollute global namespace
- Why do you use a for loop to check if a key is in the table like:
for flagParameter,commonsFile in pairs(master.twoLetter) do
if flagParameter == territory.args[1] then
commonsName = commonsFile
tempLink = commonsFile
end
end
is simpler and quicker try to assign and check for nil
commonsFile = master.twoLetter[territory.args[1]]
if commonsFile then --check for nil
commonsName = commonsFile
tempLink = commonsFile
end
3. Why check for if territory.args[2] ~= "{{{2}}}" then
? AFAIK a paremeter call will be resolved before call the module and substituded by its value.
I will upload my changes to the sandbox for test.--Moroboshi (talk) 11:14, 22 July 2013 (UTC)
- Thank you for the interest and the good advice! This is not only my first attempt with Lua programming, it is also my most serious attempt to program anything out of HTML/CSS. For instance, I wasn't aware that by not declaring variables explicitly local the module would pullute the global namespace (something that sounds obvious now that you say it).
- Your approach in 2) is definitely smarter and I'm happy to take it after testing that there are no side effects (I don't see why there should be any).
if territory.args[2] ~= "{{{2}}}" then
is the way I found to check whether a parameter for variants i.e. "1945" has been defined, after asking at mediawiki.org. Improvements welcome.- Now I comprend you check for a parameter not definited. I think its cleaner to set the parameter to
in the template and check for null string
""
in the module.--Moroboshi (talk) 18:01, 22 July 2013 (UTC)
- Now I comprend you check for a parameter not definited. I think its cleaner to set the parameter to