ninjaframework / ninja

Ninja is a full stack web framework for Java. Rock solid, fast and super productive.
http://www.ninjaframework.org
Apache License 2.0
1.91k stars 521 forks source link

Code optimization suggestions #749

Open noobyu6 opened 2 years ago

noobyu6 commented 2 years ago

Hi,

I find that the private field metricsService at Line 36 in the file 'ninja/ninja-metrics/src/main/java/ninja/metrics/InstrumentedCache.java' on the develop branch is only assigned and used in the method InstrumentedCache. Therefore, this field can be removed from the class, and become a local variable in the method InstrumentedCache. Besides, for method init, the field metricsService can be converted to an input parameter.This transformation will normally reduce memory usage and improve readability of your code.I will be happy if this transformation is helpful.

private MetricsService metricsService; // line 36 this field can be replaced by local variable

InstrumentedCache(Cache cache, MetricsService metricsService) {
        this.underlyingCache = cache;
    // MetricsService metricsService = metricsService;
    this.metricsService = metricsService;
    // init(metricsService);
    init();
}
// private void init(MetricsService metricsService)
private void init() {
     MetricRegistry registry = metricsService.getMetricRegistry();

     hitCounter = registry.counter("ninja.cache.hits");
     missCounter = registry.counter("ninja.cache.miss");
}