Hello,
I recently stumbled over various situations where I wished that I had some kind of macros like I’m used to from C++ times. When I enter the title of this thread and follow the first few links I’m not finding a good solution. I hoped to find a plugin for an IDE (jME SDK / NetBeans) and also did a quick search for that - but no result. I also must admit that I’m not a NetBeans expert (only recently learned several refactoring options and the regex search - which is really great).
Maybe someone here has a hint?
I currently need this for these two things:
thing #1 "DEBUG mode"
solution for now: “DebugConfig” is my custom singleton class for all my libraries
if( DebugConfig.get().isDebugging() ) { /* Debug logic */ };
or
if( DebugConfig.get().isDebugging( Package forThisPackage ) { /* Debug logic */ };
or
if( DebugConfig.instance.debugOn ) { /* Debug logic */ };
The third alternative is the fastest (no function calls, but perhaps access to a distant memory location everytime this static ‘instance’ variable is accessed). Also the ‘instance’ variable can not be final because I want the user to be able to give a custom DebugConfig subclass for the ‘instance’ variable - which might prevent further JIT compiler optimizations.
The “Debug logic” mainly is there to check data for validity and to write some human readable output to the logger if any check fails. This code will not be needed when the Debug phase is over.
thing #2 "Code duplication / redundancy"
Here is an example:
@Override
public String generateName(String inputString, CanGetProperty propertiesSource)
{
if(inputString == null) return null;
if(propertiesSource == null) return null;
String result = civMode.makeCaseInsensitive(inputString);
if(!quickCheck(result))
return result;
return generateNameStep(result, propertiesSource);
}
@Override
public String generateName(String inputString, CanGetProperty... propertiesSources)
{
if(inputString == null) return null;
if(propertiesSources == null) return null;
String result = civMode.makeCaseInsensitive(inputString);
for(CanGetProperty propertiesSource : propertiesSources)
{
if(!quickCheck(result))
return result;
result = generateNameStep(result, propertiesSource);
}
return result;
}
@Override
public String generateName(String inputString, Set<CanGetProperty> propertiesSources)
{
if(inputString == null) return null;
if(propertiesSources == null) return null;
String result = civMode.makeCaseInsensitive(inputString);
for(CanGetProperty propertiesSource : propertiesSources)
{
if(!quickCheck(result))
return result;
result = generateNameStep(result, propertiesSource);
}
return result;
}
@Override
public String generateName(String inputString, Iterable<CanGetProperty> propertiesSources)
{
if(inputString == null) return null;
if(propertiesSources == null) return null;
String result = civMode.makeCaseInsensitive(inputString);
for(CanGetProperty propertiesSource : propertiesSources)
{
if(!quickCheck(result))
return result;
result = generateNameStep(result, propertiesSource);
}
return result;
}
The code in the second, third and fourth method is identical. I could make an array or Set and then just call one of these methods and the other two just relay to that single implementation, but then I will need to create a temporary container (array or Set) and fill it - which costs some time. I used this technique in other places and just lived with it. Usually I don’t write code twice and live with a moderate performance loss, but maybe this is not needed?
Well, maybe someone has a good idea or knows a NetBeans plugin or other trick which is perfect to deal with such situations? :chimpanzee_sad: