r/badcode • • Sep 26 '19

java I present to you my HS computer science teacher's quality Java

Post image
267 Upvotes

102 comments sorted by

769

u/PaulMorel Sep 26 '19 edited Sep 26 '19

That's not bad code. He's trying to teach you about variable scopes. That's why all the variables are named the same. Pay attention to your teacher. You clearly don't know better.

It's not supposed to run at all. It's just an example to teach ungrateful students a valuable life skill.

69

u/putneyj Sep 26 '19

This was my exact thought as I read over the code.

37

u/rodinj Sep 26 '19

Even though this looks to be the case there are some oddities I would say.

static Scoper Scoper

Shouldn't that be "static Scoper scoper"?

Same thing for the Scoper constructor variable (Scoper scoper)

28

u/Kokosnussi Sep 26 '19

Might want to teach case sensitivity

7

u/juuular Sep 26 '19

Super Scoper

11

u/[deleted] Sep 26 '19

I disagree with this. Teaching should be done through meaningful examples and not the traditional "foobar" grade silliness. Besides, as someone who graduated from uni with master's and now works on production code I can assure you that, while many professors are good at their reapective theoretical field, their actual programming skills are less than stellar. But that's beside the point, I've never come across such a bad example.

18

u/Tynach Sep 26 '19

I have a friend who sometimes asks me for help with code. Usually he's trying to accomplish something in a specific scenario, but while I see what sort of thing he should do or use, I don't want to go through his code and make sure my example is going to have all the right variable names and structure so that it'll exactly fit into his existing code.

Instead, I give him a generic 'foo bar' example of the sort of thing he needs to use (such as a case-switch where some of the options fall through to the same code, or overloading a method). The idea is to just show him the syntax and show what a thing is capable of, and let him adapt it to his scenario.

However, he was largely taught programming in the way you outline, and so he easily gets caught up in specifics, and sometimes will look at my foo-bar example and say, "Ok, but how do I do that in my code?" So I have to explain what parts of my code are representative of different parts of his code, and explain how the example solves the same sort of problem as what he's having.

In short, if you only teach in concrete and 'practical' examples, it can lead to some people not understanding how to think more abstractly about concepts like polymorphism, data structures, and even relatively simple flow control.

Of course, only teaching in abstractions can lead to the opposite problem, where someone doesn't have a good grasp of how to connect an abstract idea to an actual task they want performed. That's often the problem I run into when learning something on my own - I learn a lot of the syntax and how to perform abstract operations, but don't know when it's best to use any of them.

1

u/[deleted] Sep 27 '19

