Main Content

checkcode

MATLAB 코드 파일에서 발생할 수 있는 문제 확인

설명

예제

참고

R2022b: 상호 작용이 개선되고 식별된 문제를 저장하는 기능이 있으므로 codeIssuescheckcode보다 권장됩니다.

checkcode(filename)은 잠재적인 문제를 보고하고 코드를 개선할 수 있는 방법을 제안하는 filename에 대한 메시지를 표시합니다. 이러한 메시지를 코드 분석기 메시지라고도 합니다. 메시지의 라인 번호는 편집기에서 클릭하여 직접 그 위치로 이동할 수 있는 하이퍼링크입니다. checkcode 메시지의 정확한 텍스트는 버전마다 다를 수 있습니다.

checkcode(filename1,...,filenameN)은 지정된 각 filename에 대한 메시지를 표시합니다.

예제

checkcode(___,option1,...,optionN)은 지정된 옵션 플래그를 기반으로 반환된 메시지를 수정합니다. 예를 들어, '-modcyc'를 지정하여 각 메시지와 함께 수정된 순환 복잡도(Modified Cyclomatic Complexity)를 반환하도록 요청합니다. 위에 열거된 구문 중 하나의 입력 인수를 사용하여 옵션을 지정할 수 있습니다.

예제

info = checkcode(___,'-struct')는 정보를 n×1 구조체형 배열로 반환합니다. 여기서 n은 검색된 메시지의 개수입니다.

msg = checkcode(___,'-string')은 정보를 문자형 벡터로 반환합니다.

'-struct' 또는 '-string' 인수를 생략하고 출력 인수를 지정하는 경우 디폴트 동작은 '-struct'입니다.

[___, filepaths] = checkcode(___)는 파일 이름에 대한 절대 경로인 filepaths도 반환합니다. '-struct' 옵션이나 '-string' 옵션을 사용하여 filepaths를 지정할 수 있습니다.

예제

모두 축소

예제 파일 lengthofline.m에 대해 checkcode를 실행합니다. MATLAB®에서는 이 lengthofline.m에 대한 코드 분석기 메시지를 명령 창에 표시합니다.

checkcode('lengthofline')
L 21 (C 1-9): Value assigned to variable might be unused.
L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 23 (C 5-11): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 23 (C 44-49): Use STRCMPI(str1,str2) instead of using UPPER/LOWER in a call to STRCMP.
L 27 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 33 (C 13-16): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 33 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD.
L 38 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 39 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 40 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 42 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 44 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 47 (C 21): A '[' might be missing a closing ']', causing invalid syntax at ')'.
L 47 (C 51): A '(' might be missing a closing ')', causing invalid syntax at ';'.
L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax.
L 48 (C 17): Add a semicolon after the statement to hide the output (in a function).

예제 파일 lengthofline.m에 대해 checkcode를 실행합니다. 메시지 ID를 포함시키고 결과를 구조체에 저장합니다.

info = checkcode('lengthofline', '-id')
info=16×1 struct array with fields:
    id
    message
    fix
    line
    column

첫 번째 메시지의 값을 봅니다.

info(1)
ans = struct with fields:
         id: 'NASGU'
    message: 'Value assigned to variable might be unused.'
        fix: 0
       line: 21
     column: [1 9]

'-modcyc' 옵션을 사용하여 예제 파일 lengthofline.m에 대해 checkcode를 실행합니다. MATLAB®에서는 lengthofline.m의 수정된 순환 복잡도를 표시하고, 그 뒤에 이 파일에 대한 코드 분석기 메시지를 표시합니다.

checkcode('lengthofline', '-modcyc')
L 1 (C 23-34): The modified cyclomatic complexity of 'lengthofline' is 12.
L 21 (C 1-9): Value assigned to variable might be unused.
L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 23 (C 5-11): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 23 (C 44-49): Use STRCMPI(str1,str2) instead of using UPPER/LOWER in a call to STRCMP.
L 27 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 33 (C 13-16): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 33 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD.
L 38 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 39 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 40 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 42 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 44 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 47 (C 21): A '[' might be missing a closing ']', causing invalid syntax at ')'.
L 47 (C 51): A '(' might be missing a closing ')', causing invalid syntax at ';'.
L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax.
L 48 (C 17): Add a semicolon after the statement to hide the output (in a function).

