per-coding-style icon indicating copy to clipboard operation
per-coding-style copied to clipboard

Allow abbreviated empty bodies.

Open Crell opened this issue 2 years ago • 6 comments

Having worked with CPP a ton in the last year and a half, I feel very strongly that we need this. The rules otherwise require wasting 2 extra lines of pointlessness that does nothing to aid readability.

I'm making this a SHOULD rather than MUST for BC reasons, but I would be OK with making it a MUST.

Crell avatar Sep 12 '22 19:09 Crell

This makes sense to me, the one question I'd pose is would it be better to require the method take up at least two lines? Ex:

class Point
{
    public function __construct(private int $x, private int $y)
    {}
    
    // ...
}
class Point
{
    public function __construct(
      public readonly int $x,
      public readonly int $y,
    ) {}
}

This feels like it'd help distinguish abbreviated methods from class properties.

KorvinSzanto avatar Sep 12 '22 20:09 KorvinSzanto

Honestly, I don't think so. It doesn't offer any visual benefit for me; it just leaves a {} handing there like a forgotten appendix.

We should probably do the same for empty classes, like exceptions. I'll add that.

Crell avatar Sep 12 '22 20:09 Crell

While it looks a bit better, it is inconsistent i.e. adds more rules to follow making everything more complicated.

samdark avatar Sep 13 '22 20:09 samdark

Hence why a MAY.

Having worked with a lot of CPP-only constructors now, I firmly believe we do need to make this adjustment. What the current conventions (and thus any automation tools based on them) force you into is just ugly, wasteful noise. I know I'm not the only one, either.

Crell avatar Sep 13 '22 22:09 Crell

Alright. With MAY it's OK.

samdark avatar Sep 14 '22 13:09 samdark

I'd also be OK with a SHOULD here... :smile:

Crell avatar Sep 16 '22 20:09 Crell

I'm still not a fan of method declarations taking a single line, I'd be more happy requiring the {} be on the next line to ensure methods don't look too much like class properties.

That said in the interest of progress let's merge this as is and discuss in a separate issue if any one else would like to.

KorvinSzanto avatar Nov 10 '22 22:11 KorvinSzanto