While odd-looking, this is actually pretty readable (modulo the goto).
Yes, you could rewrite the conditional as
if (c == 'y')
{
goto start;
}
else if (c != 'n')
{
goto ask;
}
...which uses the fact that you'll only hit the else in the event that the first ifevaluates to false, but that's actually IMO not as straightforwardly-obvious as what the OP screenshotted.
personally, I don't like swapping between negative and positive boolean checks like that. So it might be 'less' code from a line standpoint, I think it's less 'readable' in that you don't have to swap between regular and invert bools.
Oh, absolutely agreed. That's sorta the point I was making -- that yes, you could drop the c == 'n' clause, but the reality is that the resulting code will be less straightforward to read.
...which means this maybe doesn't belong in r/badcode?
can be quite useful, if there are several look alike conditions and one of the branches should do nothing. Right here there is, and it looks like normal. goto is a much bigger issue in this code.
Edit: formatting and minor misprints, I was on my phone during blackout in Kyiv
Oh yes it is. Your code makes reader to analyze every branch until he finds the right one. The code with an empty 'n' branch clearly shows that only this branch is intended for user choice of 'n', no need to look elsewhere. But it's better to leave a comment to show the branch is left empty deliberately, like /*empty*/.
Further reading: R. Martin, Clean Code S. McConnell, Code Complete
I have been working as a developer since 1993 with everything between assemby to C, C#, Python, Type script and whatnot.
I think this is a good example of overdoing and complitating code that just makes it messy and harder to read. To me, the original take a one second glance to be 100% sure I know 100% of what it is going.
I agree that languages are just tools, but you should make it easier to everybody else to understand. Programming is about solving problems, and I would say in the most elegant way. If you have to do such a thing, then at least comment it and explain why, we are not the machines who needs to understand those technical expressions, we are humans.
Every normal programmer should be able to understand what a 10 lines code with random one-letter identifiers and no indents does; but that doesn't mean such code is adequate.
Since your responses have been steamrolled by downvotes I can't see what you said after this, but:
Read the room. Your code is good and very space efficient. You brought up a good point that I agree with. I had no trouble at all reading it because Im used to it. They're saying all of this about 'readability' because you're in a subreddit about bad code.
I mean, you shouldn't use a goto either. You should probably have a start() function and an exit() function. But that's the only thing that really screams out to me.
Eh. Presumably we're already in the start function. It doesn't make the control flow confusing, and you can tell what's going on without even seeing the labels. This goto is no less clear than a while loop - you can understand the intent without even seeing the labels. Also Dijkstra didn't say, "don't use goto ever or else."
Lol.... what? Have you written code before? There absolutely are control flow guarantees when you don't throw them in the trash by putting unnecessary goto's all over the place.
For example:
char c = 'y';
while (c == 'y'){
puts("\nGame stuff happens\n");
do {
puts("\nDo you want to Play agin(y/n)?\n");
c = getchar();
while (getchar() != '\n'); // Clear input buffer
} while (c != 'y' && c != 'n');
}
Please explain to me how this code can ever enter a totally arbitrary piece of code somewhere else in the program.
I didn't mean this code in particular, but in general.
If we are talking about well crafted code, I personally don't see how using a localized goto does not offer any control for guarantees. It's not like goto can fail and if you have a model of the state machine you are making you know exactly what it can do and when it will finish, if it will finish.
That's why I said it's "bad practice," not that it's always wrong. There are times when it is justified to use a goto. This is not one of them - because there is an equally simple solution that retains the control flow guarantees provided by the C language specification.
"It's not like goto can fail and if you have a model of the state machine you are making you know exactly what it can do and when it will finish, if it will finish."
No one programs things that are intentionally buggy. Bugs happen because programmers think they know exactly what they are doing but actually they don't. Literally every programmer does this. Using dangerous operations like goto just makes this more likely to happen, harder to pick up in CR, and harder to debug. The code that was posted - which is what I am discussing - does not gain anything by using goto.
All programs are state machines, with the exception of purely functional programs. Just because a goto is "good for this" doesn't mean it's the best choice.
So yeah, it is a state machine, but it only has three states - playing, getting input, and exiting. What's the point? If there were a ton of states and it was a horribly wad of nested loops and conditionals, I might agree that goto is warranted. But that's not what we have here.
If you don't understand the difference between an abstract interface with guarantees and the underlying implementation (which enforces them), then I don't think I'll be able to explain to you why this is a really silly thing to say.
If you think you are so wise to try to belittle me then you probably should explain yourself why this is a silly thing to say and why goto exists even in the first place and when if, while and for loops were born.
You think I'm the one trying to belittle you? Re-read our exchange lol. I don't have time to argue with sardonic know-it-alls. Go brush up on CS basics.
148
u/juicewr999 Feb 10 '23 edited Feb 11 '23
Normal User: What game is agin?
66
u/Explosive_Eggshells Feb 10 '23
Honestly you could just put return, exit, or a comment saying "intentional fall through to termination" and it's kinda smart
62
u/spencerwi Feb 10 '23
While odd-looking, this is actually pretty readable (modulo the
goto).Yes, you could rewrite the conditional as
...which uses the fact that you'll only hit the
elsein the event that the firstifevaluates to false, but that's actually IMO not as straightforwardly-obvious as what the OP screenshotted.62
u/This_Growth2898 Feb 10 '23 edited Feb 10 '23
Syntax
can be quite useful, if there are several look alike conditions and one of the branches should do nothing. Right here there is, and it looks like normal.
gotois a much bigger issue in this code.Edit: formatting and minor misprints, I was on my phone during blackout in Kyiv
32
u/[deleted] Feb 10 '23
This doesn't look bad to me. I might add
exit 0to the else if, but that's more of a nit than anything.
11
u/samdog1246 Feb 10 '23
Image Transcription: Code
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!
1
u/Moolordking Feb 12 '23
As an A-level comp sci student, this is worryingly similar to my classmates. Glad there aren't group projects at this level.
3
u/[deleted] Feb 10 '23
c == 'n'
Means "nothing to see here, and place a breakpoint when you want to see ir".
8
u/TheComradeTom Feb 10 '23
Oh god
Not goto
4
u/Ok_Afternoon_4200 Feb 10 '23
You can remove the else if statement but you get extra swag points for keeping it in
1
u/BlurredSight Feb 11 '23
A minimum of return 0 or exit in the c == n would've cleared it up
1
u/BartlebysCorpse Feb 11 '23
I've seen worse. It's kind of weird, but at least basically understandable at a glance.
1
u/-Konrad- Feb 11 '23
:’( no please
1
u/Mr_Stranded Feb 11 '23
I'm not even mad. That's kind of genius
1
u/Bisquizzle Feb 11 '23
This code reeks i don't know what some of you are talking about.
1
u/singularity-software Feb 11 '23
Go to void -> experience never ending emptiness 🤡🤹🤡
1
u/LemonFizz56 Feb 11 '23
Apart from the goto it's not too horrendous
0
u/paperCrane8937 Feb 11 '23
Seeing "goto" physical pains me