-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Context.SetPanic #279
Context.SetPanic #279
Conversation
visualfc
commented
Jan 9, 2025
•
edited
Loading
edited
@@ -328,8 +336,8 @@ func (inter *Interp) callBuiltinDiscardsResult(caller *frame, fn *ssa.Builtin, a | |||
|
|||
// callBuiltin interprets a call to builtin fn with arguments args, | |||
// returning its result. | |||
func (interp *Interp) callBuiltinByStack(caller *frame, fn string, ssaArgs []ssa.Value, ir register, ia []register) { | |||
switch fn { | |||
func (interp *Interp) callBuiltinByStack(caller *frame, fname string, fn *ssa.Builtin, ssaArgs []ssa.Value, ir register, ia []register) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cognitive complexity 117 of func (*Interp).callBuiltinByStack
is high (> 30) (gocognit)
Details
lint 解释
这个lint结果表明在函数 (*Interp).callBuiltinByStack
中,其认知复杂度(cognitive complexity)为117,超过了推荐的阈值30。认知复杂度是指理解代码所需的认知努力程度,高认知复杂度通常意味着代码难以理解和维护。
错误用法
以下是一个示例代码片段,展示了可能导致高认知复杂度的错误用法:
func (i *Interp) callBuiltinByStack() {
if i.stack.Len() == 0 {
return
}
op := i.stack.Pop()
switch op {
case "add":
a, b := i.stack.Pop(), i.stack.Pop()
i.stack.Push(a + b)
case "sub":
a, b := i.stack.Pop(), i.stack.Pop()
i.stack.Push(b - a)
// 其他操作...
default:
panic("unknown operation")
}
}
在这个示例中,switch
语句根据不同的操作符执行不同的逻辑,导致代码的复杂度较高。
正确用法
以下是一个改进后的示例代码片段,通过将不同操作符的处理逻辑提取到单独的函数中,降低了认知复杂度:
func (i *Interp) callBuiltinByStack() {
if i.stack.Len() == 0 {
return
}
op := i.stack.Pop()
switch op {
case "add":
i.handleAdd()
case "sub":
i.handleSub()
// 其他操作...
default:
panic("unknown operation")
}
}
func (i *Interp) handleAdd() {
a, b := i.stack.Pop(), i.stack.Pop()
i.stack.Push(a + b)
}
func (i *Interp) handleSub() {
a, b := i.stack.Pop(), i.stack.Pop()
i.stack.Push(b - a)
}
在这个改进后的示例中,将每个操作符的处理逻辑提取到单独的函数中,使得主函数 callBuiltinByStack
的认知复杂度降低。
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
@@ -127,7 +127,11 @@ func (inter *Interp) callBuiltin(caller *frame, fn *ssa.Builtin, args []value, s | |||
case "panic": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string panic
has 3 occurrences, make it a constant (goconst)
Details
lint 解释
这个lint结果提示你在代码中使用了字符串字面量"panic",并且它出现了三次。根据建议,你应该将这个字符串字面量定义为一个常量,以提高代码的可读性和可维护性。
错误用法
func handleError() {
fmt.Println("panic")
fmt.Println("panic")
fmt.Println("panic")
}
在这个示例中,"panic"被多次作为字符串字面量使用。
正确用法
const PanicMessage = "panic"
func handleError() {
fmt.Println(PanicMessage)
fmt.Println(PanicMessage)
fmt.Println(PanicMessage)
}
在这个示例中,我们将"panic"定义为一个常量PanicMessage
,并在函数中使用这个常量来避免重复的字符串字面量。
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
if inter.ctx.panicFunc != nil { | ||
err = inter.ctx.handlePanic(caller, fn, err) | ||
} | ||
panic(err) | ||
|
||
case "recover": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string recover
has 3 occurrences, make it a constant (goconst)
Details
lint 解释
这个lint结果提示你代码中有一个名为 recover
的字符串被使用了三次,建议将其定义为一个常量。这样做可以提高代码的可读性和可维护性。
错误用法
func example() {
fmt.Println("recover")
fmt.Println("recover")
fmt.Println("recover")
}
正确用法
const RecoverString = "recover"
func example() {
fmt.Println(RecoverString)
fmt.Println(RecoverString)
fmt.Println(RecoverString)
}
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流