<div dir="ltr">On 1 October 2013 21:22, Chris <span dir="ltr"><<a href="mailto:wendlec@tcd.ie" target="_blank">wendlec@tcd.ie</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Just a short question. Usually goto statements are frowned upon as being bad programming style (in textbooks at least). D has it (thankfully) and I've used it, albeit sparingly. Sometimes goto is simply the best and most efficient solution within a code block (to avoid code duplication, unnecessary checks or redirecting to yet another function blah blah). Is it ok or even necessary to use goto in D? Or does the compiler recognize _obvious_ cases and generate code accordingly? For example would it turn something like this<br>
<br>
// ...<br>
if (word.length == 1) {<br>
// format output<br>
return output;<br>
} else if (word.length > 1) {<br>
// do some additional processing<br>
// format output<br>
return output;<br>
}<br>
<br>
into<br>
<br>
// ...<br>
if (word.length == 1) goto FormatOutput;<br>
<br>
// if word.length > 1, some extra work has to be done<br>
// initialize some variables, parse, do some processing etc.<br>
<br>
FormatOutput:<br>
// .....<br>
<br>
return output;<br></blockquote><div><br></div><div>Well, obviously that should be rewritten:</div><div><br></div><div> if (word.length > 1)<br></div><div> {</div><div> // additional processing<br></div><div> }</div>
<div> // format output<br> return output;<br></div><div><br></div><div>Note: there's an un-handled case in your example, but I'll ignore that.</div><div>Anyway, goto is supported. Walter likes it. I use it from time to time.</div>
<div>I'd say 90% of the time I find goto useful is when I need to bail from nested loops. I've often wondered if something like break(2) would be a more elegant solution to the breaking out of nested loops problem.</div>
<div><br></div><div>But in direct answer to your question, I think you'll find modern optimisers are smart enough to make the optimisation you are looking for. I haven't tested that precise case, but I've definitely expected the optimiser to do the right thing in similar cases, and it does. I rarely write code that requires me to have faith in any optimiser though.</div>
</div></div></div>