Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. Swift prizes clarity. Its parameter labeling system emphasizes self-documentation
  2. and guides code production. In nearly every case, labels follow
  3. three simple rules:
  4.  
  5. * Skip argument labels for a method or function's first parameter
  6. * Use argument labels for a method or function's subsequent parameters
  7. * Require argument labels for initializers
  8.  
  9. These base rules enhance Swift legibility. Unlike other languages whose positional argument
  10. names have meaning only within the implementation context, Swift's labels convey use and
  11. meaning at the calling site. This creates better communication, enhances maintainability,
  12. and adheres to the principle that code is written rarely and read and reviewed often.
  13.  
  14. At times, special circumstances may apply to your code as explored in the following rules:
  15.  
  16. * *Skip first argument labels* when the first argument completes a sentence established in the base name.
  17. If the argument describes a call's primary semantics, it does not require a label:
  18.  
  19. ```swift
  20. a.contains(b) // b completes the phrase "a contains b"
  21. a.mergeWith(b) // b completes the phrase "merge with b"
  22. a.readFrom(u, ofType: b) // "a, read from u" describes
  23. // primary semantics so u gets no
  24. // label.
  25. // b is an option that tunes the
  26. // primary semantics
  27. ```
  28.  
  29. * *Skip the first argument label* when a noun in the base name describes the first argument's role.
  30.  
  31. ```swift
  32. a.addObserver(b) // "add b" completes a meaningful sentence that
  33. // defines the intentended semantics. The first
  34. // argument is the "Observer".
  35. ```
  36.  
  37. * *Move the first argument label* to the base name when it describes a name or
  38. identifier that acts as the subject of the base action.
  39.  
  40. ```swift
  41. a.transitionToScene(.GreatHall) // yes
  42. a.transitionToSceneWithIdentifier(.GreatHall) // no
  43.  
  44. let p = someFont.glyph("propellor") // yes
  45. let p = someFont.glyphWithName("propellor") // no
  46. let p = someFont.glyph(name: "propellor") // no
  47. ```
  48.  
  49. * *Move the first argument label* to the base name when it describes argument attributes of existing instances.
  50.  
  51. ```swift
  52. a.tracksOfMediaType("Wax Cylinder") // yes
  53. a.removeFirstTrackOfMediaType("BetaMax") // yes
  54.  
  55. a.tracks(mediaType: "Wax Cylinder") // no
  56. a.removeFirstTrack(havingMediaType: "BetaMax") // no
  57. ```
  58.  
  59. * *Use first label arguments* when the first parameter is semantically distinct from the base name
  60. and does not complete a meaningful "sentence"
  61. ```swift
  62. a.dismiss(animated: b) // "a, dismiss b" is a sentence but
  63. // doesn't describe the semantics at all,
  64. // thus we add a label for b.
  65. ```
  66.  
  67. * *Use all argument labels* when the relationship between arguments is semantically stronger than
  68. the relationship between the first argument and the base name.
  69.  
  70. ```swift
  71. moveTo(x: a, y: b)
  72. login(userName: a, password: b)
  73. constructColor(red: r, green: g, blue: b, alpha: a)
  74. ```
  75.  
  76. * *Omit labels* for argument peers that cannot be usefully distinguished.
  77.  
  78. ```swift
  79. min(number1, number2)
  80. zip(sequence1, sequence2)
  81. ```
  82.  
  83. * *Use explicit argument labels* to describe attributes of an instance that's *being created*.
  84. Your calls should resemble initializers.
  85.  
  86. ```
  87. AudioTrack(mediaType: "BetaMax") // initializer
  88. trackFactory.newTrack(mediaType: "Wax Cylinder") // yes
  89.  
  90. trackFactory.newTrackOfMediaType("Wax Cylinder") // no
  91. ```
  92.  
  93. * *Use first argument labels* that would have normally appeared in the base name when building
  94. groups of related calls whose implementations are distinguished specifically by their parameters.
  95. Your calls should resemble initializers.
  96.  
  97. ```swift
  98. login(userName: a, password: b) // not loginWithUserName(a, password: b)
  99. login(credential: a) // not loginWithCredential(a)
  100. ```
  101.  
  102. * *Skip first argument labels* for initializers when using full width type conversions, that is
  103. when initializing from instances of another type.
  104.  
  105. ```swift
  106. extension String {
  107. // Convert `x` into its textual representation
  108. // in the given radix
  109. init(_ x: BigInt, radix: Int = 10)
  110. }
  111. text = "The value is: "
  112. text += String(veryLargeNumber)
  113. text += " and in hexadecimal, it's"
  114. text += String(veryLargeNumber, radix: 16)
  115. ```
  116.  
  117. * *Use first argument labels* when narrowing initial values to make it conform
  118. to restrictions within the new type. The label should describe how the instance
  119. will be modified:
  120.  
  121. ```swift
  122. extension UInt32 {
  123. init(_ value: Int16) // Widening, so no label
  124. init(truncating bits: UInt64)
  125. init(saturating value: UInt64)
  126. }
  127. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement