patternjavaMinor
Constructing a long string
Viewed 0 times
constructinglongstring
Problem
I have a task to construct a long properly formatted string in Java. This string will be then used as a header of the logfile that we write during the program execution. As the program runs on server continuously, this string may need to be returned via the overridden method more than once. I have used multiple
```
@Plugin(name = "property", category = StrLookup.CATEGORY)
public class CustomLookup extends AbstractLookup {
private static final String LINE_SEPARATOR = System
.getProperty("line.separator");
private static AtomicLong aLong = new AtomicLong(0);
private StringBuilder formattedString;
private StringBuilder javaPropsString;
private StringBuilder pathPropsString;
private StringBuilder osPropsString;
private StringBuilder otherPropesString;
// flag to determine if we already have formatted string ready
// as we don't want to create new string every-time.
private boolean isStringReady = false;
// flag to determine if environment variables are added to system
// properties.
private boolean isEnvPropsSet = false;
private Properties properties;
@Override
public String lookup(LogEvent event, String key) {
// if this is the first call (i.e. system startup) we
// don't log the system properties.
if (aLong.getAndIncrement() == 0)
return "";
// on any subsequent calls i.e. on file rollover
// we return the formatted string with system properties.
else if (key.equalsIgnoreCase("customKey")) {
prepareCustomLog();
return formattedString.toString();
} else
return "";
}
/**
* This method prepares the custom string in a readable format. It checks if
* the string is already prepared with the {@link #isStrin
StringBuilders and some flags to make sure I don't do anything repeatedly or unnecessarily. But I am still looking for ways to improve the performance of this code.```
@Plugin(name = "property", category = StrLookup.CATEGORY)
public class CustomLookup extends AbstractLookup {
private static final String LINE_SEPARATOR = System
.getProperty("line.separator");
private static AtomicLong aLong = new AtomicLong(0);
private StringBuilder formattedString;
private StringBuilder javaPropsString;
private StringBuilder pathPropsString;
private StringBuilder osPropsString;
private StringBuilder otherPropesString;
// flag to determine if we already have formatted string ready
// as we don't want to create new string every-time.
private boolean isStringReady = false;
// flag to determine if environment variables are added to system
// properties.
private boolean isEnvPropsSet = false;
private Properties properties;
@Override
public String lookup(LogEvent event, String key) {
// if this is the first call (i.e. system startup) we
// don't log the system properties.
if (aLong.getAndIncrement() == 0)
return "";
// on any subsequent calls i.e. on file rollover
// we return the formatted string with system properties.
else if (key.equalsIgnoreCase("customKey")) {
prepareCustomLog();
return formattedString.toString();
} else
return "";
}
/**
* This method prepares the custom string in a readable format. It checks if
* the string is already prepared with the {@link #isStrin
Solution
-
Comments should be used to describe why something is done, What is done should be described by the code itself using meaninful names for variables, parameters, methods and classes. If you need a comment to describe what the purpose of a variable is, you have named the variable not that good.
-
you shouldn't shorten variables names, because this will remove readability. Mr.Maintainer still needs to understand the code in 6 months at first glance.
here you need a comment to describe what for the variable is used. So a better name would be
Comments should be used to describe why something is done, What is done should be described by the code itself using meaninful names for variables, parameters, methods and classes. If you need a comment to describe what the purpose of a variable is, you have named the variable not that good.
-
you shouldn't shorten variables names, because this will remove readability. Mr.Maintainer still needs to understand the code in 6 months at first glance.
// flag to determine if environment variables are added to system
// properties.
private boolean isEnvPropsSet = false;here you need a comment to describe what for the variable is used. So a better name would be
areEnvironmentPropertiesSet. - for making your code less errorprone you should use braces
{}for singleif,elsestatements and also for single statements of loops.
Code Snippets
// flag to determine if environment variables are added to system
// properties.
private boolean isEnvPropsSet = false;Context
StackExchange Code Review Q#74172, answer score: 3
Revisions (0)
No revisions yet.