Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 280
Adam Arold Avatar asked Nov 29 '22 16:11

Adam Arold


2 Answers

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
}
like image 132
Jeff Foster Avatar answered Dec 04 '22 00:12

Jeff Foster


I would say either use Boolean with true, false and null or use an enum. I tend to use a Boolean being null as a kind of "don't know yet". If you are using null as something more meaningful than "don't know" you are probably semantically better off going with an enum.

like image 33
Spycho Avatar answered Dec 04 '22 01:12

Spycho