There's a few rules that I follow when trying to maintain my own personal projects, these rules also expand to any future potential contributor or maintainer that help me along the way. Some rules are more specific when it comes to certain repositories, sometimes they're more lenient, but in general most these rules are just code and project formatting to begin with. If there is any rules that need to be extended due to contributors or maintainers (due to a large project) I will include a CONTRIBUTION.md
file that explains more on what you should do when contributing.
Below you will find the languages I have strict rules for when it comes to my personal projects, there could be one or even many. Just know that I will try to enforce these when possible.
- Avoid having any functions or variables at top level, for example avoid having a
func example() {}
inmain.swift
- Instead, you can make a static variable or function associated to an extension/class/enum to make them accessible. This is for readability purposes. See FR.swift.
- This rule won't apply for bridging other code, Objective-C can have their code available at top level regardless.
- All private variables and functions must have an underscore
_
at the beginning, mainly for readability.private let _variable: String // underscore needed let variable: String // underscore not needed
- If possible, to if a class and/or struct is has different functions or variables that serve different purposes, try to isolate these into their own extension. For example, when making a
UITableViewController
, all overridden functions for the tableview (if possible) must be in an extension. - If you need to extend functionality or reuse many things, try to extend it and make it convenient by making it be able to be reused everywhere, this is to avoid constantly needing to copy-paste code everywhere.
- In a class and making a custom
init()
, the custom initializer variables must be right above the init.class Example { var something: String = "" // MARK: Init private var _direction: NSCollectionView.ScrollDirection init( direction: NSCollectionView.ScrollDirection = .vertical ) { self._direction = direction super.init(frame: frame) } }
if let
statements should be formatted specifically for readability.// if theres one thing that needs checking, this is okay if let exampleArray { // } // should be formatted like this if theres multiple checks, this is the same for guard statements. if let exampleArray, !exampleArray.isEmpty { // } // MARK: guard examples below guard let exampleArray else { return } guard let exampleArray, !exampleArray.isEmpty else { return }
- Make sure you also
// MARK: -
your classes or code appropriately, for readability. - If you're not sure and want to fix something later, use
#warning("example text")
above the snippet to clearly show within the editor what needs fixing. - In SwiftUI all these rules still apply, however there are some key differences. When making a view, if you need to use a view multiple times, avoid copypasting code and make something convenient for usage. Often times my project has its own seperate package just specifically for these reuseable componeonts, usually local of the project. This is used to keep the project tidy, and seperate the more useless code to the more important code. The important code is usually isolated into its own seperate package or the main project itself.
@CodiumAI-Agent /review