Coding style 具有多个退出点的功能:这是一个好方法吗?
在使用VBScript编程期间,在函数开始执行操作之前,我在函数中编写了许多错误检查代码。所以,如果一些预先的要求不能满足,那么我就执行“退出功能”。例如:Coding style 具有多个退出点的功能:这是一个好方法吗?,coding-style,code-readability,Coding Style,Code Readability,在使用VBScript编程期间,在函数开始执行操作之前,我在函数中编写了许多错误检查代码。所以,如果一些预先的要求不能满足,那么我就执行“退出功能”。例如: public fucnton func if not condition then func = -1 exit function End If 'Other conditions with exit functions 'Then long code goes here .........
public fucnton func
if not condition then
func = -1
exit function
End If
'Other conditions with exit functions
'Then long code goes here
..........
..........
..........
func = res
End Function
所以,我可以在多个点退出函数。这样做好吗?在这种情况下,我将得到if语句的long-else分支
也许最好写下:
public fucnton func
if not condition then
func = -1
Else
'Then long code goes here
..........
..........
..........
End If
End Function
请分享您的想法。在处理错误情况时退出函数肯定更好。原因是避免了无休止的嵌套ifs,可读性好得多。几十年来,我一直使用“if(badParameters)then exit”风格的编码。如果没有其他内容,则执行工作的实际“长代码”不会被大量的“If/then/else”梯形图推离编辑窗口的右侧。梯形图使代码看起来比实际更混乱、更复杂,并妨碍可读性