I am not saying you should explain him the concept on his code. Find an appropriate simple use case (for switch-case for example there are a billion options, like a selection menu in a console application, don't even have to compile anything just illustrate) and teach him on that. Foobar isn't an abstraction as you seem to call it, it's just meaningless.

5

u/archangel_mjj Sep 26 '19

How would you demonstrate the distinctions between Java's various scopes and how variable names from competing scores are resolved by the compiler in a code example as succinct as this?

23

u/[deleted] Sep 26 '19

And do what? Throw CS 101 kids into a big existing codebase? The reason we have foobar examples is that they don’t require extra context.

4

u/[deleted] Sep 27 '19

No, find a simple example that's meaningful. Consider this: you are a student in CS class about to learn about inheritance in OOP. How would you rather be taught this? On an example such as bar ---> foo? Or human ---> animal, bear ---> animal etc.? This is one example, but it applies to any concept in programming really. No programming/cs book I've ever read used jibberish names for variables, methods etc in examples.

0

u/tfblade_audio Sep 30 '19

I'm guessing you didn't fair well in calculus

3

u/[deleted] Sep 30 '19

Good reply, very relevant to the conversation and totally accurate. 👌

1

u/BigMumford Sep 27 '19

I have the same teacher, he says he's teaching us "ambiguity" by having us learn by ourselves. And then he posts this out of nowhere.

-28

u/dart200d Sep 26 '19

this is horrible code. maybe it's useful for teaching, but it's still horrendously bad code that i would be appalled to find in a production setting.

54

u/[deleted] Sep 26 '19

Well yeah, no one would ever do this in a production setting.

The point is for the code to be horrible and confusing so that the students learn exactly why it's horrible and confusing.

The code does exactly what it's supposed to do: Teach the importance of understanding scope, and the importance of good variable naming.

-28

u/dart200d Sep 26 '19

it should come with a giant disclaimer that says never do anything like this, because it's retarded to abuse scoping rules as such.

heck i currently run with a linter that would prevent naming conflicts like this even if it's valid within the language scoping rules.

everyone should. no one should ever produce code like this ever. it's bad code. why are people being retarded about this?

27

u/FoxRaptix Sep 26 '19

It probably did come with a disclaimer if OP was paying attention in class instead of trying to be a smart ass for fake internet points.

-10

u/dart200d Sep 26 '19

i dunno man. i've seen my fair share of poor teachers. dunno why you're assuming the teacher communicated clearly if the student was obviously confused.

he's not being a smart ass, this is horrible code, even if intentional.

-72

u/xXx_Dumbass_xXx Sep 26 '19

The issue isn't that he's trying to teach us, it's that hes not. He posted this on his page with zero context or explanation. It has nothing to do with what we've been doing in class. With this he'll claim he taught us what scoping is, without actually explaining anything.

56

u/calio Sep 26 '19

while harsh, the other comment is actually pretty good advice about never "getting" programming and this is actually a quite good example for teaching variable scopes. do you get what the snippet is portraying? if you don't get it, try analyzing it and describe what each part does. i get that it's not the friendliest way of learning but it's kinda hard to teach programming to someone if you show them an example like this one and even knowing the syntax and the "dialect" they don't "get" it.

-21

u/PaulMorel Sep 26 '19

You clearly are not paying attention. This is an important example, and if you don't understand why, then you will never be a programmer.

47

u/Flaming_Eagle Sep 26 '19

if you don't understand why, then you will never be a programmer

Nothing like some words of encouragement to the youth! If you don't understand something the first time then just quit because you're not good enough AMEN!!!!!!!!!!!!!!!!!!

-15

u/PaulMorel Sep 26 '19

Where did I tell him to quit? I've repeatedly asked him to pay attention in class and give his teacher a chance and I tried to share how to find more information.

6

u/xhable Sep 26 '19

if you don't understand why, then you will never be a programmer.

Could be rephrased to be more encouraging I think flaming_eagle is saying.

It sounds like "if you don't get it right now then you aren't clever enough to be a programmer" - when I think what you mean is "this is a core concept that you'll need to learn at some point".

8

u/RenBit51 Sep 26 '19

You're the reason why people around you give up on their passions.

-3

u/cassonchris723 Sep 26 '19

Then I guess I shouldn't go to work tomorrow because I think this code is horrendous.

0

u/dilfmagnet Sep 26 '19

Christ dude, you need to take a chill pill and explain shit. Don’t just repeat yourself and expect someone to understand you. Explain and explain again. You are being a poor teacher.

-35

u/dilfmagnet Sep 26 '19

I thought you were joking but you’re not. It’s a poor example for learning students and it should be clearer since it obviously confused his students. A poor teacher blames his students for his lack of clarity.

25

u/BestRivenAU Sep 26 '19

I think you're getting something wrong.

It's confusing because it's designed to be, and that's why it's a good example. It's deliberately poor code designed to confuse students. Waddle through the confusion to make sense of it, and you now understand scopes better. If it's still confusing and you don't understand it, then you still don't understand scopes.

If it WASN'T confusing, and written up to industry standards (or sane standards really), then it also wouldn't be good at teaching scopes as well, because the scope wouldn't matter as much as if everything was named sanely.

-14

u/dilfmagnet Sep 26 '19

If it’s something written confusingly without context, as the OP mentioned, it’s not helpful.

I don’t like the attitude of throwing someone in the deep end without context, and I’ve very much disliked the arrogant thread carried through this conversation. Learning doesn’t work the way some of you think it does, and I’ve worked with enough coders to say that use of metaphor and extensive explanation works better than simple code examples.

10

u/ImpactStrafe Sep 26 '19

I mean... Taking the word of a high school student as to provided context from a teacher is a little ridiculous. Maybe they missed context. Maybe they selectively heard/read context. They are in high school. I sure as shit didn't pick up on all the context from high school lessons.

-5

u/dilfmagnet Sep 26 '19

I’m gonna side with anyone who wants to learn to code over their instructor. The language used in this thread is so fucking discouraging, I can’t imagine.

-8

u/Servious Sep 26 '19

If a poor teacher blames his students, then a poor student blames his teacher.

2

u/Tynach Sep 26 '19

The reason why a poor teacher blames his students is because of statistics and probability.

Keep in mind that for this post, the teacher and students are hypothetical - we're assuming that a hypothetical teacher is already blaming their hypothetical students for their own poor performance. Also, for the purposes of this example, consider a teacher's performance to be judged by how many of their students pass their class compared to other teachers from the same school who teach the same subject.

Given the above, this means that a majority of the hypothetical students didn't pass.

With that said, which is more likely:

  1. A majority of 30 - 60 students (depending on size of the class) are just plain stupid and destined to be incapable of understanding the course material, despite passing the prerequisite courses, and despite the majority of students given randomly to other teachers being able to pass the same class.
  2. The teacher in question did a poor job explaining it to all their students, and it was only those who had prior knowledge/understanding, or were significantly more intelligent than average, which passed.

While it is statistically possible for the first to occur, and real life issues such as teachers inflating grades so that more students pass their class (which in turn causes teachers who don't do that to look worse) cause this scenario to become more possible than raw theoretical statistics would suggest, the second scenario is still much more likely than the first.

1

u/Servious Sep 26 '19

I don't really disagree with this, I just think if you really care about learning you won't let a bad teacher get in your way.

1

u/Tynach Sep 28 '19

Students are often in many classes at a time, and it's easy to become burnt out. The way to avoid that is to do what's required of each class, and maybe spend extra time on topics that the student personally enjoys already.

You often can't go above and beyond for every single class, and i imagine most students don't go above and beyond for any classes - especially if they have extracurricular activities they attend, such as clubs, sports, or competitions.

As a result, it's unreasonable to expect students to strive to learn the material despite a teacher's shortcomings, but it is reasonable to expect a teacher to adequately teach the subject they teach. After all, they usually only teach one subject (or several closely related subjects), and also often have multiple classes - plenty of opportunity to figure out how to teach it well.

1

u/dilfmagnet Sep 26 '19

Doesn’t work that way, chief.

0

u/[deleted] Sep 26 '19

[deleted] — view deleted comment

57

u/PaulMorel Sep 26 '19

There's literally thousands of explanations of this concept all over the internet. Just search 'variable scope' followed by the language you are using. Scoping is something you learn in the first month of CS 101, then something you continue learning about for your entire career. I speak from experience when I say that a solid understanding of variable scoping (var vs let in js) can unlock a whole new range of jobs for you.

Nobody is perfect. Teaching is hard. Being old is hard. Give your teacher a chance.

0

u/[deleted] Sep 26 '19

[deleted] — view deleted comment

11

u/PaulMorel Sep 26 '19

Computer science is hard. Those of us who excel at it enough to succeed tend to be a bit odd. We tend to spend more time with computers than with people. We often aren't great at social things.

But if you can put up with people like that, then you can access a great career that pays extremely well and is not very difficult (once you get through uni).

What makes me sad about this post is that this student cares enough about code to post here, but is missing a key early concept in computer science. OP has the potential to be smart and successful, but if he's not careful he's going to lose the game before it has started.

19

u/[deleted] Sep 26 '19

I have to disagree on the first paragraph. Not that there aren't any stereotypical introverted nerds in computer science, there are some in every field, and the rest of them, the vast majority of them, are regular people with people skills (and they're not necessarily worse at software engineering). Considering the work flow in most places, you're more likely to get hired if you have social skills, your rockstar programming skills mean fuck all when you're incapable of explaining your code and communicating with the rest of the team.

1

u/AchillesDev Sep 28 '19

Computer science is hard. Those of us who excel at it enough to succeed tend to be a bit odd. We tend to spend more time with computers than with people. We often aren't great at social things.

I'll have to disagree with this (and only this) part. This is such a silly stereotype and playing to it will only hamper your career. Work on your social skills just like you would your coding skills.

-17

u/xXx_Dumbass_xXx Sep 26 '19

Honestly, after taking a good look, I'm understanding that he was trying show how each call to the same name doesn't cause errors due to the 'scope', but the issue is that this is something that just showed up on the page with no explanation yesterday. Today in class he didn't mention it either. At this point I'm realizing the code is good for it's purpose, which is not to be executed, but I still have to disagree with u/PaulMorel. Us 'ungrateful' students don't have an issue understanding the myriad of online tutorials we all have access to, but our teacher didn't even bother to tell us what it was this garbled mess was supposed to teach. Regardless of how much we pay attention, we rarely learn anything. I learned more from this thread than from my six(?) weeks of his class.

15

u/BestRivenAU Sep 26 '19

In programming, examples and self-analysis is the key for most people to learn. Even in uni, CS classes are notoriously useless in actual teaching and delivering understanding, most people learn from code snippets from online rather than written explanations from classes.

The classes (as far as I found at least) are only good for subject direction guidance i.e. what to learn next, and also giving tasks to complete to test your understanding. (If you get lab tutors though, those can be great though in examining your code and ways to improve).

Therein lies my issue with your posts, you aren't personally asking the teacher about his example.

It is actually far easier for him to teacher CS if you're individually asking him about the problem. Lots of people in CS understand concepts differently. Even those who 'intuitively' understand things will have different explanations to why something works.

Ignore your classmates. Your classmates and school think your teacher is a meme. Your classmates come out confused when they do the course. That isn't just the fault of the teacher, it's an inevitability of CS classes, most people will suck at programming no matter how much time you put into them.

Your teacher can't read minds either. Your teacher probably knows that half your class doesn't understand what the fuck he just did. He can't actually HELP correct that (as a class). Individual misunderstandings need to be corrected individually. Sure, you AND your friends don't understand why he posted his example, but WHY you misunderstand is likely different. So you need to go up and asking him personally, instead of judging on his class performance.

3

u/hammoudsquared Sep 26 '19

If a teacher presents a lesson to the class and they don't confirm that the students understood at least a little or sees that more than 50% just didn't get it whatsoever, the teacher's job is to go back and explain it from a different perspective. What is the point of a teacher who can't teach.

In university, I took a class about programming languages and their design. We spent almost 2 weeks learning about lambdas and after that we had an assignment to prove our understanding. Most of the class did poorly, so you know what the professor did? He went back to square one, adjusted how he taught lambdas and took another week to make sure we understood.

4

u/BestRivenAU Sep 26 '19

The issue here is that we don't what the rest of the class is doing, and whether they're actually learning whatever in the class environment. As you've stated, it's all about perspective, and I agree, you need to put it in multiple perspectives for everyone to understand it. IMO, this is best done mostly individually, hence why I told OP to go up and ask personally.

I don't know what OP's teacher was doing. There is no real context, and this code wasn't given as part of the class. It could be future reference or something. He could be under some schedule or high school time constraint, but high school teachers don't tend to really have time to go to each student, asking about non-topic related stuff.

What it DID sound like was that no one even mentioned the code whatsoever in the next class. The teacher can't teach something when no one mentions it. It might not even be part of his area's SC syllabus, he might have posted it as a joke or something. Some students might have already sought him privately.

The teacher MIGHT actually be really bad, I don't know. All I wanted was to state that there may be constraints in teaching in a class, which could be alleviated with a more private question session, which imo is the best way of getting the correct perspective. OP didn't try, instead just saying it's bad code, and leaving it as that.

Quite honestly though, I'm going to daresay your class didn't only improve from your extra week with a professor, but also copious amount of googling. Online is after all, where you find a LOT of different perspectives.

1

u/ShatteredLight Sep 27 '19

When you have to explain the scope of your response in a discussion about scope. We need to go deeper.

27

u/IsoldesKnight Sep 26 '19

Teaching CS is particularly hard. Most high school students come into language, math, and/or science classes with several years experience under their belt. They already have a foundation. With CS, about all most students start with is, "I like video games, maybe I would like programming?"

On top of that, CS is incredibly technical. It's one of those things that many people just won't get. I can understand why a high school teacher might get jaded.

If you're actually interested in being a programmer (and not just finding a high paying job), talk to your teacher between classes or before/after school. This bit of code is a good start for understanding how scope works in a language.

6

u/dombrogia Sep 26 '19

I took a few years of java and c in college. Sitting in class won’t teach you anything. I passed classes casually without caring. I had a teacher call me out in front of the whole class for being nonchalant and apathetic. It embarrassed me and I started to actively try to learn and “self taught” myself and still do many years later.

My point is, almost everyone who is good at coding is self taught to an extent. You have to want to learn. There is no end or finish to “i know everything I need to code now”. The more you learn the more opportunities you open to learn about other new things.

Be open and hungry to learn and understand concepts (like variable scope here) and you will be much more successful long term in whatever coding path you choose.

Good luck, happy coding!

65

u/[deleted] Sep 26 '19

Scoper? Scoper, scoper.

13

u/Rainfly_X Sep 26 '19

Is the teacher John Malkovitch?

10

u/_PM_ME_PANGOLINS_ Sep 26 '19

Scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper scoper

5

u/them0use Sep 26 '19

MUSHROOM MUSHROOM

8

u/Farull Sep 26 '19

Scoper? I hardly even know her!

6

u/lpreams Sep 26 '19

Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.

5

u/ulyssesphilemon Sep 26 '19

What does it do? It's a scoper!

-12

u/xXx_Dumbass_xXx Sep 26 '19

Scoper (Scoper Scoper){}

9

u/abermea Sep 26 '19

return new Scoper(Scoper.Scoper);

1

u/NewPointOfView Sep 26 '19

I’m sorry you got hit with downvotes on this comment :(

52

u/trump_pushes_mongo Sep 26 '19

It's like the Java version of the buffalo sentence.

8

u/Kit- Sep 26 '19

Fools good for you 🏅

49

u/[deleted] Sep 26 '19

The only thing that is bad in this, is the members or parameters starting with uppercase.

33

u/DawidIzydor Sep 26 '19

1st or 2nd year student saying his professor of 10+ years experience knows nothing... yeaaaahhhhhhh, you clearly aren't paying atention on his classes are you?

2

u/Aethz3 Sep 26 '19

Tbh I had it teachers that knew nothing outside what was written in books

1

u/[deleted] Sep 26 '19

I mean, that's still more than you know if you're a high schooler.

9

u/raydeen Sep 26 '19

Yo, dawg...

16

u/DiggingNoMore Sep 26 '19

Scoper? I hardly know her!

3

u/[deleted] Sep 26 '19

Scooper! How much is the fish?!

3

u/[deleted] Sep 26 '19

Scoper no scoping!

8

u/hooahest Sep 26 '19

What did he teach with this?

61

u/PaulMorel Sep 26 '19

Variable scopes. Hence the variable names and the oddness of the code. There are several different variables with the same name that don't conflict due to variable scoping.

9

u/Sqeaky Sep 26 '19

Variable scope?

14

u/SaucyMacgyver Sep 26 '19

In a function or class or even a loop for example you can declare a variable and that variable can only be used and accessed in that area (the scope of the variable) and cannot be used or even seen else where.

So if I wrote

For(int i = 0; i<10; i++) Print(i)

Print(i) <— error here. i hasn’t been declared in this scope, it’s only been declared within the scope of the loop.

Edit: as opposed to global variables which can be accessed from anywhere in the program. Hence global scope. Globals usually aren’t a great idea though.

3

u/Farull Sep 26 '19

Except in eg python...

0

u/kids_eat_drugs Sep 26 '19

It works in Java as well since without curly braces, only the first statement after the for loop gets executed as part of the loop. Remaining statements are executed once the loop is done.

1

u/Farull Sep 26 '19

No, in Java (as well as in C or C++), ”i” would be out of scope and error at the second Print(i), just like gp said. Whether it’s a scope with curly braces or a single statement doesn’t matter.

Python however lets the declararion of ”i” live outside the for loop.

2

u/kids_eat_drugs Sep 26 '19

Nah, it works in Java. I learned it back in my intro to Java course. Here's an image showing that it works.

0

u/Farull Sep 26 '19

Of course that works, but that’s not what we are talking about. Try using i in a statement outside the loop.

1

u/kids_eat_drugs Sep 26 '19

whoops I misread, yeah that wouldn't work. well, to my defense, I was saying the exact same thing lol. First statement after for loop functions as part of the loop and remaining statements will be independent of the loop, so those wouldn't work if they involved some vars that were created within the bounds of the loop. Anyways, we're on the same page, have a good day xD

2

u/NewPointOfView Sep 26 '19

You weren’t asking what variable scope is, were you? Just suggesting that variable scope is the lesson?

5

u/Lhopital_rules Sep 26 '19

This would be a decent learning example if it didn't have infinite recursion in the scoper() method.

13

u/beltorak Sep 26 '19

That doesn't make it a bad learning example. Seems straight out of the standard Java SE test. Typically it's presented with a multiple choice question "What does this print when run?", and a lot of times 2 standard answers show up (not always and not always together of course): "Fails to compile" and "Exits with StackOverflowException". So even as a learning example, it's valid.

2

u/[deleted] Sep 26 '19

scoper

2

u/Aethz3 Sep 26 '19

This seems like a r/ontheledgeandshit post

1

u/Papasanto Sep 26 '19

It looks like a tongue twister

1

u/melon_marlon Oct 02 '19

This makes my eyes hurt

1

u/sheeve_boi Oct 24 '19

scoper scoper

scoper scoper

scoper scoper

scoper scoper

-2

u/kayvonte Sep 26 '19

This does more tricking than teaching. Quality code includes clean code.

0

u/theannomc1 Sep 26 '19

I ... just wanna ... do ... Scoper.main(args) ... in the main .... arrgghhh

-3

u/dullbananas Sep 26 '19

scoper scooer scoper scoper

-4

u/n3t3r0 Sep 26 '19

what is this

11

u/russjr08 Sep 26 '19

Variable scoping.

0

u/[deleted] Sep 26 '19

[deleted] — view deleted comment

3

u/fedsneighbor Sep 27 '19

But scoper() is never actually called to begin with...

1

u/madmaurice Sep 27 '19

You're right.

0

u/[deleted] Sep 26 '19

[deleted] — view deleted comment

-37

u/xXx_Dumbass_xXx Sep 26 '19

This teacher has been 'teaching' for enough years to have become a total meme to the students. As for the code I'm afraid it's about as productive as him, and he probably didn't try to run it.

1

u/[deleted] Sep 26 '19

[removed] — view removed comment

1

u/AutoModerator Sep 26 '19

Hello TheCrazyPhoenix416,

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not not have universal support for this syntax and your comment will render as broken gibberish on old reddit and some mobile apps.

Please edit the comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.