September 09, 2022

On Friday, 9 September 2022 at 14:22:51 UTC, Steven Schveighoffer wrote:

>

Instead of switch, you can use:

do {
   ...
} while(false);

And then use a break inside.

Not the same, continue applies to the do then, not an outer loop. Also not clear until the end of the construct that it's not a loop.

>

But honestly, all this looks like "I want to avoid goto at all costs". The original looks better to me.

It's easier to reason about switch because you don't have to read the whole function looking for the matching label.

>

Another possibility that uses goto, but might be preferred, is:

    if (FuncDeclaration func = sc.parent.isFuncDeclaration())
    {
        tm.symtab = func.localsymtab;
        if (!tm.symtab)
        {
            // Inside template constraint, symtab is not set yet.
            goto L1;
        }
    }
    else
    {
        tm.symtab = sc.parent.isScopeDsymbol().symtab;
    }
    assert(tm.symtab);
    tm.ident = Identifier.generateId(s, tm.symtab.length + 1);
    tm.symtab.insert(tm);
L1:

That's an improvement.

September 09, 2022

On Friday, 9 September 2022 at 14:18:04 UTC, Paul Backus wrote:

>

On Friday, 9 September 2022 at 14:03:57 UTC, Nick Treleaven wrote:

>

Where switch works like switch(0) default: today.

I think maybe a more intuitive keyword for this would be do:

do
{
    // ...
    if (whatever) break;
    // ...
}

...which would work like do { ... } while (0); currently does.

So continue would do the same as break? Bit weird.
Also you don't know if it's a loop or not until you find the closing brace. It's better to know in advance.

September 09, 2022

On 9/9/22 11:40 AM, Nick Treleaven wrote:

>

On Friday, 9 September 2022 at 14:22:51 UTC, Steven Schveighoffer wrote:

>

Instead of switch, you can use:

do {
   ...
} while(false);

And then use a break inside.

Not the same, continue applies to the do then, not an outer loop. Also not clear until the end of the construct that it's not a loop.

If we are talking about wrapping existing code that might contain a continue, you need to also consider that the existing code might contain a break. In both cases, you could label the target and use a targeted continue or break statement.

But again, goto is better here.

> >

But honestly, all this looks like "I want to avoid goto at all costs". The original looks better to me.

It's easier to reason about switch because you don't have to read the whole function looking for the matching label.

You don't though. Just search for it. Just like you would have to search for the matching closing brace of the switch. At least in the case of the label, you can use text search, and not have to account for nested braces.

-Steve

September 09, 2022

On Friday, 9 September 2022 at 15:57:45 UTC, Steven Schveighoffer wrote:

>

On 9/9/22 11:40 AM, Nick Treleaven wrote:

>

On Friday, 9 September 2022 at 14:22:51 UTC, Steven Schveighoffer wrote:

>

Instead of switch, you can use:

do {
   ...
} while(false);

And then use a break inside.

Not the same, continue applies to the do then, not an outer loop. Also not clear until the end of the construct that it's not a loop.

If we are talking about wrapping existing code that might contain a continue, you need to also consider that the existing code might contain a break. In both cases, you could label the target and use a targeted continue or break statement.

If you know there's no break but there is a continue, you don't have to use a label.

>

But again, goto is better here.

> >

But honestly, all this looks like "I want to avoid goto at all costs". The original looks better to me.

It's easier to reason about switch because you don't have to read the whole function looking for the matching label.

You don't though. Just search for it. Just like you would have to search for the matching closing brace of the switch. At least in the case of the label, you can use text search, and not have to account for nested braces.

Why would you need to stop reading the code sequentially and lookup the closing brace? With goto you need to to understand what you're looking at. That's my point.

September 09, 2022

On 9/9/22 12:31 PM, Nick Treleaven wrote:

>

On Friday, 9 September 2022 at 15:57:45 UTC, Steven Schveighoffer wrote:

>

