snippetjavaMinor
Showing a list of plugins to filter
Viewed 0 times
listpluginsshowingfilter
Problem
How can I refactor this function? I show a list of plugins in JSF, and I should filter them. I added function
I want to pay your attention this code interacts with JSF page:
filterPlugins but I have some questions:- Should we simplify block with conditional expression?
- I reassign global variable "plugins" to introduce my filter function (is it not bad practice?)
load()@NotNull
@DataModel("plugins")
List plugins;
public void load() {
plugins = pluginManager.getPlugins();
plugins = filterPlugins(searchParam);
SortingUtil.sort(plugins, SortingUtil.SortType.ID_ASC);
if (plugins.size() > 0) {
if (plugin != null && plugins.contains(plugin)) {
selectPlugin(plugin);
} else {
selectPlugin(plugins.get(0));
}
} else {
plugin = null;
}
}
@SuppressWarnings("unchecked")
private List filterPlugins(final String searchParam ){
return (List)Iterables.filter(plugins, new Predicate() {
@Override
public boolean apply(@Nullable Plugin plugin) {
return plugin.getName().contains(searchParam)
|| plugin.getNetworkClasses().contains(searchParam)
|| plugin.getClassName().contains(searchParam);
}
});
}
@Override
public void selectPlugin(Plugin p) {
plugin = p;
}Plugin.javapublic class Plugin {
private static final long serialVersionUID = -8424575107726996696L;
@NotNull
private Long id;
@NotNull
private String name;
@NotNull
private String networkClasses;
+ setters and getters
}I want to pay your attention this code interacts with JSF page:
Solution
Early Return
You can return early to reduce the nesting of your if statements:
Also note that I changed
Plugins Field
Code like this:
Can be a bit confusing. I would change it to:
And then change
plugin field
I would still like to see the declaration of
You can return early to reduce the nesting of your if statements:
if (plugins.isEmpty()) {
plugin = null;
return;
}Also note that I changed
plugins.size() > 0 to !plugins.isEmpty(), I think it's more readable.Plugins Field
Code like this:
plugins = pluginManager.getPlugins(); // assign field
plugins = filterPlugins(searchParam); // method expects plugins field to be assigned,
// but doesn't work on it but returns insteadCan be a bit confusing. I would change it to:
plugins = filterPlugins(pluginManager.getPlugins(), searchParam);And then change
filterPlugins to:private List filterPlugins(final List plugins, final String searchParam )plugin field
I would still like to see the declaration of
plugin as well as the selectPlugin method. As mentioned in the comments, it looks a bit like it might be bad design.Code Snippets
if (plugins.isEmpty()) {
plugin = null;
return;
}plugins = pluginManager.getPlugins(); // assign field
plugins = filterPlugins(searchParam); // method expects plugins field to be assigned,
// but doesn't work on it but returns insteadplugins = filterPlugins(pluginManager.getPlugins(), searchParam);private List<Plugin> filterPlugins(final List<Plugin> plugins, final String searchParam )Context
StackExchange Code Review Q#67326, answer score: 7
Revisions (0)
No revisions yet.