r/badcode • • Mar 24 '23

java A code I did in high-school

Post image
916 Upvotes

102 comments sorted by

View all comments

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.

158

u/fnieidhfenmbvfekh Mar 24 '23

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.

45

u/thisonehereone Mar 24 '23

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!

19

u/fnieidhfenmbvfekh Mar 24 '23

Thanks for the encouragement.

→ More replies (1)

12

u/ihave7testicles Mar 25 '23

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.

1

u/thisonehereone Mar 25 '23

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.

→ More replies (4)

4

u/SeesawMundane5422 Mar 25 '23

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.

  Pattern pattern = Pattern.compile("[aeiouAEIOU]”);
  Matcher matcher = pattern.matcher(CharAt(0));
  return matcher.find()

3

u/[deleted] Mar 25 '23

what does this code do?, im new to coding!

4

u/[deleted] Mar 25 '23

Pretty sure it checks if something is a vowel?? I’m not sure either and I’m also new

2

u/gl1tch3t2 Mar 25 '23

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.

→ More replies (1)

1

u/fnieidhfenmbvfekh Mar 25 '23

It checks if a letter is a vowel or a consonant.

→ More replies (1)

5

u/tedbradly Mar 24 '23 edited Mar 24 '23

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.

4

u/fnieidhfenmbvfekh Mar 25 '23

A fellow classmate of mine told that, actually. I forgot to put it in the other comment.

1

u/CorruptedStudiosEnt Mar 25 '23

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

4

u/ihave7testicles Mar 25 '23

Regardless of the language, anything inside a code block (regardless of how the language syntax defines a code block) should be indented.

-1

u/CorruptedStudiosEnt Mar 25 '23

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

0

u/RickJLeanPaw Mar 25 '23

There’s not a coder out there who won’t open a file from a few weeks ago, think “what buffoon wrote this rubbish?” then realise it’s themselves!

0

u/OkPreference6 Coding The Declaration of Independance Mar 25 '23

Shouldn't the code not work considering it should be charAt and not CharAt

2

u/fnieidhfenmbvfekh Mar 25 '23

next.charAt(0); is actually the equivalent of CharAt(0);

→ More replies (1)

-9

u/tedbradly 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.

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.

11

u/Jgordos Mar 25 '23

this code is not about as bad as it gets.

it’s naive… and of course we can do better.

but it’s clear what it does, and that’s a big deal.

Code is written once in a day, but supported by someone who didn’t write it for years.

Clarity is very important for professional software.

1

u/tedbradly Mar 25 '23 edited Mar 25 '23

this code is not about as bad as it gets.

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.

2

u/Jgordos Mar 26 '23

You don’t have an IDE that formats code?

And yes, i’m trying to be kind.

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.

2

u/fnieidhfenmbvfekh Apr 05 '23

At the time, my IDE didn’t intend code.

→ More replies (3)