On 9/9/22 11:40 AM, Nick Treleaven wrote:

>

Not the same, continue applies to the do then, not an outer loop. Also not clear until the end of the construct that it's not a loop.

If we are talking about wrapping existing code that might contain a continue, you need to also consider that the existing code might contain a break. In both cases, you could label the target and use a targeted continue or break statement.

If you know there's no break but there is a continue, you don't have to use a label.

So when you say "Not the same", you mean that <1% difference.

> > >

It's easier to reason about switch because you don't have to read the whole function looking for the matching label.

You don't though. Just search for it. Just like you would have to search for the matching closing brace of the switch. At least in the case of the label, you can use text search, and not have to account for nested braces.

Why would you need to stop reading the code sequentially and lookup the closing brace? With goto you need to to understand what you're looking at. That's my point.

If the closing brace is close at hand, then the appropriate label would be too. It will even stand out more.

But you are saying "read the whole function". I'm assuming you mean that it's far away from the break/goto statement.

If it's off-screen, the label wins hands-down. Using my editor I can find "L1:" much easier than I can find a specific "}"

-Steve

September 10, 2022
On Friday, 9 September 2022 at 02:00:28 UTC, Walter Bright wrote:
> On 9/8/2022 5:26 PM, max haughton wrote:
>>> The refactoring can come later in its own PR.
>> It almost never does though, let's be realistic.
>
> Quite a few of my PRs are refactorings.

You are much better than most in this regard but my point is that every at some point thinks "This is the million dollar goto, I've been waiting for this moment all year" or just that one goto couldn't posssibbblyy hurt, and locally they might well be right but in my experience with the exception of tight interpreter loops I *very* rarely see truly justified uses for goto.

They might look clever but they're usually a symptom of code that is to abstraction/design what goto is to structured control flow.

If the code does eventually get refactored then great but especially in a professional environment where you might be more able to manage a larger refactoring, it's better to pay the refactoring cost now rather than later if at all possible.

Another more subtle psychological factor against using goto is that sometimes gotos are actually worth it, fair enough, but now other people working on the code can see the goto in the corner of their eye and think they can program like it's 1999 and blame it on the goto.

Replacing gotos with a void function call return is a nice pattern that D luckily makes trivial to use, for anyone reading unaware of the pattern.
September 10, 2022

On Friday, 9 September 2022 at 17:04:44 UTC, Steven Schveighoffer wrote:

>

So when you say "Not the same", you mean that <1% difference.

That's where bugs hide.

> > > >

It's easier to reason about switch because you don't have to read the whole function looking for the matching label.

You don't though. Just search for it. Just like you would have to search for the matching closing brace of the switch. At least in the case of the label, you can use text search, and not have to account for nested braces.

Why would you need to stop reading the code sequentially and lookup the closing brace? With goto you need to to understand what you're looking at. That's my point.

If the closing brace is close at hand, then the appropriate label would be too. It will even stand out more.

But you are saying "read the whole function". I'm assuming you mean that it's far away from the break/goto statement.

If it's off-screen, the label wins hands-down. Using my editor I can find "L1:" much easier than I can find a specific "}"

Why do I need to stop reading the code sequentially and find the closing brace of a switch? I do not. Searching for a label is disruptive, even if automated, it breaks my train of thought. With goto the matching label could be inside the switch or it could be outside it, at different levels of scope. With break, you know it just jumps to the end of the switch. When I'm reading code, I'm trying to understand each statement. I can't understand a goto without knowing which scope it goes to. With break you know what scope that is. This is a key part of why we have structured programming, because you know where the code will go to when exiting a construct.

September 10, 2022
On Saturday, 10 September 2022 at 06:27:32 UTC, max haughton wrote:
>
>
> Replacing gotos with a void function call return is a nice pattern that D luckily makes trivial to use, for anyone reading unaware of the pattern.

Just to be sure could you please give a small example.


1 2 3
Next ›   Last »