Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested types in Swift - what is the good practice?

I have a swiftlint warning that bothers me.

warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)

However, the nesting of structs is an established programming technique, and quite a few people advocate it.

Edit: Indeed @vadian points out the Swift language guide's rule:To nest a type within another type, write its definition within the outer braces of the type it supports. Types can be nested to as many levels as are required.

I am aware it clashes with the use of generics, and that Xcode may become unbearably slow. It actually was (through measuring the slowest compilation spots) the reason why I started looking at this nesting rule.

What is the reason for the lint rule, and what is the good practice in that respect? Please point out the technical reasons, rather than purely opinion-based advice.

Microsoft actually has a page about nested types, and when they are appropriate. While it is not targeted at Swift, it does have some interesting trans-language thoughtbits.

like image 983
Kheldar Avatar asked Mar 24 '17 09:03

Kheldar


1 Answers

After much searching, all I've found is @jpsim's remark that "the idea behind the nesting rule is to avoid complex interfaces".

Therefore, apart from the compiler issues outlined in my question, which will eventually subside, there seems to not be any technical reason for this rule.

like image 89
Kheldar Avatar answered Oct 02 '22 13:10

Kheldar