S
S
sorry_i_noob2018-07-08 20:00:14
PHP
sorry_i_noob, 2018-07-08 20:00:14

I've heard that goto is NOT RECOMMENDED to use. What about while(true)? There are many if's in the function that have return. Is while(true) bad manners?

Hello. I've heard that goto is NOT RECOMMENDED to use. What about while(true)? In my function (inside while(true)) there are if's in which return. Is while(true) bad manners in this case? And is it bad manners in general?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
L
longclaps, 2018-07-08
@longclaps

I heard a ringing)))
For some reason, it seems to me that the exit from a language construct (a cycle, or a function, etc.) into the code that encloses it does not create any difficulties.
But jumping inside such a construction, on an arbitrary line, is fraught.
Try to think this thought on your own, but if it doesn’t work out - just remember it)

A
Artem Spiridonov, 2018-07-08
@customtema

Show a specific example. And on what EP, in what environment.
while(true) and whatever you describe is normal for real time systems. And very bad for web applications and the like.

A
Alexey Makarenya, 2018-07-15
@makarenya

The last time I used GOTO was in QBasic (you can google it). It was my first programming language, and I myself studied at school, and not even in high school. You can always do without it, although sometimes it is not so convenient. But I have seen it repeatedly used in serious projects. Almost always it is the only one for several dozen files with code.
Rejection of GOTO is one of the principles of structured programming. And one more of his principles is a single exit point for any design. In essence, this is a refusal to return, except for the case when you need to return some value at the very end of the function, and also the refusal to continue or break in cycles. And... this is no longer done in any company where I had a chance to work, in any open source project, the code of which I had a chance to look at.
The reason why one principle is learned by heart by all programmers, and the second is just an excuse to laugh is their price. A program rich in goto becomes unreadable, unpredictable, and unmaintainable very quickly. It becomes impossible to understand what is happening in it after a couple of hundred lines. But with return from the middle of the function, as well as with break from the middle of the loop - the price is low. It's not so easy to turn a program into trash with them. Readability and maintainability do not fall, including when using infinite loops. And in some cases, without them, nowhere. For example, any program for a microcontroller is an endless loop and without any possibility to exit it.
So often an infinite loop is an opportunity to make the program simpler, and not to duplicate lines of code once again - that is, to simplify the understanding and maintainability of the program, and this is much more important than blindly following the old principles. On the other hand - if you use infinite loops almost as often as everyone else - then the question already begins to arise - are you familiar enough with loops in the language in which you write. Indeed, very often even the most non-trivial constructions and features are solved using the for loop - in this case it is extremely powerful (if we are talking about c/c++/c#/...)
PS. Breaking out of a bunch of nested loops with return from a function is the easiest way to avoid the need for a GOTO and at the same time avoid overhead on extra and unnecessary conditions. Used by everyone and with a bang

#
#, 2018-07-08
@mindtester

1 - bad manners
2 - another question is that sometimes it's a cool solution (when you reduce the number of lines of code ... to one or two)
... if there are more than 2 lines of code - you are digging a hole for yourself ... maybe everything will work fine .. or maybe you will have to spend a lot of time in the debugger... and it may not immediately appear a combination of circumstances that destroys everything
ps this is a matter of code readability/comprehensibility. when lines 2-3 - readability is easy

A
ApeCoder, 2018-07-09
@ApeCoder

by while(true) it is not clear what exactly is the exit condition from the loop. That is, it is obvious that the cycle ends sometime, but the exit condition is spread over its body. Also, if the loop exits, it is also difficult to understand it as a black box - i.e. adding, for example, to the end of a function of some code does not guarantee its execution.

D
Doc44, 2018-07-23
@Doc44

while(true) is fine.
but if you fuss while(true) wherever possible - it's bad.

P
pqgg7nwkd4, 2018-08-25
@pqgg7nwkd4

I somehow needed to implement a complex algorithm presented in the form of a block diagram from the documentation. And I used goto, and I didn’t care that it was bad manners, there was no desire to translate it into cycles. And in everyday life it does not occur to me to use it.
while(true) is also quite a convenient construction, because her block can be repeated or exited at any time. It is convenient, for example, to check the parameters of a function. It seems to me that the languages ​​lack a construct for this.
The author, you can say so: you can write code with and without goto for good and bad. Don't worry about that

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question