Java Coding Style – Public Member Variables in Mobile Platforms

androidcoding-stylejavamobile

We performed a code review recently of mobile application Java code that was developed by an outside contractor and noticed that all of the domain objects / data transfer objects are written in this style:

public class Category {
    public String name;
    public int id;
    public String description;
    public int parentId;
}

public class EmergencyContact {
    public long id;
    public RelationshipType relationshipType;
    public String medicalProviderType;
    public Contact contact;
    public String otherPhone;
    public String notes;
    public PersonName personName;
}

Of course, these members are then accessed directly everywhere else in the code. When we asked about this, the developers told us that this is a customary performance enhancement design pattern that is used on mobile platforms, because mobile devices are resource-limited environments. It doesn't seem to make sense; accessing private members via public getters/setters doesn't seem like it could add much overhead. And the added benefits of encapsulation seem to outweigh the benefits of this coding style.

Is this generally true? Is this something that is normally done on mobile platforms for the reasons given above? All feedback welcome and appreciated –

Best Answer

the developers told us that this is a customary performance enhancement design pattern that is used on mobile platforms, because mobile devices are resource-limited environments

Assuming that authors did not bother to provide authoritative references to justify their "design decision", you are pretty safe to consider this a brain-fart of underqualified coders.

I've been in mobile Java racket for several years (still having 2 or 3K SO reputation in related tags because I use it as the way to not forget the basics), reviewed and wrote plenty of code, studied multiple specifications, tutorials and style guides - none of these ever mentioned even a shade of difference this kind design could possibly have compared to Java SE. If you're interested in finding such tutorials and guides yourself, check SO tag wikis in relevant tags like java-me, midp etc.

Related Topic