r/iOSProgramming • • 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

19 comments sorted by

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.

-1

u/WeirdIndividualGuy 1d ago

The irony of having this take while your comment is riddled with typos

1

u/KukrCZ 22h ago

edited a little bit to minimise triggering 😅

0

u/dynocoder 1d ago

what makes it more structurally sound though? you could split up a line across many and it’ll work just the same

0

u/KukrCZ 1d ago

Splitting annotations and definitions on separate lines is one thing.

S
p
l
i
t
i
n
g
i
t

m
o
r
e

i
s

n
o
t
.

2

u/dynocoder 23h ago

ok but that sentence would be structurally and semantically the same had you typed it normally

0

u/KukrCZ 22h ago

True. Then it is just simply matter of preference.

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

1

u/AutoModerator 1d ago

Hey /u/FahadAppMaster, your content has been removed because Reddit has marked your account as having a low Contributor Quality Score. This may result from, but is not limited to, activities such as spamming the same links across multiple subreddits, submitting posts or comments that receive a high number of downvotes, a lack of recent account activity, or having an unverified account.

Please be assured that this action is not a reflection of your participation in our subreddit. This is simply an automated filter in place to reduce spam.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.