I know there are some posts talk about primitive obsession : When is primitive obsession not a code smell?, Is "avoid the yo-yo problem" a valid reason to allow the "primitive obsession"?, but the posts mostly talk about business data fields like "ZipCode" and "Address" (I can't define what "business object" is accurately, but I think it may mean the data source comes from database, user input or something need validation...), how about some non business data fields? For example:
public class MyActivity extends Activity{
private static boolean isFirstShown=false; //show welcome dialog when user enters this page first time
private static int lastSelectedItemIndex; //reload last selection
private int clickCount; //eg: show some Easter egg animations after some click
.
.
.
onResume(){
if(!MyActivity.isFirstShown){
//display welcome dialog
}
MyActivity.isFirstShown=true;
}
}
The data field above is not from database, do I need to create a type for them? I prefer to use primitive type for them because:
If there are many these fields, it may have many little classes and hence harder to find required file to edit in file menu
It may decrease the readability of the code, for example, I need to change something like:
if(!MyActivity.isFirstShown){
//display welcome dialog
}
to
if(!MyActivity.isFirstShownObject.getValue()){
//display welcome dialog
}
which has more characters and looks less natural to understand
Sometimes I rely on "whether it is primitive" to distinguish if it is a business object quickly, eg: if I see "int lastSelectedItemIndex", I can know it is not a field from database
I believe the scope of those fields are small, and the values are not from direct user input, I can handle it carefully so that the chance of having invalid values (eg:negative click counts) are small, less classes to yoyo can help everyone understand the program more easily, which the benefit outweighs the risk of having invalid values and interchanging fields (eg:lastSelectedItemIndex=click count).
Do I need to create types for those fields? It it necessary or "class obsession" (opposite to primitive obsession)?
getValue
, are you implying that every individual primitive should be wrapped in an object, to the point of never having a class that contains more than one primitive value?