As developers we do not want to hard code values in our source code.
And defining these values as constants does help to appease the static analyzer, but the value is still hard coded.
For a Java developer, any change to the value requires to recompile the sources. And not only the source code with the declaration of the constant, but every source file that references this constant. Changing the value of a public constant is usually a bad idea. So the constant is not a property, since it should not be changed at development time and cannot be changed during runtime.
Happy Hacking with Properties
To work around this, a Java developer may use Properties. So here is the quickhack:
Additionally a properties file named
app.properties is supplied within the classpath of the project to make this code work.
So why is this a quickhack?
- The properties file is part of the classpath. Therefore the code does not have to be recompiled, but repackaged. The problem is that in this example the configuration is shipped with your code. You can easily work around this, if you provide your configuration in its own JAR. This is easily overlooked, since many Java Standards demand that the configuration is packaged with the code (e.g. te
web.xmlin a WAR file).
- The properties are easily managed by the developer, who edits the value and saves it to the source code management system. This is a pretty good idea, since this way the property value changes can be tracked and different versions of configuration can be compared. But configuration values are not so easily edited by the operations team that has to open the properties file from within JAR file with their text editors. It depends on your use case, if this is a problem.
- The property may be changed during runtime in the in-memory representation of the properties, but it may be challenge to persist such a change.
- You find no error handling in this code. What if the property or the whole properties file is missing? Is it ok to return 0 as default?
- Bad traceability: in case the property value is not an integer, an exception is raised. Further work should be invested to tell which property has a wrong type of value. What is missing is a proper value conversion. And in the code above there is just the conversion to integer. What about the other primitive types? What about URIs or even more complex types?
- But even if the value can be parsed as an integer, there may be constraints on the value. Is a negative value for an iteration count really allowed? What is missing is a proper validation of the value.
- Where is the validation code put? Crafting it manually requires some form of integration pattern, maybe boilder plate code. If you are implementing a business function, you surely do not want to bother with such basic tasks.
- This code has to be tested. On the bottom line code and configuration is the same. A configuration is an externalized, configurable piece of code.
- A configuration file is part of the public interface. We mentioned above, that values passed in have to be converted and validated. What is still missing is a documentation that tells users of the system which values are allowed and what default values are provided. It should also tell which properties are mandatory and which optional. Which are read only and which can be changed at runtime. And there is even more metadata. This documentation has to be kept in sync with the implementation. The number of iterations may be limited to a maximum number in future.
- And in the end we added another magic value: The name of the property. This has to be added as a constant as we cannot add the key to a properties file. So again it needs to be decided where these property keys should be defined.
- A developer should not bother with the administration of properties. Neither where to put them, nor how to load them. Handling properties is an architectural decision. There should be a documented way on how to deal with properties in a given project. Everyone will happily adhere to these rules instead of inventing the wheel over and over again. Not only within a project, but also from project to project.
Properties class is basically a hashtable with load and store functions. There are certainly use cases for
Properties, but for many more use cases this Java class provides too basic support for application developers.
Some Elaboration with Preferences
- The user of this class does not bother where to pick the property values from. This is handled by an implementation that usually extends AbstractPreferences and is registered via a PreferencesFactory. Java also provides a default implementation (that may or may not suit your needs).
- If you need it, there is support for user and system properties.
- There is even an event mechanism that allows to register code to be notified of value changes.
- Value changes are stored transparently by the implementation.
- There is a conversion support for all basic types.
So this class cures some of the problems above, but not all. The developer still has to care for:
- Basic Testing
- Key Handling
- Handling complex types
If you do not want to invest in providing a technical basis for handling properties in your application, you may have a look at a configuration library. There are many open source versions on the internet. smartics Properties is one of them. Please note that this library is currently in an early version.