News for hackers. Stuff that matters.
Re:Practical Go: Real-world advice for writing maintainable ... by ragona on Thursday February 21, 2019 @02:40PM (#19219868)
> A long function is easier to understand than an exploded one.

This is a pretty controversial position, and quite situational in my opinion. I absolutely agree that having to hop all over the source to understand something is frustrating, but that doesn't mean that very long functions are the right solution. Some combination of reasonably named helper methods and a function flow that makes the logic easy to parse should be the goal; either end of the spectrum is a problem.

Re:Practical Go: Real-world advice for writing maintainable ... by xtian on Thursday February 21, 2019 @03:29PM (#19220287)
Re:Practical Go: Real-world advice for writing maintainable ... by ra7 on Thursday February 21, 2019 @04:21PM (#19220672)
The summary is great:

> If a function is only called from a single place, consider inlining it.

> If a function is called from multiple places, see if it is possible to arrange for the work to be done in a single place, perhaps with flags, and inline that.

> If there are multiple versions of a function, consider making a single function with more, possibly defaulted, parameters.

> If the work is close to purely functional, with few references to global state, try to make it completely functional.

> Try to use const on both parameters and functions when the function really must be used in multiple places.

> Minimize control flow complexity and "area under ifs", favoring consistent execution paths and times over "optimally" avoiding unnecessary work.

Re:Practical Go: Real-world advice for writing maintainable ... by networkimprov on Thursday February 21, 2019 @02:49PM (#19219955)
I took issue with this because it's a conventional wisdom, and does a fair bit of damage.

Single-caller functions attract other callers over time, gain backwards-incompatible features, and result in regressions.

Re:Practical Go: Real-world advice for writing maintainable ... by Scramblejams on Thursday February 21, 2019 @03:09PM (#19220125)
This is one reason I like nested functions. They’re not available to the surrounding scope so they don’t succumb to these weaknesses, while also allowing you to organize your very long function internally by task. I use ‘em in Python all the time.

It’s a bummer that more languages don’t support them, though you can get there with lambdas too, sometimes at the cost of more syntax.

Re:Practical Go: Real-world advice for writing maintainable ... by networkimprov on Thursday February 21, 2019 @03:17PM (#19220207)
Go has closure functions; great feature.

One of the (few) things I like about Javascript is the ability to define a closure anywhere in the containing function, so it appears in the order of operations:

  function f() {
    setTimeout(fDing, 2000);
    g();
    function fDing() {
      ...
    }
  }
Re:Practical Go: Real-world advice for writing maintainable ... by johndubchak on Thursday February 21, 2019 @03:43PM (#19220408)
Disclaimer: I have no Golang programming experience.

I am curious with this approach though...you're nesting behavior and/or logic, doesn't this further obscure the meaning of the code and contribute more to the need to jump around the source in order to figure it out?

Re:Practical Go: Real-world advice for writing maintainable ... by vram22 on Thursday February 21, 2019 @05:56PM (#19221401)
Go has anonymous functions too.

The Go Programming Language book (Kernighan and Donovan) has some examples of them.

Re:Practical Go: Real-world advice for writing maintainable ... by SaltyBackendGuy on Thursday February 21, 2019 @05:39PM (#19221227)
Now write a unit test for

    function fDing() {
      ...
    }
Honestly with modern JS I am not sure this feature is that great. Looks more like a code smell these days imo.

Edit: Formatting

Re:Practical Go: Real-world advice for writing maintainable ... by Roboprog on Thursday February 21, 2019 @06:12PM (#19221543)
Now write a unit test for the inline block of code within the longer function which would have become the nested function???

You write the test (where any is needed) for the outer function.

Edit: the start of the discussion was about using nested functions to decompose what otherwise would have been “unpartitioned” long functions. Such blocks of code nested within a long function would not be possible to unit test, either.

Unit tests, rather than integration tests, are usually bogus, anyway, though.

Re:Practical Go: Real-world advice for writing maintainable ... by int_19h on Thursday February 21, 2019 @11:01PM (#19223018)
You write unit tests for units of code. A function with nested functions inside of it is a single unit of code; that's essentially what those functions being nested, and hence not directly invokable from the outside, indicates.
Re:Practical Go: Real-world advice for writing maintainable ... by kazagistar on Friday February 22, 2019 @10:52AM (#19226309)
Make private functions that are only visible to the current module. Then write your unit tests directly in that same module, next to the functions, so they can access them even when the rest of the world cannot. Of course, this requires sensible language support.
Re:Practical Go: Real-world advice for writing maintainable ... by Roboprog on Friday February 22, 2019 @09:39PM (#19231247)
Hierarchy vs list. I don’t want a list (of subroutines). I want a tree of self contained routines. Only your containing routine uses you. Which “private“ routines use which? (I know the IDE will tell me about this routine, and that routine, but I don’t want to have to ask)

Your employer doesn’t want you testing getters, anyway, but rather features. Unit test fanatics need to stop.

I guess unit tests were useful for C++, when it was constantly crashing everything :-(

C++ and its legacy need to ride off into the sunset, already.

Re:Practical Go: Real-world advice for writing maintainable ... by Roboprog on Thursday February 21, 2019 @06:07PM (#19221509)
A bit of our “heritage” in programming destroyed by the C family of programming languages

Pascal, like Algol, had nested subroutines for decomposing longer operations without leaking the details.

Nested functions is one of the things I like about JavaScript as well.

Re:Practical Go: Real-world advice for writing maintainable ... by Someone on Thursday February 21, 2019 @07:35PM (#19222051)
C has static functions for decomposinglo ger operations without leaking the details.

For better or for worse, taking advantage of that forces you to keep source files fairly short and cohesive in functionality.

Re:Practical Go: Real-world advice for writing maintainable ... by int_19h on Thursday February 21, 2019 @11:02PM (#19223022)
You are still leaking the details of the function that is the sole caller of those other functions. It's not leaking across translation unit boundary, but it's not the only one that matters.
Re:Practical Go: Real-world advice for writing maintainable ... by pjmlp on Friday February 22, 2019 @01:24AM (#19223547)
ML derived languages, Julia, D and C# support them.
Re:Practical Go: Real-world advice for writing maintainable ... by Gibbon1 on Thursday February 21, 2019 @07:22PM (#19221969)
GNU C supports nested functions as an extension. I use them for the exact reasons you mention.
Re:Practical Go: Real-world advice for writing maintainable ... by favorited on Thursday February 21, 2019 @08:48PM (#19222420)
That extension results in executable stack memory, which is going to be a non-starter for many developers.
Re:Practical Go: Real-world advice for writing maintainable ... by Gibbon1 on Thursday February 28, 2019 @04:10AM (#19270070)
I believe that that only happens if you pass a nested function as a function pointer. Then gcc emits a trampoline.

If you just call it from the enclosing function it's fine.

Re:Practical Go: Real-world advice for writing maintainable ... by ragona on Thursday February 21, 2019 @03:42PM (#19220400)
Yeah, agreed. I do this in Rust a lot too, and I picked it up while writing a lot of Python.
Re:Practical Go: Real-world advice for writing maintainable ... by tigershark on Thursday February 21, 2019 @10:35PM (#19222901)
C# has inner named functions.
Re:Practical Go: Real-world advice for writing maintainable ... by kazagistar on Friday February 22, 2019 @10:48AM (#19226286)
An excellent balance is to try to make a function only do one level of abstraction at a time. It's a bit of a flexible guideline, but basically you shouldn't call `isUserActive`, do some complex arithemtic, read extract data from a complex data structure, and call a templating engine in one function, since those a results all different levels.

As long as what are doing is approximately the same type of thing, it is fine to do a lot of things without breaking readability.