1

I'm looking for a way to prevent developers from using some methods that are causing instability (Performance) issues for the site; for example, I have a method fetches the information from the database (e.g. getData(String id)), and another method fetches the information form the in-memory cache (e.g. getCachedData(String id)). The getData(String id) method is used in getCachedData(String id) if and only if we have a cache-miss. How can I prevent developers from using getData(String id) method? in other words, I need to force developers to use getCachedData(String id) always.

Please note that the access modifiers (private, protected, etc.), code design, or java docs are not really preventing developers. Do we have a final solution for this issue?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Ahmad AlMughrabi
  • 1,612
  • 17
  • 28
  • Possible duplicate of [How to prevent public methods from being called from specific classes](https://stackoverflow.com/questions/5486797/how-to-prevent-public-methods-from-being-called-from-specific-classes) – Bagus Tesa Aug 26 '19 at 09:33
  • 5
    "access modifiers (**private**, protected, etc.), are not really preventing developers". How do they bypass it? Are they using reflection?! –  Aug 26 '19 at 09:34
  • Nope, these methods are distributed in different classes. The problem, there are many system architects worked on the same platform, everyone created his own utility to read from the cache, currently, I have 5 different implementations that do the same functionality (some of them w/ cache and others w/ no cache), cleaning up all of them will take an extremely long time. – Ahmad AlMughrabi Aug 26 '19 at 09:35
  • @dyukha, they are able to change the code, as we have an API w/ no docs, the simplest approach is to look the SQL queries and see the closest method/implementation for it. The problem that we are not really able to review hundreds of line of code every day. – Ahmad AlMughrabi Aug 26 '19 at 09:39
  • 3
    @AhmadAlMughrabi, even if you block the particular method. devs could just slap a raw query themselves. it can be mitigated with some automatic code-inspector as suggested below. but if someone creates a new libary, publish it to maven for instance. then use it. you wont know unless you review the code manually. its more like, need to educate the devs. – Bagus Tesa Aug 26 '19 at 09:49
  • 1
    "**final** solution": eliminate the "risky methods". Also "5 different implementations that do the same functionality" is obviously not desired.... and, most important, as Bagus already commented: educate the devs! – user85421 Aug 26 '19 at 09:56
  • @bagusTesa, I agree with you. I see https://stackoverflow.com/questions/57655221/how-to-prevent-developers-from-use-risky-methods#comment101760790_57655308 is more relevant. – Ahmad AlMughrabi Aug 26 '19 at 10:23

1 Answers1

4
  1. Document the methods well, and explain which is preferable.
  2. Write a custom annotation to produce a compilation warning.
  3. Decorate the class, so getData would return getCachedData.

The interface is pretty unclear, there shouldn't be a method getCachedData at all. getData returns either the cached data or newly-fetched data.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • Thanks, @Andrew - the problem, there are many system architects worked on the same platform, everyone created his own utility to read from the cache, currently, I have 5 different implementations that do the same functionality (some of them w/ cache and others w/ no cache), cleaning up all of them will take an extremely long time. I need a short-term solution that at least keep the production safe. – Ahmad AlMughrabi Aug 26 '19 at 09:50
  • but how do the 5 implementations fetch the data? different sql implementation? or all of them call getData() at the end? – khadrawy Aug 26 '19 at 09:57
  • 4
    @AhmadAlMughrabi You can't really "force" someone to use a particular method if they have full access to the codebase and, thus, may change anything. A good tactic would be to introduce pull requests to the team and review them on the presence of the method calls you would like to avoid. – Andrew Tobilko Aug 26 '19 at 09:57
  • @andrewTobilko, Yeah, it looks like there is no option except training them. – Ahmad AlMughrabi Aug 26 '19 at 10:26