Tuesday, March 24, 2015

Mindful selection of constant values (for uniqueness)

(I ran into this issue when trying to grok the Android Bluetooth API, and thought it was worthy of a post.)

Say you have a value and need to know the name of the constant. For example, you are debugging some code, and things would be much more obvious if you could log the name of a state returned by a method getState(), not just its value:

1
2
3
4
5
6
7
8
  // Possible device states
  public static final int STATE_IDLE = 0x00;
  public static final int STATE_STARTING = 0x01;
  public static final int STATE_RUNNING = 0x02;
  ...
  public int getState() {
    return mState;
  }

You could solve this with a lookup table. But if there are many constants, or the documentation is in the code, or new constants may be added later, or there are undocumented manufacturer additions, you can save a lot of code by using reflection to look up the names of the constants at runtime.

With reflection, you would obtain all the constants in the class, look for the constant whose value matches the one you got from getState(), and display the constant's name instead of its value.

But say that this code is also in the same class:

1
2
3
4
5
6
7
  // Possible power settings
  public static final int POWER_OFF = 0x00;
  public static final int POWER_ON = 0x01;
  ...
  public int getPower() {
    return mPower;
  }

While it may make sense to represent power settings with a 0 and 1, the reuse of these values breaks our ability to uniquely discover a constant's name using reflection, because one value now maps to two constants.

One approach to ensure uniqueness is to use a higher order byte to encode the information that the constant represents. In this example, hex 0x0a00 is added to STATE_ constants, and 0x0b00 is added to POWER_ constants:

1
2
3
4
5
6
  public final static int STATE_IDLE     = 0x0a00;
  public final static int STATE_STARTING = 0x0a01;
  public final static int STATE_RUNNING  = 0x0a02;

  public final static int DEVICE_OFF     = 0x0b00;
  public final static int DEVICE_ON      = 0x0b01;

Developers may not encounter the need for reflection in normal coding. But it can come in very handy during testing, or when you encounter a complex system like Bluetooth, and are trying to figure it out. There are going to be times where different constants must have the same value. But many times, values can be unique. Mindful consideration of constant values to permit discovery of their corresponding constant names through reflection may be appreciated by others who will eventually be interacting with your code.

9 comments:

  1. Being a blogger,I always think of making something which could help us in forming android studio code formatter and you have just made my life so easy!!

    ReplyDelete
  2. You can contact me for a peaceful time frame, a proper Escorts Service in Paharganj conference and even an occasion or a capacity; I will take a position right nearby and will complement and improve your area amongst the team. First of all hiring Call Girls in Paharganj does not mean you will get infected with immune diseases. Our Agency is a genuine and trusted escort agency and provides a call girl who is educated and well mannered.

    ReplyDelete
  3. The best thing about Escorts Service in Paharganj, Delhi agency is that we have not just a few college girls for you to choose from but a whole collection of erotic girls simply waiting to have sex with their clients and it would not be courteous to not fulfill a girl’s wishes, right? If you are concerned about how you the whole booking will be carried out and other questions about the mode of payment then you can call us at Call Girls in Paharganj, Delhi agency and one of our receptionists will be glad to help you out.

    ReplyDelete
  4. On the off chance that you are procuring an autonomous Aerocity Escorts, you need to manage that Call Girls in Paharganj itself. Brighter your life with attractive and venereal Escorts Service in Paharganj available to satisfy your lustful longings. Maybe you are searching for the high-class sexual pleasure in Aerocity. Hence give us a chance to serve you the best services according to your needs.

    ReplyDelete
  5. When it comes to hiring escorts, there are different kinds of ladies available from different parts of the world. However, there is something more than special about Escorts Service in Paharganj that compel men to visit Aerocity, again and again, to spend some sensual time with our naughty and sexiest Call Girls in Paharganj, near Indiara Gandhi International Airport, New Delhi.

    ReplyDelete
  6. The Escorts who work at Call Girls in Paharganj have a raised degree of guidance and are thusly essentially loosening up and capitalizing on their organization. There are many types of Escorts Service in Paharganj. Some of them are called centerfold, beauticians, lap dancers, and beauticians. As these agencies are out of various areas, you can choose one according to your liking.

    ReplyDelete
  7. To hire our Call Girls in Paharganj you can speak with our call girl representative over the call. It is very easy to get escort service in Aerocity now. Call us now!! They love to make relationships for the night with Indian men and that is why they join our escort agency to provide foreign Escorts Service in Paharganj. These ladies are referred to as Foreign escorts in Aerocity.

    ReplyDelete
  8. These bold and confident sexy girls from the house of Sexy Call Girls in Paharganj, Delhi also have some of the most delicious figures that often make our clients drool over them the moment they arrive at your doorstep! These hot and sexy girls are well in demand by our long list of loyal clientele because of the first class sexual services each one of these babes from the house of Escorts Service in Paharganj, Delhi render to their clients.

    ReplyDelete
  9. Call Girls in Paharganj, here are incredibly evolved and they appreciate the customer’s needs since this city has every sort of individual and the fight is there for everyone. Our escort knows this reality, so they give you 100% satisfaction guaranteed. We Provide best Escorts Service in Paharganj that will you never forget and come again and again. Our Agency maintain the quality and an immense collection which contains a range of categories of escort girls.

    ReplyDelete