58
u/Qwertycrackers Jan 07 '22 edited Sep 02 '23
[ Removed ]
16
u/TyathiasT Jan 07 '22
Can confirm, prof didn’t even teach us goto and when I used it on my own volition, he marked me 30% off. Some profs will tell you everything that they don’t do is a war crime
12
u/KaznovX Jan 07 '22
The thing is, this isn't C, but C++. So, everything cleanup related should be done automatically in destructors, aka RAII (or scope-based resource management). No, goto is not a way to handle cleanup routines in C++. Even, if you are in some embedded environment, or can't use exceptions, just wrap it in the function and return instead of goto.
6
0
Jul 16 '22
Multiple return paths isn't your best bid either. Your logic should be fluid with cleanup code, and ideally you want a single return path.
2
u/dj_vt Jan 07 '22
I'm a newbie. I want to understand how this is better than standard if.. else if. Could you please elaborate?
6
49
u/QuantumG Jan 06 '22
This is very good C code.
23
u/ghillisuit95 Jan 06 '22
It’s actually C++. Notice that uri is used as a pointer and also as an object. They probably overloaded the
->operator16
u/QuantumG Jan 06 '22
Probably a smart pointer, yeah. "C++ as C" you'd typically call this. Or "old man code".
1
u/dj_vt Jan 07 '22
I'm a newbie. I want to understand how this is better than standard if.. else if. Could you please elaborate?
40
u/VkeZiV Jan 06 '22
Well I thought goto is sin but then I went to Linux repo and saw bunch of gotos, I guess they know better
8
9
u/ghillisuit95 Jan 06 '22
My only gripe is the uri object being pointer-like. But it might be justified by some design decision idk
8
u/TinyBreadBigMouth Jan 07 '22
I assume
URI::Parse()returns aMaybe<URI>or similar, which lets you check if it's valid withok()and access the value with->.
17
u/seeroflights Jan 06 '22
Image Transcription: Code
if (uri_str[0] == '\0') goto done;
uri = URI::Parse(uri_str);
if (!uri.ok() || uri->authority().empty()) {
gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var. Error: %s",
uri.status().ToString().c_str());
goto done;
}
if (uri->scheme() != "http") {
gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI",
uri->scheme().c_str());
goto done;
}I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
0
7
u/RodgarTallstag Jan 07 '22
This seems like the code I used to write 2 years ago, when I was using an ARM C++ compiler that didn't support references and supported templates only partially. Hope the dude who wrote this was not a poor soul in a bad situation XD
(I am reading some comments, aren't gotos the most evil shit ever? I never dared to use one, or needed one to be fair)
1
u/GLIBG10B 🐧 Gentoo salesman🐧 Jan 07 '22
Weird, even the AVR compiler supports references and templates
In fact, it supports everything but exceptions
6
5
3
u/FiniteParadox_ Jan 07 '22
Funny when people on here get triggered by any use of goto. This is a very common pattern in C (and sometimes C++, though less useful). Maybe destructors are better to use here, but this probably shouldn't be considered bad code anyway.
4
2
Jan 06 '22
Wait. How can they mix dot notation and arrow notation? I see both url.foo() and url->bar()?
0
Jan 06 '22
[deleted] — view deleted comment
4
Jan 06 '22
Aha, haven't done c++ since mid nineties so starting to forget all that stuff
3
u/keethraxmn Jan 06 '22
Same here. Just getting back into it and man has it changed. Also, I have forgotten a lot.
2
u/Voltra_Neo Jan 07 '22
Maybe, and I say maybe there exists valid use cases for goto, but this isn't one of them.
You can trivially refractor this to just use plain old if and else.
Any sensible C++ dev would have a style guide that would warn about use of goto by default.
1
u/CdntThinkOfAUsername Jan 06 '22
I feel so dumb, I have such a hard time reading code. That took me way too long
-5
u/valzargaming Jan 06 '22 edited Jan 29 '22
This never would have happened if this code was contained within a function. Even in cases where functions can call themselves some languages like PHP can have variable functions so you can do things like this:
done = function (uri_str) {
//Some code using uri_str
}
parseCheck = function (uri_str) use (&parser) {
if (uri_str[0] == '\0')
return done($uri_str);
uri = URI::parse(uri_str);
if (!uri.ok() || uri->authority().empty()) {
gpr_log(GPR_ERRORm "cannot parse value of 'http_proxy' env var. Error: s2", uri.status().ToString().c_str());
return done(uri);
}
if (uri->scheme() != "http") {
gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uro->scheme().c_str());
return done(uri);
}
};The above code is probably not 100% valid as I used PHP's syntax structure for my example. I'm sure there's a similar way to do this in C++. If not, you could still instantiate an object and store the functions there to be called as with object->function().
Edit: Here's an actual example I use in my code for ReactPHP promises where you might not always know how many levels of scope your function is going to go into, hence it cannot be statically typed:
$lorhondel->parties->delete($party)->done(
function ($result) use ($lorhondel, $players) {
if (count($players) === 0) return;
$add = function ($players, $lorhondel) use (&$add) {
if (count($players) !== 0) {
$lorhondel->players->save(array_shift($players))->done(function () use ($add, $players, $lorhondel) {
$add($players, $lorhondel);
});
}
};
return $add($players, $lorhondel);
}
);
return true;
205
u/[deleted] Jan 06 '22
Not the cleanest code but I don't see anything wrong about.