CodeSOD: Replacing a City
Mateus inherited some code where half the variables were named things like strAux1 and strAux2. It was a data driven application, and the data had some conventions that weren't always ideal; for example, every city also had an associated abbreviation stored in its name field, e.g., "SAO PAULO (SP)" and "RIO DE JANEIRO (RJ)".
The original developers wanted to be able to present the names of cities without the abbreviation, so they wrote this:
RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(UPPER(ISNULL(C.CITY,'')),'(RS)',''),'(SC)',''),'(PR)',''),'(SP)',''),'(MG)',''),'(RJ)',''),'(DF)',''),'(ES)',''),'(MS)',''),'(MT)',''),'(BA)',''),'(PI)',''),'(AL)',''),'(TO)',''),'(PE)',''),'(PA)',''),'(CE)',''),'(RN)',''),'(MA)',''),'(AP)',''),'(RR)',''),'(RN)',''),'(AM)',''),'(AC)',''),'(XX)',''),'(EX)',''),'(SE)',''),'(PB)',''),'(GO)',''),'(RO)',''))
Whitespace added for readability(?).
The good news is that I don't think Brazil is going to be adding any major cities deserving of their own abbreviation any time soon. But by effectively hard-coding the list of viable abbreviations instead of using more advanced string splitting (or *gasp* a regex), or y'know actually normalizing the data correctly, they've done a nice job making the code harder to maintain.
REPLACE REPLACE REPLACE REPLACE this code.
[Advertisement] ProGet's got you covered with security and access controls on your NuGet feeds. Learn more.