설정 파일을 만들고 지정하여 특정 메시지가 표시되지 않도록 합니다. 예를 들어, 파일 lengthofline.mOR 연산자로 || 대신 |를 사용하는 라인이 여러 개 포함되어 있습니다. 기본적으로 checkcode는 이러한 라인에 플래그를 지정합니다.

checkcode('lengthofline')
L 21 (C 1-9): Value assigned to variable might be unused.
L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 23 (C 5-11): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 23 (C 44-49): Use STRCMPI(str1,str2) instead of using UPPER/LOWER in a call to STRCMP.
L 27 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 33 (C 13-16): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 33 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD.
L 38 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 39 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 40 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 42 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 44 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 47 (C 21): A '[' might be missing a closing ']', causing invalid syntax at ')'.
L 47 (C 51): A '(' might be missing a closing ')', causing invalid syntax at ';'.
L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax.
L 48 (C 17): Add a semicolon after the statement to hide the output (in a function).

OR 연산자로 |를 사용하는 것으로 플래그를 지정하는 메시지의 표시를 차단하는 설정 파일을 만듭니다.

  1. 탭의 환경 섹션에서 기본 설정 버튼을 클릭합니다.

  2. 왼쪽 창에서 코드 분석기를 선택합니다.

  3. 디폴트 설정 아래 심미성과 가독성 섹션에서, (스칼라) 조건문에서 | 대신 ||를 OR 연산자로 사용하십시오 메시지의 선택을 취소합니다.

  4. 파일 이름으로 mysettings.txt를 입력하고 현재 폴더에 저장합니다.

  5. 취소 버튼을 눌러 활성화된 설정을 변경하지 않고 기본 설정 패널을 닫습니다.

사용자 지정 설정 파일 mysettings.txt를 사용하여 예제 파일에 대해 checkcode를 실행합니다. 메시지 (스칼라) 조건문에서 | 대신 ||를 OR 연산자로 사용하십시오는 차단되어 메시지 목록에 더 이상 표시되지 않습니다.

checkcode('lengthofline','-config=mysettings.txt')
L 21 (C 1-9): Value assigned to variable might be unused.
L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 23 (C 5-11): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 23 (C 44-49): Use STRCMPI(str1,str2) instead of using UPPER/LOWER in a call to STRCMP.
L 27 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 33 (C 13-16): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 33 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD.
L 38 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 39 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 40 (C 17-45): To improve performance, use 'isscalar' instead of length comparison.
L 42 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 44 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed.
L 47 (C 21): A '[' might be missing a closing ']', causing invalid syntax at ')'.
L 47 (C 51): A '(' might be missing a closing ')', causing invalid syntax at ';'.
L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax.
L 48 (C 17): Add a semicolon after the statement to hide the output (in a function).

입력 인수

모두 축소

파일 이름으로, 문자형 벡터, string형 배열 또는 문자형 벡터로 구성된 셀형 배열로 지정됩니다. 파일 이름은 부분 경로를 포함할 수 있지만 검색 경로의 폴더에 있거나 현재 폴더에 있어야 합니다.

filename이 비 스칼라 string형 배열 또는 문자형 벡터로 구성된 셀형 배열인 경우, MATLAB®은 각 파일에 대한 정보를 표시합니다.

참고

파일 이름으로 구성된 셀형 배열과 문자형 배열은 결합할 수 없습니다. 예를 들어, {'lengthofline', 'buggy'}, 'collatz'는 입력값으로 사용할 수 없습니다.

예: 'lengthofline'

예: {'lengthofline', 'buggy'}

데이터형: char | string

표시 옵션으로, 다음 값 중 하나로 지정됩니다. 옵션은 어떤 순서로 지정해도 좋습니다.

옵션설명
'-id'메시지 ID를 요청합니다. 여기서 ID는 문자형 벡터입니다. 구조체에 반환될 때 출력값에는 메시지와 연결된 ID인 id 필드도 있습니다.
'-fullpath' checkcode가 위치를 지정하지 않도록 입력 파일 이름을 절대 경로로 가정합니다.
'-notok'

checkcode를 무시하는 지시문 %#ok로 끝나는 라인을 비롯한 filename의 모든 라인에 대해 checkcode를 실행합니다.

%#ok와 프로그램의 메시지 표시 차단에 대해서는 코드 분석기의 메시지 표시자와 메시지 조정하기 항목을 참조하십시오.

