Code formatting edge-cases


#1

I usually have a good idea of how I like my code to look so I can parse it in the future, but sometimes I just hit a wall trying to figure out how to format some of the longest lines in my code to keep under the 100 column limit.

I know it’s okay to go over sometimes (in the words of Captain Barbosa, “They’re more like guidelines than actual rules…”) but I don’t like to because I think long solitary lines are ugly. However, I’ve been experimenting with some styles and I don’t know which of them is less ugly than just fudging the column limit.

Here’s some out-of-context snippets I’ve been most heavily tested while working on, from https://github.com/cybergeek94/multipart/tree/revival:

Long type and parameter lists in function

Long concrete parameter list in function

Long where clause in impl, pushed left by some naivety in Rust’s associated type lookup:

Long for-loop iterator expression:

I have read the Rust style guide but it’s been a while and I haven’t been able to find an up-to-date copy online. Last time I checked, it didn’t really cover things like this. I assume they must be edge-cases.

I know the code is probably full of logic errors as well; I’m still working on it. It’s an almost-total rewrite of an old project of mine.


#2

There’s an active discussion on an RFC: Style: function declarations, which might have some more on this.


#3

Personally, I’d either (a) bind the expression you are looping over to a name or (b) use the foreach method from itertools.


#4

@DroidLogician I’m pretty strict about column limits too, except I keep it to 80 columns instead of 100. If you stroll through my code, you may find some ideas. In particular:

This is a technique I use a lot if it’s too inconvenient to use line continuations. Giving names to intermediate things is sometimes nice too, for its own sake.


#5

This is really no easy task. See below what I think is the best you can do. But I think I would too try to bind some of the subexpressions to their own variables.

for (search_idx, maybe_boundary) in
    buf[self.search_idx..].windows(self.boundary.len())
                          .enumerate()
                          .filter(|&(_, window)| window[0] == boundary_0) 
{
    self.boundary_read = self.boundary == maybe_boundary;
    self.search_idx += search_idx;            
}

#6

80 is a bit short, IMO. 100 is just about right to fit in Vim when I have it splitting the screen widthwise with an MSYS2 console.