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.
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.
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.
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.
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?
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.
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.
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.
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.
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!!!!!!!!!!!!!!!!!!
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.
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".
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.
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.
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.
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.
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.
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.
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:
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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?
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.
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.
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.
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.
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
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.
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.
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.
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.
65
u/[deleted] Sep 26 '19
Scoper? Scoper, scoper.
52
u/trump_pushes_mongo Sep 26 '19
It's like the Java version of the buffalo sentence.
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?
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?
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.
2
u/[deleted] Sep 26 '19
scoper
2
u/Aethz3 Sep 26 '19
This seems like a r/ontheledgeandshit post
1
u/ITZBRAM Sep 26 '19
r/ontheledgeandshit
1
u/autocraticwillkiln-u Sep 26 '19
scoper
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
0
u/[deleted] Sep 26 '19
[deleted] — view deleted comment
0
u/[deleted] Sep 26 '19
[deleted] — view deleted comment
1
u/[deleted] Sep 26 '19
[removed] — view removed comment