r/iOSProgramming • u/FullstackViking • 1d ago
Discussion Something I never bothered to ask when learning Swift... Why is the standard for Attributes to be inline rather than on a preceding line?
I visually prefer newlines, but code search works marginally better on inlined attributes.
7
Upvotes


14
u/trouthat 1d ago
It’s mostly personal preference. For me if it fits within the maximum number of characters on a line it stays inline otherwise it goes to the next line
6
u/WitchesBravo 1d ago
When attributes were first added they were mostly short and without arguments, just before var/let was easy and worked. Their usage has since grown to encompass more use cases so now newline makes sense
4
u/CaffeinatedMiqote 1d ago
Short enough -> inline Too long/annotations have wildly different lengths: everyone goes into a newline because jimmy here is a fat fuck
3
u/dawedev 1d ago
Mostly length. Short property wrappers like @State or @Published stay inline because they read as part of the declaration, but anything with arguments like @available(iOS 17, *) usually gets its own line. SwiftLint's opt-in attributes rule lets you pin which ones go where, so the search argument kind of goes away once it's enforced.
9
u/KukrCZ 1d ago edited 22h ago
Structural visibility is a king/queen. Having var/let starting on the same char is just more structurally sound.
2
u/MyHairIsATree 1d ago
This is interesting too see, as in Kotlin the equivalent, atl east in terms of syntax - annotations, are usually done using a new line like in the first image
2
u/Sea_Wrongdoer_3500 1d ago
i dont think theres a hard standard, its mostly what apple samples do. short property wrappers like u/State private var x read fine on one line so people copied that
swiftlint actually has an attributes rule that kind of codifies the split: same line for vars and imports, own line for funcs and types (@MainActor, u/available etc). thats what i see in most codebases
for long ones like yours tbh preceding line reads better, i wouldnt fight it if the team agrees
2
u/kst9602 1d ago
I prefer newlines too. It's hard to follow variable names if they're inline.
1
u/dynocoder 1d ago
ive always broken into new lines whenever the annotations got long. u dont have to follow the basic form, and its not a “standard”
1
u/bscothern 1d ago
I personally do new lines for them. As it is easier to see them all and keeps the properties they are applied to more consistent from an indentation standpoint.
I also find multiple declarations on the same line evil though and I know some people love it. For me it is all clarity and ability to read it easily. ¯_(ツ)_/¯
1
u/[deleted] 1d ago
[removed] — view removed comment