Article 6J3C1 CodeSOD: Replacing a City

CodeSOD: Replacing a City

by
Remy Porter
from The Daily WTF on (#6J3C1)

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.

proget-icon.png [Advertisement] ProGet's got you covered with security and access controls on your NuGet feeds. Learn more.
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments