Tuesday, June 22, 2010

One Var To Rule Them All

As I was watching a great Tech Session video from ESRI's last Developer Summit on the JavaScript API, I was introduced to the concept of global variables being evil. Further research confirmed that I needed to alter my coding patterns a little bit. Since then I have tried to keep global variables to a minimum. If I do need them I have started storing them within a single variable like this:
// global variable var g = {}; g.baseURL = "http://gis.sandy.utah.gov/ArcGIS/rest/services/"; g.googleBaseURL = g.baseURL + "Public_Works/Google_Basemap/MapServer"; g.bwURL = g.baseURL + "Public_Works/BulkWasteAreas/MapServer"; g.vectorQueryURL = g.baseURL + "Public_Works/Vector_Basemap/MapServer/6"; g.locatorURL = g.baseURL + "Sandy_Address_Locator/GeocodeServer"; g.fldBW_DATE = "BW_DATE"; g.fldAREA_NUM = "AREA_NUM";

This has kept my code a little cleaner and has made it far easier to find the evil things during run-time with FireBug. My latest project with bulk waste data was my first attempt at global variable redemption.

No comments:

Post a Comment