[swift-evolution] Proposal: optionally define nested functions elsewhere
Jean-Daniel Dupas
mailing at xenonium.com
Sun Feb 7 17:04:50 CST 2016
> Le 7 févr. 2016 à 16:06, Amir Michail via swift-evolution <swift-evolution at swift.org> a écrit :
>
>
>> On Feb 7, 2016, at 10:04 AM, Craig Cruden <ccruden at novafore.com> wrote:
>>
>> If indentation is a big problem …. either you have a very small monitor, or you may have too much conditional code :p
>>
>> If your functions are concise and you don’t create big spaghetti conditional code - I would not think indentation would be a great problem.
>
> Nested functions allow you to reduce the length of parameter lists. And so you may end up with a very long function containing many nested functions while trying to reduce parameter list size.
>
An other way to reduce parameter list and nested function is to define a class (or struct) that declare the parameters as ivar, and the netted function that have to be split as methods.
>>
>> Usually I create functions inside functions for duplicate code within that function — and even without that de-duplication a function should not be too large, so once you reduplicate the code should be …. pretty short.
>>
>>
>>> On 2016-02-07, at 21:59:43, Amir Michail <amichail at gmail.com> wrote:
>>>
>>>
>>>> On Feb 7, 2016, at 9:57 AM, Craig Cruden <ccruden at novafore.com> wrote:
>>>>
>>>> Is there a language that implements it this way?
>>>>
>>>> I actually don’t much like it, most of my functions are fairly small and I actually like it the way it is.
>>>>
>>>> If you are using an IDE or editor it is easy to collapse the function if it is not what you want to focus on.
>>>>
>>>
>>> Another problem with nested functions is that the indentation keeps increasing thus leaving less room for the code. This proposal also avoids that problem.
>>>
>>>>
>>>>> On 2016-02-07, at 21:32:19, Amir Michail via swift-evolution <swift-evolution at swift.org> wrote:
>>>>>
>>>>> The problem with nested functions is that they can make the function containing them very long and hard to read.
>>>>>
>>>>> So the idea is to separate the body of a nested function from where it is declared.
>>>>>
>>>>> For example:
>>>>>
>>>>> func f() {
>>>>> var x:Int
>>>>> func g(y:String) {
>>>>> print(“x=\(x), y=\(y)")
>>>>> }
>>>>> ...
>>>>> }
>>>>>
>>>>> could be refactored as:
>>>>>
>>>>> func f() {
>>>>> var x:Int
>>>>> func g(y:String) // function body elsewhere to avoid clutter in f
>>>>> ...
>>>>> }
>>>>>
>>>>> func f().g(y:String) {
>>>>> print(“x=\(x), y=\(y)")
>>>>> }
>>>>>
>>>>> _______________________________________________
>>>>> swift-evolution mailing list
>>>>> swift-evolution at swift.org
>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>>
>>>
>>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list