In the Java API, you have a file called Constants.java which contains all the String constants as given in the snippet example below. This is supposed to be used for all API parameter exchanges. The issue is that all these constants need to be declared as "final" for them to actually become constants in Java... i.e. they should all be declared as "public static final", not just as "public static" as they have been declared now. In reality all these variables are not treated as Constants at all by the JVM!!! There are multiple issues that arise out of it... 1. These constants cannot be used in Switch...Case statement, which is one of their primary purpose. 2. Since JVM treats these as variables rather than constants, they take up memory in the JVM eden memory pool which it wouldn't if they were constants, making any application using the API memory and computationally inefficient.
/** Exchanges. */ public static String EXCHANGE_NSE = "NSE"; public static String EXCHANGE_BSE = "BSE"; public static String EXCHANGE_NFO = "NFO"; public static String EXCHANGE_BFO = "BFO"; public static String EXCHANGE_MCX = "MCX"; public static String EXCHANGE_CDS = "CDS";
Thanks Sujith, Since we are looking at changes to Constants, here are further 2 suggestions which can further improve on it.... 1. Constants.java only has constants required as input to the API, and even on that I am not sure it is comprehensive. It would be very helpful if the response output constants from the API are also included. e.g OrderStatus {COMPLETE, REJECTED, CANCELLED, OPEN}
2. The more correct way to define these constants would be as an enumeration. It will avoid the scope of making any mistakes at all, will obviate the need for string parsing etc. You an even have a helper function in the enumeration as a getter... e.g.
public enum OrderStatus { COMPLETE, REJECTED, CANCELLED, OPEN; public static OrderStatus getStatus(String statusStr) { try { return OrderStatus.valueOf(statusStr); } catch (Exception e) { return OrderStatus.OPEN; // or you can throw this exception with a fail fast design pattern. } } }
We will add the order status to the constants as well. We may not be able to make Enum since there are many intermediate order statuses that can't be listed and one may not see a few statuses until certain circumstances. Since things are arbitrary we can't make it Enum.
Since we are looking at changes to Constants, here are further 2 suggestions which can further improve on it....
1. Constants.java only has constants required as input to the API, and even on that I am not sure it is comprehensive. It would be very helpful if the response output constants from the API are also included. e.g OrderStatus {COMPLETE, REJECTED, CANCELLED, OPEN}
2. The more correct way to define these constants would be as an enumeration. It will avoid the scope of making any mistakes at all, will obviate the need for string parsing etc. You an even have a helper function in the enumeration as a getter... e.g.
public enum OrderStatus {
COMPLETE, REJECTED, CANCELLED, OPEN;
public static OrderStatus getStatus(String statusStr) {
try {
return OrderStatus.valueOf(statusStr);
} catch (Exception e) {
return OrderStatus.OPEN;
// or you can throw this exception with a fail fast design pattern.
}
}
}