Proposal: Define and call a named nested function in one step.


(Amir Michail) #1

Writing nested functions with meaningful names is a good way to document the code while reducing the complexity of the function containing them.

This proposal concerns nested functions that are defined just before they are called (a common thing to do to avoid passing parameters to the nested function):

func f() {
  …
  func g() {
    ...
  }
  g()
  ...
}

With this proposal, you would be able to just write:

func f() {
  …
  call func g() {
    ...
  }
  ...
}


#2

1. How would you pass parameters to such a function?
2. If the only purpose of the function is to be called immediately, why not write it inline?
3. If you really want to separate the logic, why not use a do block? You can even label it if you want:

    func f() {
      g: do {
        ...
      }
    }

4. Nested functions are generally helpful so that the call site becomes more readable (e.g. you pass the nested function to a `map` or `filter`).

Stephen

···

On Feb 17, 2016, at 10:58 AM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

Writing nested functions with meaningful names is a good way to document the code while reducing the complexity of the function containing them.

This proposal concerns nested functions that are defined just before they are called (a common thing to do to avoid passing parameters to the nested function):

func f() {

func g() {
   ...
}
g()
...
}

With this proposal, you would be able to just write:

func f() {

call func g() {
   ...
}
...
}

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Amir Michail) #3

1. How would you pass parameters to such a function?
2. If the only purpose of the function is to be called immediately, why not write it inline?
3. If you really want to separate the logic, why not use a do block? You can even label it if you want:

   func f() {
     g: do {
       ...
     }
   }

If at some point in the future you want to call the nested function more than once, it takes less finger typing to do so than with the labeled do.

Also, if a cyclomatic complexity tool treats nested functions as if they were not nested, then you would get a lower cyclomatic complexity for the containing function with my proposal than with a labeled do.

···

On Feb 17, 2016, at 11:06 AM, Stephen Celis <stephen.celis@gmail.com> wrote:

4. Nested functions are generally helpful so that the call site becomes more readable (e.g. you pass the nested function to a `map` or `filter`).

Stephen

On Feb 17, 2016, at 10:58 AM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

Writing nested functions with meaningful names is a good way to document the code while reducing the complexity of the function containing them.

This proposal concerns nested functions that are defined just before they are called (a common thing to do to avoid passing parameters to the nested function):

func f() {

func g() {
  ...
}
g()
...
}

With this proposal, you would be able to just write:

func f() {

call func g() {
  ...
}
...
}

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Brent Royal-Gordon) #4

Also, if a cyclomatic complexity tool treats nested functions as if they were not nested, then you would get a lower cyclomatic complexity for the containing function with my proposal than with a labeled do.

If you're defining a function in the middle of a chunk of code, calling it, and continuing with more code after the call, that function is not really reducing the complexity of your code.

(That is, defining a nested function at the top or bottom of its containing function, and then using it somewhere in the middle, does make your code easier to understand. Defining it in the middle at the site where it's called doesn't.)

···

--
Brent Royal-Gordon
Architechies