Loop and argument spacing style

I have some stylistic preferences listed below. I am wondering which I can keep using, and which I should change to conform to the community.

  1. I used the “Format Document” option in VSCode, and it made some choices that I don’t think read very well. I read through the style guide. These topics aren’t specifically addressed, but it does seem to encourage spaces between binary operators and discourage extra parentheses. Is the VSCode format the preferred way, or are one of the other options below acceptable?
# Auto-formatted
for n in 2:N - 1 # It looks like the subtraction is applied to every element of the range.
    push!(v, v[end] + x - 1) # Hard to see that the last terms all combine into one argument.
end
# Parentheses
for n in 2:(N - 1)
    push!(v, (v[end] + x - 1))
end
# No spaces (my preference)
for n in 2:N-1
    push!(v, v[end]+x-1)
end
# Extra spaces
for n in 2 : N - 1
    push!(v,  v[end] + x - 1)
end
  1. Additionally, I quite like the naming scheme for i in 1:I. However, I know Fortran users hate to see that. I also realize it is against the style guide which says that variable names should be lower case. Is there a Julian convention for naming the length of a dimension?

  2. Lastly, I like to group longer mathematical expressions by term like z = w + x*(y - 1) - 3*z, but the automatically formatted style would be z = w + x * (y - 1) - 3 * z instead.

2 Likes

Note that there are various additional style guides (YASGuide, BlueStyle, …) and while they have a lot of overlap, there is no single community-wide style guide that addresses everything.

AFAIK the preferred approach is that package authors pick a style, and contributors just conform to that.

I do that when it makes sense, but occasionally deviate. Eg I would be tempted to write the above as

for n in 2:(N - 1)           # OR 2:(N-1) is fine too
    push!(v, v[end] + x - 1) # note: removed unnecessary ()s
end

removing superfluous ()s.

Well, tough luck, they are reading Julia code.

Not that I am aware of, if it is a vector then len is common, N is also fine.

Just do that then.

Generally, while style guides are useful, I would not get too hung up about these details. Just keep coding and occasionally prettify if you feel like it.

FWIW, IMO refactoring to improve code clarity is much more important for readability than tiny details about spaces and such.

2 Likes