11

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.

Kheldar
  • 5,361
  • 3
  • 34
  • 63
  • 3
    I'm not familiar with `swiftlint` but actually Apple says in the Swift Language Guide: [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.**](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/NestedTypes.html#//apple_ref/doc/uid/TP40014097-CH23-ID242) – vadian Mar 24 '17 at 10:03
  • 1
    `String.CharacterView.Index` is used by the Swift standard library. – Martin R Mar 24 '17 at 10:07
  • @MartinR true enough, which is exactly why I've stated it's an "established technique", and why I ask the question "what is the reason for this linting rule". Surely people don't make linting rules without good reason? – Kheldar Mar 24 '17 at 10:11
  • 2
    I don't know of a technical reason. It seems more like a "best practice" (and opinion-based) rule to me, like "a function should not be longer than nn lines" or "indenting should use 4 spaces". – Martin R Mar 24 '17 at 10:18

1 Answers1

7

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.

Kheldar
  • 5,361
  • 3
  • 34
  • 63