Boolean vs boolean(s) as trilean switches

I’m developing an Android application and I just ran into something. I have some anonymous classes (event listeners). They are parameterized from the database. What I did is this:

buttonA.setOnTouchListener(new View.OnTouchListener() {
                        private Boolean isActive = null;
                        private boolean isTrigger;
                        private int onLevel;
                        private int offLevel;
                        private int chIdx;

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if (isActive == null) {
                                Cursor btnSettings = dbHelper.getButtonsTable().fetchButton(1, profileId, currentMode);
                                ...
...
                            }
                            return true;
                        }

Is it considered a good practice to use the Boolean object as a trilean switch (it has a null value if the listener haven’t been parameterized yet) or I should use two boolean variables…or maybe an integer?

Do you have and ideas?

Answer

Best yet, use an type (probably an enum) with accurate descriptions of the three states. Booleans don’t give a lot of information to the person who is calling the function (especially when used as a tristate).

public enum ActiveStatus {
  On,
  Off,
  Unknown
}

Leave a Reply

Your email address will not be published. Required fields are marked *