我想知道我能做些什么才能让它更具可读性和清洁性。通过可读性,我的意思是更容易为其他开发人员阅读。
我真的不想两次使用相同的代码。我想我可以制作一些方法或方法来缩短它,但我不确定......
@Override
public void dispatchEvent(Event event) {
checkNotNull(event);
CancellableEvent cancellableEvent = null;
boolean cancellable;
if (cancellable = event instanceof CancellableEvent) {
cancellableEvent = (CancellableEvent) event;
checkArgument(cancellableEvent.isCancelled());
}
// Ignore-cancellation event handlers will run
for (EventPriority priority : EventPriority.values()) {
Map<Method, EventListener> internalMapping = getRegistry().getMethodMap(event.getClass(), priority, true);
if (internalMapping != null) {
for (Entry<Method, EventListener> entry : internalMapping.entrySet()) {
try {
entry.getKey().invoke(entry.getValue(), event);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
/*
* Delegate any exceptions that occur from
* the method to a runtime exception.
*/
throw new RuntimeException(e);
}
}
}
}
// Event handlers that consider cancellation will run
for (EventPriority priority : EventPriority.values()) {
Map<Method, EventListener> internalMapping = getRegistry().getMethodMap(event.getClass(), priority, false);
if (internalMapping != null) {
for (Entry<Method, EventListener> entry : internalMapping.entrySet()) {
try {
entry.getKey().invoke(entry.getValue(), event);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
/*
* Delegate any exceptions that occur from
* the method to a runtime exception.
*/
throw new RuntimeException(e);
}
// Immediately return in the case of the event being cancelled.
if (cancellable && cancellableEvent.isCancelled()) {
return;
}
}
}
}
}
答案 0 :(得分:3)
如果您正在讨论异常,那么在Java 7中您可以加入异常。
以下是关于Working with Java7 Exception
的文章} catch (ParseException | IOException exception) {
// handle I/O problems.
}
关于迭代,您可以使用单独的方法来调用功能。
答案 1 :(得分:2)
有什么建议让一些代码更具可读性?很长很好的代码之一是很好的和干净的代码:类应该尽可能小,方法应该尽可能小。
假设你可以做一些“提取方法”重构和提取,例如:
processIgnoreCancellationEventHandlers(); processEventHandlersWithPossibleCancellation();
如果可能的话,我会更进一步使用不同的输入参数制作一种方法,例如:
processEventHandlers(noCancellationEventHandlers); processEventHandlers(CancellationAwareEventHandlers);
这样你将有两个成就:
答案 2 :(得分:2)
很难知道没有更多的背景但这里有一些想法。
两个循环的for (Entry<Method, EventListener> entry : internalMapping.entrySet()) {
循环似乎相同。我会把它放到它自己的方法中。这将需要一个Map
,它将完成整个循环。那么你们两个for循环会小得多。
private void runMap(Map<Method, EventListener> methodMap) {
for (Entry<Method, EventListener> entry : methodMap.entrySet()) {
...
}
}
然后你可以做一个循环:
for (EventPriority priority : EventPriority.values()) {
runMap(getRegistry().getMethodMap(event.getClass(), priority, true));
runMap(getRegistry().getMethodMap(event.getClass(), priority, false));
}
当你在一个包含整个循环的if (internalMapping != null) {
循环中做某事时,我倾向于使用if (internalMapper == null) continue;
。这减少了缩进级别。
已提到异常处理。您也可以先处理InvocationTargetException
,然后再处理catch (Exception e)
,然后打印出所有其他内容。
答案 3 :(得分:2)
我假设你真正想做的就是消除这两个循环。我只是强制它并提取一个包含所有必要参数的方法,例如:
@Override
public void dispatchEvent(Event event) {
checkNotNull(event);
CancellableEvent cancellableEvent = null;
boolean cancellable;
if (cancellable = event instanceof CancellableEvent) {
cancellableEvent = (CancellableEvent) event;
checkArgument(cancellableEvent.isCancelled());
}
fireEvents(false, event, cancellableEvent, cancellable);
fireEvents(true, event, cancellableEvent, cancellable);
}
private void fireEvents(boolean considerCancellation, Event event, CancellableEvent cancellableEvent, boolean cancellable)
{
// Event handlers that consider cancellation will run
for (EventPriority priority : EventPriority.values()) {
Map<Method, EventListener> internalMapping = getRegistry().getMethodMap(event.getClass(), priority, ! considerCancellation);
if (internalMapping != null) {
for (Map.Entry<Method, EventListener> entry : internalMapping.entrySet()) {
try {
entry.getKey().invoke(entry.getValue(), event);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
/*
* Delegate any exceptions that occur from
* the method to a runtime exception.
*/
throw new RuntimeException(e);
}
// Immediately return in the case of the event being cancelled.
if ( considerCancellation && cancellable && cancellableEvent.isCancelled()) {
return;
}
}
}
}
}
然后你可以重构新的fireEvents方法并清理它。
答案 4 :(得分:2)
永远不要在if条件下进行分配。这很容易出错:
if (cancellable = event instanceof CancellableEvent) {
...
}
这样做:
boolean cancellable = event instanceof CancellableEvent;
if (cancellable) {
...
}
答案 5 :(得分:1)
您可以将条目的调用重构为另一种方法。
private final void invokeEntry(Entry<Method, EventListener> entry, Event event) {
try {
entry.getKey().invoke(entry.getValue(), event);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
/*
* Delegate any exceptions that occur from
* the method to a runtime exception.
*/
throw new RuntimeException(e);
}
}
然后您可以将 dispatchEvent 方法替换为:
@Override
public void dispatchEvent(Event event) {
checkNotNull(event);
CancellableEvent cancellableEvent = null;
boolean cancellable;
if (cancellable = event instanceof CancellableEvent) {
cancellableEvent = (CancellableEvent) event;
checkArgument(cancellableEvent.isCancelled());
}
// Ignore-cancellation event handlers will run
for (EventPriority priority : EventPriority.values()) {
Map<Method, EventListener> internalMapping = getRegistry().getMethodMap(event.getClass(), priority, true);
if (internalMapping != null) {
for (Entry<Method, EventListener> entry : internalMapping.entrySet()) {
invokeEntry(entry, event);
}
}
}
// Event handlers that consider cancellation will run
for (EventPriority priority : EventPriority.values()) {
Map<Method, EventListener> internalMapping = getRegistry().getMethodMap(event.getClass(), priority, false);
if (internalMapping != null) {
for (Entry<Method, EventListener> entry : internalMapping.entrySet()) {
invokeEntry(entry, event);
// Immediately return in the case of the event being cancelled.
if (cancellable && cancellableEvent.isCancelled()) {
return;
}
}
}
}
}
答案 6 :(得分:1)
由于除了一个布尔值之外的循环是相同的,我首先将它们分开,然后根据需要将它们进一步分解。
@Override
public void dispatchEvent(Event event) {
checkNotNull(event);
CancellableEvent cancellableEvent = null;
boolean cancellable;
if (cancellable = event instanceof CancellableEvent) {
cancellableEvent = (CancellableEvent) event;
checkArgument(cancellableEvent.isCancelled());
}
handleEvents(event, true);
handleEvents(event, false, cancellableEvent);
}
public void handleEvents(Event event, boolean cancellable)
{
handleEvents(event, cancellable, null);
}
public void handleEvents(Event event, boolean cancellable, CancellableEvent cancellableEvent)
{
for (EventPriority priority : EventPriority.values()) {
Map<Method, EventListener> internalMapping = getRegistry().getMethodMap(event.getClass(), priority, cancellable);
if (internalMapping != null) {
for (Entry<Method, EventListener> entry : internalMapping.entrySet()) {
try {
entry.getKey().invoke(entry.getValue(), event);
} catch (IllegalAccessException | IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
/*
* Delegate any exceptions that occur from
* the method to a runtime exception.
*/
throw new RuntimeException(e);
}
// Immediately return in the case of the event being cancelled.
if (cancellableEvent != null && cancellable && cancellableEvent.isCancelled()) {
return;
}
}
}
}
}