What do you think of this control block?
do
{
FILE f = fopen("file.txt", "r");
if (f == NULL) quit; /*goes to else part*/
/*success here*/
for (int i =0; i < 10; i++){
...
if (error) quit;
}
}
else
{
/*some error*/
}
Thiago Adams:
What do you think of this control block?
do
{
FILE f = fopen("file.txt", "r");
if (f == NULL) quit; /*goes to else part*/
/*success here*/
for (int i =0; i < 10; i++){
...
if (error) quit;
}
}
else
{
/*some error*/
}
I have come to the conclusion that lots of advanced and very
speficic control-flow structures are being invented to avoid
using `goto' -- a simple and generic solution, with the
additoinal bonus of keeping the semantically linear code
actually flat.
Your proposal is not bad: it is keeping the main code flat
and saves extracting the `do' part into a separate function
(to take advantage of `return`). The `else' keyword seems
misleading, because it is not strictly alternative to the
`do' part. Is it your purpose to re-use C's existing
keywords? If not, then consider `fail` instead of `quit`
and `onfail` instead of `else`. Also, `do' may be renamed
`failable', indicating a block with controlled failure.
P.S.: I am eternally unhappy with the control-flow mechanisms
in existing programming languages.
On 10/8/2025 8:14 AM, Anton Shepelev wrote:
I have come to the conclusion that lots of advanced and very
speficic control-flow structures are being invented to avoid
using `goto' -- a simple and generic solution, with the
additoinal bonus of keeping the semantically linear code
actually flat.
The problem with goto is define a name and to find this name
on the source code. Is it up or down?
[...]
[...]
P.S.: I am eternally unhappy with the control-flow mechanisms
in existing programming languages.
The problems with 'goto' go even farther than only the
technical lookup; it goes into the semantical domain when
jumping into and out of scopes (for example).
Janis Papanagnou:
The problems with 'goto' go even farther than only the
technical lookup; it goes into the semantical domain when
jumping into and out of scopes (for example).
All true. I almost never use upwards-going goto (except for loop-and-a-half), and I never jump inside scopes, only out
of them.
Alternatives:
try {
ÿÿ throw; //error
ÿÿ quit;ÿ //early success - exit without going to catch
}
catch {
}
ÿ What do you think of this control block?
ÿ do
ÿ {
ÿÿÿÿ FILE f = fopen("file.txt", "r");
ÿÿÿÿ if (f == NULL) quit; /*goes to else part*/
ÿÿÿÿ /*success here*/
ÿÿÿÿ for (int i =0; i < 10; i++){
ÿÿÿÿÿÿÿÿ ...
ÿÿÿÿÿÿÿÿ if (error) quit;
ÿÿÿÿ }
ÿ }
ÿ else
ÿ {
ÿÿÿÿ /*some error*/
ÿ }
No RAII ? Silly language !
Em 15/10/2025 18:04, Bonita Montero escreveu:
No RAII ? Silly language !
I would be happy to chat about the disadvantages of RAII.
I think this is for another topic, and someone could complain
that is not about C. I think it is about C, why not introduce
RAII in C.
Sysop: | Tetrazocine |
---|---|
Location: | Melbourne, VIC, Australia |
Users: | 14 |
Nodes: | 8 (0 / 8) |
Uptime: | 207:40:20 |
Calls: | 178 |
Files: | 21,502 |
Messages: | 80,307 |