SciMLStyle icon indicating copy to clipboard operation
SciMLStyle copied to clipboard

Function arguments in multiple lines.

Open cossio opened this issue 3 years ago • 9 comments

# Yes 
function my_large_function(argument1, argument2,
                           argument3, argument4,
                           argument5, x, y, z)

# No
function my_large_function(argument1, 
                           argument2,
                           argument3, 
                           argument4,
                           argument5,
                           x,
                           y,
                           z)

Any reason not to prefer the Blue style advice here:

function my_large_function(
    argument1, argument2,
    argument3, argument4,
    argument5, x, y, z
)
    # code
end

Notably, when there are many arguments in multiple lines, no argument is placed on the same line as the function name. All arguments are at the same, one-level, indentation, so there is no custom indentation here and there are no alignment issues if, say, the function name changes. Also, the closing parenthesis is alone in its own line, making very clear when the argument list stops and the code begins. However it'd be fine that not every argument is on its own line (as enforced by Blue, but relaxed here).

cossio avatar May 29 '22 11:05 cossio

One per line is insane to read when you get to like 30 keyword arguments. Closing parenthesis is fine on its own line though.

@YingboMa what do you think?

ChrisRackauckas avatar May 29 '22 13:05 ChrisRackauckas

Given that a column width is being enforced, it shouldn’t be hard to see the closing parenthesis if it is on the same line as arguments. There is something to be said for saving vertical space too.

isaacsas avatar May 29 '22 13:05 isaacsas

It also (to my eyes), breaks the convention that the indentation can be quickly scanned to see where a function ends. Now one has to actively ignore parentheses that arise at the same indentation level (will editors handle that without issues when one uses code folding?).

isaacsas avatar May 29 '22 13:05 isaacsas

Code folding is indeed a bit strange when the closing parenthesis is not indented. You get two folds: One for the args and another for the function body. But maybe that can be solved on the VS Code Julia Extension? https://github.com/julia-vscode/julia-vscode/issues/2907

cossio avatar May 29 '22 13:05 cossio

That doesn’t seem too bad! But there are many different editors people use, and I’d worry a decent number of them may have issues.

isaacsas avatar May 29 '22 14:05 isaacsas

One per line is insane to read when you get to like 30 keyword arguments. Closing parenthesis is fine on its own line though.

Yeah, I agree. I made sure JuliaFormatter preserves

function my_large_function(argument1, argument2,
                           argument3, argument4,
                           argument5, x, y, z)
    ...
end

YingboMa avatar May 30 '22 23:05 YingboMa

@YingboMa Is it guaranteed to preserve:

function my_large_function(
    argument1, argument2,
    argument3, argument4,
    argument5, x, y, z
)
    # code
end

too?

cossio avatar May 31 '22 00:05 cossio

Just my 2 cents.

Here I think BlueStyle version is better, especially when you have a lot of keywords with large names. Take a look at this snippet from PrettyTables.jl with BlueStyle and SciMLStyle:

    pinfo = _print_info(
        data;
        alignment = alignment,
        cell_alignment = cell_alignment,
        cell_first_line_only = cell_first_line_only,
        compact_printing = compact_printing,
        formatters = formatters,
        header = header,
        header_alignment = header_alignment,
        header_cell_alignment = header_cell_alignment,
        max_num_of_columns = max_num_of_columns,
        max_num_of_rows = max_num_of_rows,
        limit_printing = limit_printing,
        renderer = renderer,
        row_labels = row_labels,
        row_label_alignment = row_label_alignment,
        row_label_column_title = row_label_column_title,
        row_number_alignment = row_number_alignment,
        row_number_column_title = row_number_column_title,
        show_header = show_header,
        show_row_number = show_row_number,
        show_subheader = show_subheader,
        title = title,
        title_alignment = title_alignment
    )


    pinfo = _print_info(data; alignment = alignment,cell_alignment = cell_alignment,
                        cell_first_line_only = cell_first_line_only,
                        compact_printing = compact_printing, formatters = formatters,
                        header = header, header_alignment = header_alignment,
                        header_cell_alignment = header_cell_alignment,
                        max_num_of_columns = max_num_of_columns,
                        max_num_of_rows = max_num_of_rows, limit_printing = limit_printing,
                        renderer = renderer, row_labels = row_labels,
                        row_label_alignment = row_label_alignment,
                        row_label_column_title = row_label_column_title,
                        row_number_alignment = row_number_alignment,
                        row_number_column_title = row_number_column_title,
                        show_header = show_header, show_row_number = show_row_number,
                        show_subheader = show_subheader, title = title,
                        title_alignment = title_alignment)

IMHO is way easier to work with the first version to fix problems or update something.

ronisbr avatar Jun 26 '23 20:06 ronisbr

The counterpoint is that the blue style uses a lot more vertical screen space, thereby increasing the chance of being unable to fit all the code in a function on a single screen (especially on laptop screens). I find that a lot more of an obstacle than having multiple parameters per line.

isaacsas avatar Jun 27 '23 12:06 isaacsas