이 페이지는 아직 한국어로 제공되지 않습니다. 번역 작업 중입니다. 현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.
Metadata
ID:swift-code-style/max-class-lines
Language: Swift
Severity: Warning
Category: Best Practices
Description
This rule states that Swift classes should not exceed 1000 lines of code (by default). The purpose of this guideline is to ensure code remains readable and maintainable. Large classes can become difficult to understand, debug, and maintain, increasing the likelihood of bugs and inefficiencies.
Following this rule promotes clean code practices and fosters efficient software development. It encourages developers to adopt modularity by breaking down complex problems into smaller, more manageable pieces.
To prevent classes from becoming too large, it is good practice to split functionality into multiple smaller classes, each with a single, well-defined responsibility. If a class is growing too complex, evaluate whether some of its functionality can be moved to a separate class. You can use inheritance or composition to help distribute responsibilities. For example, if MyClass is too large, you could create a new class, MySubClass, and let MyClass inherit from it:
The goal is to write code that is easy to read, understand, and maintain. By keeping classes concise and focused, you can improve code quality and make your software easier to work with.
Arguments
max-lines: Maximum number of lines. Default: 100.
Non-Compliant Code Examples
// This class is NOT compliant because it is >100 lines long.classPerson{varname:Stringvarage:Intvaroccupation:String?init(name:String,age:Int,occupation:String?=nil){self.name=nameself.age=ageself.occupation=occupation}funcdisplayOccupation(){print(self.age);}}
Compliant Code Examples
// This class is compliant because it is <=100 lines long.classPerson{varname:Stringvarage:Intvaroccupation:String?init(name:String,age:Int,occupation:String?=nil){self.name=nameself.age=ageself.occupation=occupation}funcdisplayOccupation(){print(self.age);}}
원활한 통합. Datadog Code Security 사용해 보기
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드 분석해 보기
이 규칙의 사용법
1
2
rulesets:- swift-code-style # Rules to enforce Swift code style.
리포지토리 루트에서 위의 콘텐츠를 사용해 static-analysis.datadog.yml 생성
Datadog의 무료 IDE Plugins를 사용하거나 Code Security 스캔을 CI 파이프라인에 추가