Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 27 |
Nodes: | 6 (0 / 6) |
Uptime: | 38:01:44 |
Calls: | 631 |
Calls today: | 2 |
Files: | 1,187 |
D/L today: |
22 files (29,767K bytes) |
Messages: | 173,681 |
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?
[...]--- Synchronet 3.21a-Linux NewsLink 1.2
[...]
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 {
-a-a throw; //error
-a-a quit;-a //early success - exit without going to catch
}
catch {
}
-a What do you think of this control block?
-a do
-a {
-a-a-a-a FILE f = fopen("file.txt", "r");
-a-a-a-a if (f == NULL) quit; /*goes to else part*/
-a-a-a-a /*success here*/
-a-a-a-a for (int i =0; i < 10; i++){
-a-a-a-a-a-a-a-a ...
-a-a-a-a-a-a-a-a if (error) quit;
-a-a-a-a }
-a }
-a else
-a {
-a-a-a-a /*some error*/
-a }
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.