'-cyc'

파일 내 각 함수의 McCabe 순환 복잡도를 표시합니다. 일반적으로 복잡도가 낮은 프로그램은 이해하기 쉽고 수정하기 편리합니다. 복잡도가 높은 프로그램일수록 오류가 있을 가능성이 더 높습니다. 더 작고 더 간단한 함수로 나누면 함수의 복잡도를 낮출 수 있습니다. 복잡도가 10을 초과하는 프로그램은 분리하는 것이 좋다는 주장도 있습니다.

순환 복잡도에 대한 자세한 내용은 Measure Code Complexity Using Cyclomatic Complexity 항목을 참조하십시오.

'-modcyc'

파일 내 각 함수의 수정된 순환 복잡도(Modified Cyclomatic Complexity)를 표시합니다. 함수의 수정된 순환 복잡도는 한 가지 차이점을 제외하면 McCabe 순환 복잡도와 동일합니다. McCabe 순환 복잡도는 switch 문 내의 case 각각을 1로 세는 반면 수정된 순환 복잡도는 전체 switch 문을 1로 셉니다. 일반적으로 switch 문은 중첩된 if-elseif-else 문보다 간단하므로 수정된 순환 복잡도가 코드 복잡도의 더 나은 척도로 간주되는 경우가 많습니다.

순환 복잡도에 대한 자세한 내용은 Measure Code Complexity Using Cyclomatic Complexity 항목을 참조하십시오.

'-config=settingsfile'

'-config=factory'

활성화된 디폴트 설정 파일을 지정된 설정 파일로 재정의합니다. 지정된 파일이 현재 폴더에 없는 경우, 해당 파일에 대한 전체 경로를 제공하십시오.

설정 파일을 만드는 방법에 대한 자세한 내용은 코드 분석기 메시지 설정 저장 및 재사용 항목을 참조하십시오. 유효하지 않은 파일을 지정한 경우 checkcode는 지정된 파일을 열거나 읽을 수 없다는 메시지를 반환합니다. 이 경우 checkcode는 공장 초기값 설정을 사용합니다.

모든 설정 파일을 무시하고 공장 초기값 기본 설정을 사용하려면 '-config=factory'를 지정하십시오.

출력 인수

모두 축소

메시지 정보로, n×1 구조체형 배열로 반환됩니다. 여기서 ncheckcode 명령에서 반환하는 메시지 개수입니다. 여러 개의 파일 이름을 입력값으로 지정하거나 셀형 배열을 입력값으로 지정하는 경우 info는 구조체로 구성된 셀형 배열을 포함합니다.

필드

설명

message

코드를 분석할 때 감지된 의심스러운 구문을 설명하는 메시지.

line

메시지가 적용되는 파일의 라인을 나타내는 라인 번호로 구성된 벡터.

column

메시지가 적용되는 파일의 열을 나타내는 열 번호(열 범위)로 구성된 2열 배열. 이 배열의 첫 번째 열은 편집기에서 메시지 내용이 시작되는 부분을 명시합니다. 배열의 두 번째 열은 편집기에서 메시지 내용이 끝나는 부분을 명시합니다. 2열 배열에는 발생한 메시지마다 하나의 행이 있습니다.

메시지 정보로, 문자형 벡터로 반환됩니다. 여러 개의 파일 이름을 입력값으로 지정하거나 셀형 배열을 입력값으로 지정하는 경우에는 msg에 각 파일 정보를 구분하는 문자형 벡터가 포함됩니다. 이 문자형 벡터는 등호 문자 10개, 공백, 파일 이름, 공백, 등호 문자 10개 형식으로 표시되어 각 파일을 구분합니다.

예: ========== C:\MyMatlabFiles\buggy.m ==========

파일의 절대 경로로, 문자형 벡터로 구성된 셀형 배열로 지정됩니다. MATLAB에는 지정된 입력 파일과 동일한 순서로 filepaths가 나열됩니다.

코드 분석기에서 특정 코드 라인을 무시하도록 하려면 해당 라인 끝에 %#ok를 사용하십시오. 태그 뒤에 주석을 추가할 수 있습니다.

unsuppressed1 = 10 	% This line will get caught
suppressed2 = 20		%#ok This line will not get caught
suppressed3 = 30		%#ok This line will not get caught

확장 기능

버전 내역

R2011b에 개발됨