<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>225817</bug_id>
          
          <creation_ts>2021-05-14 10:54:57 -0700</creation_ts>
          <short_desc>Proposed change to WebKit Code Style Guidelines for if-return-else-return</short_desc>
          <delta_ts>2021-05-21 10:55:29 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>New Bugs</component>
          <version>Other</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Sam Weinig">sam</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>cdumez</cc>
    
    <cc>darin</cc>
    
    <cc>rniwa</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1760142</commentid>
    <comment_count>0</comment_count>
    <who name="Sam Weinig">sam</who>
    <bug_when>2021-05-14 10:54:57 -0700</bug_when>
    <thetext>I&apos;d like to propose a change to the WebKit Code Style Guidelines (https://webkit.org/code-style-guidelines/) specifically around this rule: https://webkit.org/code-style-guidelines/#linebreaking-else-if. 

The current rules state that &quot;An else if statement should be written as an if statement when the prior if concludes with a return statement.&quot; Additionally, this is generally construed to apply to else statements after a return being elided entirely as well. e.g.

    if (condition)
        return foo;
    else
        return bar;

should be written as:

    if (condition)
        return foo;
    return bar;


I think we should remove this rule and leave it up to the writer to choose how to style this, allowing both else-if after a return and else after a return. My arguments for allowing this are:

- It is often easier to follow logic if you can read out the &quot;if, else-if, else-if, else&quot;, rather than having to notice that there is a return in the statement and adding the else in your head.
- For single line conditions like the above example, the lining up of the return statements is pleasing.
- For constexpr if, the elses are required, so we have to break the rule for them anyway.

I&apos;m eager to paint this bike shed, so what do you think?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1760146</commentid>
    <comment_count>1</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2021-05-14 11:04:51 -0700</bug_when>
    <thetext>I did run into this recently when introducing an &quot;if constexpr&quot; and had to ignore the rule then. That said, unless forced to by constexpr, I actually like that not using an else statement reduces the nesting. It is the same reason we like early returns in WebKit.

I think this would look terrible for e.g.:
```
if (myEarlyReturnCondition)
    return;
else {
    // My
    // long
    // function
    // body.
}
```

For single line conditions, the having the else statement doesn&apos;t look bad but I don&apos;t find it more pleasing to have the else statement :)

I also personally don&apos;t find it confusing or harder to follow-up that we don&apos;t have an else after an early return.

Anyway, if others disagree with me fine but my 2 cents is that I prefer the current coding style (except in the very specific case of if constexpr where we don&apos;t have any other choice).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1760167</commentid>
    <comment_count>2</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2021-05-14 12:18:00 -0700</bug_when>
    <thetext>I like early return when it’s a true &quot;early exit&quot;. I think it can make it much easier to follow the logic of a function.

I don’t think we should insist on it when the two sides of the condition are parallel, especially when both are simple one-liners.

In practice, we are not enforcing an early return style.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1760201</commentid>
    <comment_count>3</comment_count>
    <who name="Sam Weinig">sam</who>
    <bug_when>2021-05-14 14:05:30 -0700</bug_when>
    <thetext>(In reply to Chris Dumez from comment #1)
&gt; I did run into this recently when introducing an &quot;if constexpr&quot; and had to
&gt; ignore the rule then. That said, unless forced to by constexpr, I actually
&gt; like that not using an else statement reduces the nesting. It is the same
&gt; reason we like early returns in WebKit.
&gt; 
&gt; I think this would look terrible for e.g.:
&gt; ```
&gt; if (myEarlyReturnCondition)
&gt;     return;
&gt; else {
&gt;     // My
&gt;     // long
&gt;     // function
&gt;     // body.
&gt; }
&gt; ```
&gt; 

I agree this would be bad, and I don&apos;t think we should have a rule to require this. I am very much proposing this. I am just proposing that we call this decision a &quot;styleistic choice&quot;. I happen to think something like:

```
if (aCondition) {
   // Do
   // Something
   // here 
   return foo;
} else if (anotherCondition) {
   // Do
   // Something
   // else 
   return bar;
} else {
   // Do
   // a.
   // third thing
   return baz; 
}
```

is quite pleasing. Especially since if you didn&apos;t have the early returns, we would write it:

if (aCondition) {
   // Do
   // Something
   // here 
} else if (anotherCondition) {
   // Do
   // Something
   // else 
} else {
   // Do
   // a.
   // third thing
}



&gt; For single line conditions, the having the else statement doesn&apos;t look bad
&gt; but I don&apos;t find it more pleasing to have the else statement :)
&gt; 
&gt; I also personally don&apos;t find it confusing or harder to follow-up that we
&gt; don&apos;t have an else after an early return.

I am not really arguing that it is confusing, just that in some cases it is more pleasing to have the else.

        switch (format.pixelFormat) {
        case PixelFormat::RGBA8:
            if (format.alphaFormat == AlphaPremultiplication::Premultiplied)
                return std::make_tuple(8u, 32u, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast);
            else
                return std::make_tuple(8u, 32u, kCGBitmapByteOrder32Big | kCGImageAlphaLast);

        case PixelFormat::BGRA8:
            if (format.alphaFormat == AlphaPremultiplication::Premultiplied)
                return std::make_tuple(8u, 32u, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
            else
                return std::make_tuple(8u, 32u, kCGBitmapByteOrder32Little | kCGImageAlphaFirst);

        case PixelFormat::RGB10:
        case PixelFormat::RGB10A8:
            break;
        }

looks more pleasing to me and a bit easier to follow than 

        switch (format.pixelFormat) {
        case PixelFormat::RGBA8:
            if (format.alphaFormat == AlphaPremultiplication::Premultiplied)
                return std::make_tuple(8u, 32u, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast);
            return std::make_tuple(8u, 32u, kCGBitmapByteOrder32Big | kCGImageAlphaLast);

        case PixelFormat::BGRA8:
            if (format.alphaFormat == AlphaPremultiplication::Premultiplied)
                return std::make_tuple(8u, 32u, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
            return std::make_tuple(8u, 32u, kCGBitmapByteOrder32Little | kCGImageAlphaFirst);

        case PixelFormat::RGB10:
        case PixelFormat::RGB10A8:
            break;
        }

(this is really a minor thing, it&apos;s just something I commonly get wrong due to my desire for parallel construction).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1760238</commentid>
    <comment_count>4</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2021-05-14 15:06:05 -0700</bug_when>
    <thetext>Yeah, allowing else clause in the case multiple conditions are parallel to one another makes sense to me.

But rather than leaving it to author &amp; reviewed to make arbitrary decision, can we codify that?

I&apos;m afraid some people will ignore this and start arguing / writing code without an early return even in the case where an early return and no-else clause is more appropriate because there is no logical parallelism between the two.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1762489</commentid>
    <comment_count>5</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2021-05-21 10:55:29 -0700</bug_when>
    <thetext>&lt;rdar://problem/78318197&gt;</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>