gpt4 book ai didi

java - 我怎样才能使其更具可读性和更清晰?

转载 作者:行者123 更新时间:2023-12-02 12:58:49 25 4
gpt4 key购买 nike

我想知道我能做些什么来使其更具可读性和简洁性。我所说的可读性是指对于其他开发人员来说更容易阅读。

我真的不想两次使用相同的代码。我想我可以采取一些方法来缩短它,但我不太确定......

@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;
}
}
}
}
}

最佳答案

如果您正在谈论异常,那么在 java 7 中您可以将异常俱乐部化。

这是关于 Working with Java7 Exception 的文章

} catch (ParseException | IOException exception) {
// handle I/O problems.
}

关于迭代,您可以使用单独的方法来调用功能。

关于java - 我怎样才能使其更具可读性和更清晰?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12608347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com