New to coding, PLEASE roast my code. :3
Posted by SnooAvocados9393@reddit | programming | View on Reddit | 13 comments
Posted by SnooAvocados9393@reddit | programming | View on Reddit | 13 comments
lewster32@reddit
Looks fine to me. If I was being picky, the else on line 24 is unnecessary as you're exiting the process before it. I often see people write, e.g.:
When it could more simply be:
Since both paths return (or exit) early, the else is redundant.
Onheiron@reddit
Actually the case i different, it's more like
Also what's wrong with
It's clear and straight forward. What are you optimizing by taking away the else branch?
lewster32@reddit
The
coinFlip
var is aboolean
so once you're in that code path, one of those outcomes is guaranteed. If the value is true, it'll print and exit, otherwise the only other possible outcome is another exit (or a throw, but that's unlikely).It's not really a matter of optimisation (the compiler will almost certainly optimise this anyway) and you're right that the else is maybe a bit more... visually balanced? But it's still unnecessary, and an understanding of why leads one to good practices like guard clauses, and generally making code flatter and more readable.
programming-ModTeam@reddit
This post was removed for violating the "/r/programming is not a support forum" rule. Please see the side-bar for details.
Kaimito1@reddit
I don't do java but one thing I think that could be helpful is to encase your static string operations in an Enum.
That's a good practice to have so if you ever need to change the sting it'll be in only one place (if you do end up using the same string in multiple places)
Onheiron@reddit
Why and enum tho, why not just global constants you can change via config?
Kaimito1@reddit
Depends on how big the project will be I guess.
If it's just scoped to this class then I think an Enum inside the class would do. If you end up using it in other places then you can upgrade it to a global constant at that point imo or something more reusable
Onheiron@reddit
Makes sense I'm just not too fond of "Enum.valueOf", unless you associate a string with each enum entry and convert user input with enum entry by matching that string. Otherwise you're linking the actual enum name with the exact input string, that'd be like "the name of the const should match the string, not its value".
Kaimito1@reddit
Oh yeah I agree.
If I'd do it in JS it'll be (I'm on phone so TS is a pain)
Unsure how java would do it but that's what I'm imagining.
Onheiron@reddit
deal :)
pdpi@reddit
Line #13 works fine for what your program does right now, but it's position is a smell, for two reasons.
First, random() is a pretty cheap operation, but it could be much more expensive. You shouldn't be doing that work at all unless necessary. Second, a natural "improvement" to your program as it exists right now is that it would repeatedly ask what you want the user to do until they reply with "quit" or "done" or something like that (in fact, I suggest you do exactly that as an exercise!).
Put those two things together, and it makes much more sense to have that call to random() inside the if in line #20. Likewise, the sum in line #38 should be inside the if in line #48 (just like you're already doing the other three operations).
Speaking of those operations, that's a good place to introduce the switch expression:
Onheiron@reddit
Beside very bad formatting, many empty lines and capitalized S in "Subtraction" it seems fine.
Venthe@reddit
r/learnprogramming