Variables created inside if-statements
이전 댓글 표시
Hello,
Is there a way to avoid/throw a warning/throw an error when a variable is created inside an if-statement?
For example:
if some_condition
% This variable did not exist before the if-statement
something = rand(5);
end
result = SOMEFUNCTION(something);
If the condition some_condition is not fulfilled, the program will crash. This is a really bad practice and it is allowed by Matlab without a warning. Unfortunately the code I am maintaining is full with these cases.
In general, it would be better if Matlab throws an error whenever a variable is created inside an if-statement even if the if-elseif-else case covers all the possible outcomes.
댓글 수: 3
dpb
2013년 6월 19일
Try mlint
Other than that, it's programmer's responsibility to design the code and implement it correctly.... :)
gire
2013년 7월 3일
Jan
2013년 7월 3일
@gire: It is not surprising that MLint creates the same messages as the editor, because MLint does create the messages in the editor.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!