SciML / SciMLStyle

A style guide for stylish Julia developers
https://docs.sciml.ai/SciMLStyle/stable/
MIT License
216 stars 18 forks source link

Spaces around keyword arguments #14

Closed rikhuijzer closed 2 years ago

rikhuijzer commented 2 years ago

Whether this style advises spaces around keyword arguments is not clear. The JuliaFormatter implementation got a space:

https://github.com/domluna/JuliaFormatter.jl/blob/4bbe2d0f9603cd7c5dbb19c23a51ca8b24eea4e1/src/styles/sciml/pretty.jl#L35

while the README.md misses a lot of spaces.

However, shouldn't this be without a space? This is to be consistent with Code Blue Style, YAS and it is easier to read. For example, when calling and defining keyword arguments, having spaces and no spaces helps to easily disambiguate which are variables and which are keyword arguments:

with_some = 1
function my_long_function(;
        with_some=1,
        long=2,
        arguments_in_it=3
    )
    return with_some * long * arguments_in_it
end
my_long_function(;
    with_some=3,
    long=4,
    arguments_in_it=5
)
arguments_in_it = 5

On Slack, they say that with spaces is better because it allows keyword arguments with an exclamation mark, but that sounds like it shouldn't be done in the first place.

YingboMa commented 2 years ago

Having spaces is nice when there are a lot of kwargs.

rikhuijzer commented 2 years ago

@YingboMa, #14 is still unclear about what is the standard since the PR both introduced diffeq_solver_kwargs = (; abstol=1e-6, reltol=1e-6,) and f(x = 1; y = 2). One has spaces around the equal signs and one has not.

To be sure that we're talking about the same thing, the whitespace_in_kwargs setting in JuliaFormatter.jl does this:

using JuliaFormatter

code = """
    f(x; y= 2) = x * y
    """

println("whitespace_in_kwargs=false:\n")
print("  ", format_text(code; whitespace_in_kwargs=false))
println()

println("whitespace_in_kwargs=true:\n")
print("  ", format_text(code; whitespace_in_kwargs=true))
println()
whitespace_in_kwargs=false:

  f(x; y=2) = x * y

whitespace_in_kwargs=true:

  f(x; y = 2) = x * y
GunnarFarneback commented 2 years ago

I don't at all agree that no spaces around keyword arguments are easier to read but I do agree that #16 did not resolve this issue.

The only place I can find which reasonably clearly specifies this, although indirectly, is

The = character in NamedTuples should be spaced as in keyword arguments. Space should be put between the name and its value.

and the corresponding examples.

This part is rather confusing:

Surround most binary operators with a single space on either side: assignment (=), [...]. Binary operators may be excluded from this guideline include: [...] optional arguments/keywords (e.g. f(x = 1; y = 2)).

The example is of the rule, not of the exception, and I'm not clear why there should be an exception here.