It works right? You can't be down on yourself for high school code. This is an opportunity to look at this and say - How can I make this better? Which is what your instructor should have been doing with you.
Yes it works, and yes my teacher told me to check if the letter was between 65 to 90, or 97 to 123, and then only check for the vowels. This was actually homework. I’m not down on myself, I just wanted to post it here for laughs. Currently, I could do something that’s much better than this.
Awesome. I like the attitude that you know you can do it better. I think those are some of the 'ah-ha' moments when you start to look at something in a different way, which leads to having a better understanding. Keep it up!
I like your encouragement. This is how I feel as well. As a Senior Software Engineer who's been doing this for 30 years, I always say the best way to write code is to "Make it work, then make it work well, then make it look good."
This code is perfect for the stage it's in, it's just in the "Make it work" stage.
OP, thanks for sharing, it's the same thing any of us would have done when starting out.
Always make it look good and readable. Don't just make it "work" think about how to make it work well, and iterate/improve upon further later as needed.
I think this is key. The make it work stage is just figuring out how it works. This goes for anything from your first language to a new language or library.
Once you know how it works, make it more readable. I’ve personally found quick comments help a lot. They’re just for me and get replaced by clear and concise comments where needed.
hey there, just saw this. I have a very similar philosophy as you. make it work then make it pretty. Sometimes I don't like the sub, because it's not bad code, it's just evolving. also I didn't realize OP was still in high school, I thought it was a look back. have a good one!
Code bros. I love that our philosophies are the end result of completely different experiences, but we got to the same place. Too much ego sometimes instead of passing on knowledge and lifting others up.
TL;DR The code would/should tell you if the character/letter provided is a vowel.
From a guess telling the user if the letter they provided is: a vowel, a consonant, or neither. The whole program isn't visible so can't be 100%. But you are both correct in that it will definitely at least (or should) tell you if what you provided is a vowel. The reason I make the assumptions above about it not just being vowels is that, if it were OP could have ended the program basically right after the System.out.println(Letter+" is a vowel") line (a few extra things would be needed like closing the switch statement and a few curly braces, etc). And why a consonant or a vowel instead of anything else like a number is because they named the variable letter.
I hope that makes sense, I'm on phone so I've tried to be concise.
Yes it works, and yes my teacher told me to check if the letter was between 65 to 90, or 97 to 123, and then only check for the vowels. This was actually homework. I’m not down on myself, I just wanted to post it here for laughs. Currently, I could do something that’s much better than this.
Did your teacher not tell you also to use a lowercase or uppercase function as well to reduce the number of cases? Also, it's considered bad practice to have comments that repeat what the code expresses. int i = 5; // set i to 5
I hope she also told you about the indentation conventions that help someone visually understand the structure of the code. More or less, something inside braces should be indented by 2-4 spaces. That doesn't express it completely but should get the idea across.
The last thing to mention is your coding style has an inconsistency when it comes to the use of final. scanner is not final despite it meeting the requirements for it whereas letter is. Now, most style guides for Java recommend avoiding final except for members of a class or where it's required for code to compile. However, if you choose to use a style with final everywhere, you should at least be consistent.
I was so ready to fight you over indentation standards, but I'm going to let this one go because I don't code in Java, so for all I know that is the standard convention for it lol
Having it be indented at all isn't what I take issue with. What kind of psychopath thinks two or three spaces is an appropriate number? I'll still fight over four, but at least that's still actual indentation. lol
Not clear to me if you’re saying that checking for 65-90 is how you would do better… but… for reference this is one of the few cases where regular expressions make code easier to read.
It works right? You can't be down on yourself for high school code. This is an opportunity to look at this and say - How can I make this better? Which is what your instructor should have been doing with you.
The code is still about as bad as it gets. You're reading too much into the situation. Most code in this subreddit works and is just poorly written. Sometimes, the submission is about a bug.
The code is so bad that basically only someone brand new to programming would create it. I'll summarize below everything wrong with it for your benefit since you've self-reported your inexperience with your comment.
There are no indentations. Basically, every single style guide in existence leverages indentation to make the structure of the program's statements readily perceivable. This alone qualifies anything written as r/badcode.
The expression of the algorithm uses way more code than needed. First, a call to a lowercase or uppercase function will chop the number of cases in half. More importantly, ascii math can be done to guarantee the character is a letter, and every consonant case can be removed, replaced with a single default.
The comments are about as bad as humanly possible. Every style guide recommends avoiding comments that simply restate what the line of code states e.g. int i = 5; // set integer i to 5. It's code poison to comment every single line, repeating the exact thing the code says.
The use of final is inconsistent. If they want to use a style where final is used wherever possible, scanner should be final as well since its reference never gets reassigned.
Worse, style guides for Java warn not to use final wherever possible, because almost every variable you make becomes final. It ends up transmitting almost no guarantees / understandings while polluting your codebase. The only two times people recommend the use of final are for class members that the constructor initializes and where it is required for code compilation (like when capturing a variable in a lambda).
If you are a proponent of clean code as described by Uncle Bob, the condition should be refactored into a named function like isVowel, so the code can read like if (isVowel(letter)). This could be as simple as a private method in Letter.
Letter isn't a good name for a class. Something more expressive like DetectVowelsMain is much preferred (another complaint from Clean Code by Uncle Bob).
Hopeful optimism and kindness are great, but it's just patronizing and destructive when it's a straight lie to a new programmer who needs to hear what is what. If someone is trying to run by crawling on the floor, about the only thing you can sensibly say is they need to stand up and launch into a stride.
you can take any approach you’d like. i personally believe learning takes time. a junior programmer will not be able to take in everything you say and internalize the information after a single code review.
he can certainly make the changes you’re asking for, but will he understand why you asked for them?
he’ll probably be able to parrot back what you said, but be careful what you tell them because they fall into “cargo coding“ paradigms.
I agree with all of your remarks regarding the use of final, and the need to refactor into subroutines…
in your eagerness to tell me how inexperienced i am, you neglected to read all of the other comments other people wrote.
i felt no need to reiterate the comments others had made, but no one ever mentions the need to write code that support engineers can easily read and maintain.
have a good career! i’m near the end of mine, and i hope i never work with a condescending ass like you ever again.
How easy it is to fix an error in code doesn't mean the error doesn't exist.
you can take any approach you’d like. i personally believe learning takes time. a junior programmer will not be able to take in everything you say and internalize the information after a single code review.
he can certainly make the changes you’re asking for, but will he understand why you asked for them?
he’ll probably be able to parrot back what you said, but be careful what you tell them because they fall into “cargo coding“ paradigms.
This is all nonsense. There is only one way to approach truth, and that's by talking about it. Telling someone good job after they make several huge errors (many of which are simple and will be immediately understood) makes no sense. Successful adults see constructive criticism as free lessons / free mentorship. The others have narcissistic, childish world views and take it as an insult, getting defensive. We can rest assured you're projecting some kind of "traumatic" experiences you've had while getting code reviewed, meaning you aren't the type to excel in the field from the bounty of free lessons people give.
I agree with all of your remarks regarding the use of final, and the need to refactor into subroutines…
in your eagerness to tell me how inexperienced i am, you neglected to read all of the other comments other people wrote.
Great. Then you realize the code is bad. I was worried for a second when, just 1 comment above, you said the code is not bad. It made it come off as you not being a programmer with more than a few hours of coursework under your belt.
i felt no need to reiterate the comments others had made, but no one ever mentions the need to write code that support engineers can easily read and maintain.
I'm not sure what you're talking about here, but yes, good code can be understood by a reasonable programmer.
have a good career! i’m near the end of mine, and i hope i never work with a condescending ass like you ever again.
I'm sorry you view constructive criticism as an insult despite having decades of experience where successful adults are supposed to congregate. Must have been horrible working with you, you being so touchy in how the truth is discussed.
in your opinion, is there only “good code” and “bad code”?
Are there degrees of “badness” and “goodness”?
There is good code and bad code. Obviously, there is also a range in between. Here, the code is about as bad as it gets. I've listed all of the problems. It's needless to say that any code sample that doesn't use core aspects of style that literally 99.999% of programmers use is about as bad as it gets. Writing 15 unneeded case statements is also pretty bad. It's more code to see, more code to understand, and more code that could have a bug in it. In this case, the alternative is not complex mentally, so using it is strictly better. Inconsistency in style is always bad as well. It's unreasonable to argue a person should arbitrarily follow two diametrically opposed guides at random times. final should be in that code twice or none at all. This is a clear sign of a novice, and it's clearly bad code as well.
This isn't about being cool or being dominant. It's just facts. Some people run twice as fast as others. Some people will never exercise, staying slow. Some people physically can't ever run fast due to disability. Others are running slow now and will run quite fast later. The snapshot here is the code is quite horrendous, and feedback about that fact can only help. The alternative is lying by omission, giving zero opportunity for the coder to learn quickly. They'll have to figure this stuff out at random by bumping up against the topics over time rather than simply being given a list of faults on a subreddit designed around code with faults.
You're forgetting they can do validation in one line and erase every single consonant case. The code is tremendously bad. It doesn't even use indentation. The comments are code poison, simply restating what the code statements say. There's also an inconsistent use of final where the input's variable is final but the scanner's isn't. Consistency is important for any style guide, but worse, most people recommend never using final since its guarantees are pretty lax, adding almost no value despite polluting almost every variable you have (except in the case of members of a class where final signifies the constructor will initialize its value + creates a compiler error if not initialized). E.g. Java at Google never uses final except for members or where it's required for the code to compile.
My 1st language was actually HTML, my 2nd language JavaScript, and Java my 3rd language. However, the 1st 2 I learned in middle school, while the 3rd in high school, so the difficulty level increased quite a bit.
If you’re looking into how to refactor this code, I’d suggest looking into using a list or dictionary and seeing if the letter or object you want is within that list. Much less lines of code, much easier to adjust when needed, and much more flexible.
You’re telling me! I’m too old for the entertainment bizz and thought I’d try tech. After my last career fair at school though I might take my chances as an old wash up dancer.
import java.util.Scanner;//importing the Scanner.
public class Letter{//Starting the program.
public static void Letter(){//Initialising the class.
Scanner Letter=new Scanner(System.in);//So that the user can input the letter.
System.out.println("Enter a letter.");//Asking the user for a letter.
final char letter=Letter.next().CharAt(0);//Storing the user input.
switch(letter){//Checkign for the letter that the user input.
case 'A'://If the user had input A.
case 'a'://If the user had input a.
case 'E'://If the user had input E.
case 'e'://If the user had input e.
case 'I'://If the user had input I.
case 'i'://If the user had input i.
case 'O'://If the user had input O.
case 'o'://If the user had input o.
case 'U'://If the user had input U.
case 'u'://If the user had input u.
System.out.print(Letter+" is a vowel.");//Printing an appropriate message.
case 'B'://If the user had input B.
case 'b'://If the user had input b.
case 'C'://If the user had input C.
case 'c'://If the user had input c.
case 'D'://If the user had input D.
case 'd'://If the user had input d.
case 'F'://If the user had input F.
case 'f'://If the user had input f.
case 'G'://If the user had input G.
case 'g'://If the user had input g.
case 'H'://If the user had input H.
case 'h'://If the user had input h.
case 'J'://If the user had input J.
case 'j'://If the user had input j.
case 'K'://If the user had input K.
case 'k'://If the user had input k.
case 'L'://If the user had input L.
case 'l'://If the user had input l.
case 'M'://If the user had input M.
case 'm'://If the user had input m.
case 'N'://If the user had input N.
case 'n'://If the user had input n.
case 'P'://If the user had input P.
case 'p'://If the user had input p.
case 'Q'://If the user had input Q.
case 'q'://If the user had input q.
case 'R'://If the user had input R.
I don't use OCR, as it's unreliable (especially for code) for the reasons list at section 3, here. This post wasn't much trouble though, as it's mostly copy and paste with small adjustments.
The default was for if the user enter a number, special character, or anything else that wasn’t a letter. I put 2 images but for some reason the 2nd image didn’t get sent through.
Create a vowels array, upper and lower case. Take in user input and split it into an array. For loop through the array and compare each element to the vowel array. Break on error with message
That’s a good idea. Unfortunately, at the time of making this code, I didn’t learn what arrays were. If it was in 10th grade, I would have done it the array method.
If I remember correct the very old days, there were discussions that this kind (of if statements) were faster when compiled into binary (there were no intermediate back then) than to write a loop through an array. C and pre 100mhz computers
Maybe you’re British as I know they don’t teach it there for some fucking reason, but yeah Y is sometimes a vowel. Examples include the word: “my”, “gym”, and so on… Yeah it is mostly a consonant but is sometimes a vowel. Good video on the topic: https://youtu.be/6ZXLQNI_HEU
Sorry for being a dick and assuming… Well it wouldn’t surprise me if English taught there is influenced by how the Brits teach it, because, imperialism, but yeah regardless sorry for assuming, and sorry for being a dick if I was rude. Also yeah that youtuber makes a lot of interesting videos on languages, worth looking into if you’re at all interested! He’s why I know the word “him” used to actually be gender neutral, it changed only like idk 300 years ago or so.
290
u/thisonehereone Mar 24 '23 edited Mar 24 '23
It works right? You can't be down on yourself for high school code. This is an opportunity to look at this and say - How can I make this better? Which is what your instructor should have been doing with you.
15
u/iEliteTester Mar 24 '23
It's not /that/ bad, You just need to:
4
u/LivingInAnIdea Mar 25 '23
Ah, the thrills of learning Java as your first language in a high school class. Glad you're coding habits are improving!
5
u/RudeZucchini1790 Mar 25 '23
I was a stripper in high school. Now I’m 33 finally learning to code and my programs probably close to that.
124
u/Shartyshartfast Mar 24 '23
At least you resolved the tabs vs spaces argument by choosing ‘neither’.
84
u/BananafestDestiny Mar 24 '23
28
u/LankySeat Mar 24 '23
I feel like using code from Highschool is like cheat codes on this sub.
27
u/[deleted] Mar 24 '23
// this is a sentence
// more words here
// to be or not to be
42
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 24 '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!
25
u/-Konrad- Mar 24 '23
The comments killed me
16
u/rutgerrk Mar 24 '23
"I have written more than a thousand lines of Java code!"
2
u/zonivii Mar 24 '23
I mean if it works it works but still there's a
default:for that 😋5
u/[deleted] Mar 25 '23
Can you please add some comments to your code? I can’t understand what’s going on here.
4
u/TurintheDragonhelm Mar 25 '23
Tbh this isn’t unreasonable for learning logical structure of code. Bad code writes good code.
1
u/[deleted] Mar 25 '23
Didn’t back in the day Java case-switches fell through?
1
u/Terranaform Mar 25 '23
Create a vowels array, upper and lower case. Take in user input and split it into an array. For loop through the array and compare each element to the vowel array. Break on error with message
1
u/Romejanic Mar 25 '23
What kills me more than the lack of a default case is how there’s zero indentation and comments on every line lmao.
Not to worry though, my high school code was pretty terrible too
1
u/UltimatePeace05 Mar 25 '23
Wait!, Did you ever compile this masterpiece?
3
u/Cybasura Mar 25 '23
Well, if its in high school, quite frankly its serviceable
It does the job
2
u/[deleted] Mar 24 '23
Not that bad could've been just if-else statements
2
u/[deleted] Mar 24 '23 edited Jul 04 '23
fuck u/spez
1
u/never_inline Mar 25 '23
That's pretty smart. You avoided writing printf for each case.
1
u/couldbe_dead Mar 25 '23
No. But i seriously appreciate the efforts done...
1
u/chessset5 Mar 25 '23
Better than the stuff I see in college
1
u/UnstoppableJumbo Mar 25 '23
Looks like something Copilot would generate
1
u/[deleted] Mar 25 '23
Better than if else for readability of code well done!
1
u/abrams666 Mar 25 '23
If I remember correct the very old days, there were discussions that this kind (of if statements) were faster when compiled into binary (there were no intermediate back then) than to write a loop through an array. C and pre 100mhz computers
1
u/coloredgreyscale Mar 25 '23
Not that bad for a beginner; it's good that you used switch instead of if / else if
1
u/randCN Mar 25 '23
Sir, this is Very good JAVA code Sir
1
u/ghua Mar 25 '23
I would use GOTO☝️
1
u/[deleted] Mar 25 '23
Genius
I remember the first time I had learnt about switch cases, I was so excited that I used it everywhere lol
1
u/Bo_Jim Mar 25 '23
When you're learning, there's nothing wrong with commenting every line. When you're learning, any code that works is "good code".
1
u/[deleted] Mar 26 '23
atleast you made comments
0
u/[deleted] Mar 25 '23
[deleted] — view deleted comment
2
u/[deleted] Mar 31 '23
[removed] — view removed comment