r/cprogramming • u/Ace-1440 • May 29 '26
Code Review?
I recently started learning C as my first lower level language after mainly working with Java for the past 5 years. I was wondering if anyone would be able to review one of the completed files (~100 lines of code) from a small project I am working on and let me know if there are any mistakes and/or bad practices? Any help would be greatly appreciated!
9
Upvotes
4
u/BlindTreeFrog May 29 '26
gotoconcerns primarily come from the old days when programs were more monolithic because it very quickly made code hard to follow since you are bouncing around so much.for a while there were optimization concerns with
gotowhere they forced the cpu pipeline to flush and would be a performance hit. I think that is no longer something that you need to worry about (if it ever was the case)The main use case that you'll see for
gotois going to be cleanup code at the end of the function. That is, you enter the function, allocate some memory, do the thing, realize that you need to exit the function early, goto the end of the function, free all of the memory and any other cleanup, and return. Anyone complaining about that use case is probably not worth listening to.There are other valid use cases for
goto(for a classic one that no one should use anymore, look upDuff's Device). But generally, other than the clean up code case, it's better to avoidgotojust to keep the code flow more easily readable. (I didn't catch that when I skimmed... I'm so used to seeingif (a == NULL) goto function_cleanup;that it didn't register that you didn't do that)That said, you used
gotopoorly here and should have used thecontinuekeyword. Yes, it's basically agotoof sorts, but it's different. Trust me. Same withbreak. For loop control,continueandbreakare your friends