Aside from saving keystrokes, we also see benefits when we work with other developers. Take for instance the situation where two developers are working on the same project and define a class named “Location”. Both of these developers are unaware that the other is using the same name for a class. To further complicate the matter, developer #1’s Location class is used to define latitude/longitude information, while developer #2’s Location class is used to define street address, city, and zip data. When the two developers combine their code into the final project, chaos will ensue as the interpreter or compiler will not know which definition of Location to use. This is called a namespace collision and happens frequently when developers are working on problems with similar language semantics or abstractions.
Here’s how we fix it in a language like Java:
// File 1: import com.urbaninfluence.GeoLocate.Location;
Location myLoc1 = new Location(); // refers to GeoLocate.Location
// File 2: import com.urbaninfluence.Models.Store.Location;
Location myLoc2 = new Location(); // refers to Models.Store.Location