I wonder if this teacher got tricked into believing and regurgitating that early returns are bad... it would cut this code, nesting and general confusion in half.
Don't listen to em kids, early returns are the right choice a lot of the time.
I understand one reason behind it; it can create a grey area for what resources remain allocated when you return in the middle of your function.
In the real world it's a pretty simple guideline to follow, that you ONLY do this at the top of the function when it's clear no additional resources have been allocated. If a team can't pull this off they never had a chance anyways.
Worth noting that in modern C++, RAII makes even that concern mostly obsolete.
What I had heard from my professor who taught a C/C++ course and also had industry experience and also thought early returns/break/etc. were bad was that because they're a form of goto (basically true even if not technically true for early returns) and goto is bad, those statements are also bad. They even cited Goto Considered Harmful to justify it. Misunderstanding Dijkstra's argument seems to be a pervasive theme in the industry.
Alternatively, simply release resources on an early return. A lot of modern languages have tools for this and even if you’re stuck with C this is a valid case for goto (put all the cleanup after the nominal return and jump into the appropriate point).
Then i think anything else he says is probably not worth listening to. Early returns are the only way to write code, unless you want to end up the subject of a post on r/badcode
So this returns false if the file is already open, true if it exists and had to open it, true if it had to create it (and try to open it without checks) , and false if it didn't exist and couldn't create it?
First you’d have to get the guy to explain wtf is this supposed to do in the first place. Open list? Looks for a file in what looks like apple’s objective c?
Probably right I was letting ios::in fool me. I just looked it up. Apparently there’s no namespaces in objective c. So if anything it’s a clue it’s not iOS stuff. Haven’t touched that in a while, getting rusty.
Seriously I’m by no means an expert coder but I would roast a teacher for this lol like no comments or documentation at all this looks like one of those fucked up pseudo code exercises you have to evaluate in your head.
I'd bring it up with the prof, it he wasn't cool about it then go to the department. I'd recommend firing someone at my job if they were a senior and put this in a PR, and didn't listen to a code review and went ahead and merged.
I recently started to go to university but I’ve been working as a software developer for around 7 years. It’s a joke what some professors produce and how locked in they are in „there way“. They’re telling you about readable code and want you to use function names like krt(double n, double k) and krth(double n, double k). What the krth?
You just need to understand a few basic thing with code in academic areas, it must compile and do what the question asked but must never be perfect, as if some code can get 100/100 on it then you know that you and your classes code will get a very serious inspection and neither you nor your instructor want that sort of attention.
Did some code in cobol in the late 90s at uni and being someone who came in as a mature student with cobol experience I aced everything but the lecturer apologized for not giving full marks as it would of made life harder for the other students so I just nodded and spent the rest of my time helping my class mates get better .
I’ve known professors to find this to be “unrepresentative” of a students true ability. That’s why they allow for 30% of the class to fail and another 20% to not get the prerequisite pass
From memory if I'd of been given 100% as I'd deserved it would of meant that not just my code but everyone in my classes code would of been suddenly under the microscope and given they had helped me through some modules I could never drop them under the bus shall we say, as without help I'd of flunked a few modules especially the bean counting ones and the more formal maths...
Doesn't line up with my uni experience in the 2010s - I (and presumably many of the other good students) routinely got full marks for coding assignments.
I mean, looking at the salary of a university professor versus pretty much anywhere else in the field (unless maybe you’re at a top university), it’s pretty obvious that professors aren’t going to be nearly the best in the field
C’è poco da downvotare, in un sacco di gruppi italiani di programmazione vedo sempre studenti che pubblicano cose oscene fatte dai prof, ci sono un sacco di ragazzi che si lamentano del fatto che non si impari quasi nulla di programmazione in particolare alle superiori, roba che con risorse gratuite online impari meglio.. questo non esclude che ci siano anche istituiti dove ci sono professori con le palle enormi, ma sono una percentuale minore. Ho visto corsi di istituti tecnici dove i prof scrivevano Giava invece di Java, confondevano concetti di altri linguaggi e sembrava che lèggessero un copione senza conoscere realmente gli argomenti di cui parlavano. Per fortuna non è sempre così ma anche questa è una realtà è non è da ignorare.
Se stai prendendo per il culo, sorry ma non hai capito, scriveva Giava, su tutte le slide e presentazioni. La tua battuta non c’entra niente nel contesto.
take that person, and tell him he has to use classes because it has to show the students how they work. Notice how the file handling is using a very C interface, while clearly using classes, and is just a bad re-implementation of what STL already have
Like with "if"s, you can single line "else"s without brackets. I've occasionally used "if (true) foo++; else bar--;" (separate lines) but I've never thought of using brackets for one and no brackets for the other. Fiendish code here.
I don't have proof right now because it's early here but my instinct is that all this code could be collapsed into a single line that's less than 20 characters long
I am stunned. First it's unreadable even though I caught that it seem to be part some latin language like the "nome" variable. It's seemingly an object eleco that holds an open file handle, and tries to create and reopen it if it fails and only then sets the nomeFile field? I am sure whatever program this has a large amount of edgecases stemming from this function alone.
I recently learned that since I went to a public uni that my professors' salaries are publicly disclosed. Sheesh ten years in the industry and I'm already making more than the department chair. Professors probably aren't the best coders in town or they'd be coding.
It's almost as bad when people from academia want to come to a commercial company.
First you must deal with their code that is designed to work only once, then you must squash their egos during code reviews, so they don't defend their code like a fraking Alamo.
161
u/The_Northern_Light May 11 '23
I wish academia required at least some private sector experience before you could become a teaching professor.
I would be forcibly ejected from the building if I submitted this for code review.
164
u/[deleted] May 11 '23
So this returns false if the file is already open, true if it exists and had to open it, true if it had to create it (and try to open it without checks) , and false if it didn't exist and couldn't create it?
Not to mention all the unmentionables.
This isn't just bad code, it's hell code.
66
u/wiesemensch May 11 '23
I recently started to go to university but I’ve been working as a software developer for around 7 years. It’s a joke what some professors produce and how locked in they are in „there way“. They’re telling you about readable code and want you to use function names like
krt(double n, double k)andkrth(double n, double k). What the krth?43
u/thekwoka May 11 '23
The worst part is not just using english for variables.
17
u/Zealousideal_Show45 May 11 '23
I'm the worst software developer I know and even I won't produce such shit code lol
18
u/wwxxcc May 11 '23
Good thing he is a professor and not a software developer i guess.
3
u/HighMarck May 11 '23
Sembra un prof italiano, sono pochi qua quelli che veramente sono bravi ed in grado di insegnare..!
8
u/Chance-Ad4773 May 11 '23
College professors write bad code. If they wrote good code, they wouldn't be teachers
4
u/This_Growth2898 May 11 '23
If he could do better, he wouldn't be your professor.
2
u/stormAster720 May 11 '23
This has to be marked as a war crime
3
u/_Vicix May 11 '23
Oh mio dio
3
u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 11 '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!
2
u/lestofante May 11 '23
Classic C programmer trying to write C++
1
u/mMykros May 11 '23
Why is there no { after that else
1
u/Alby7503 May 11 '23
itis momento
1
u/Pangocciolo May 11 '23
Mannaggialaputtana, tell him to teach to name things in English!
1
u/zombarista May 11 '23
If you can’t, teach.
1
u/reverendsteveii May 11 '23
I don't have proof right now because it's early here but my instinct is that all this code could be collapsed into a single line that's less than 20 characters long
1
u/fts_now May 11 '23
That's why Italian SE get paid shit, the good ones are all abroad
1
u/youngbull May 11 '23
I am stunned. First it's unreadable even though I caught that it seem to be part some latin language like the "nome" variable. It's seemingly an object eleco that holds an open file handle, and tries to create and reopen it if it fails and only then sets the nomeFile field? I am sure whatever program this has a large amount of edgecases stemming from this function alone.
1
u/ErikBonde5413 May 11 '23
It returns true if the file was closed and it managed to open it, false if it either was open or it was closed and didn't manage to open it.
Interesting. I wonder what the use case is.
1
u/Pepineros May 11 '23
This is atrocious, unless it’s intentionally bad as a refactoring exercise.
1
u/thanatica May 11 '23
Starting with a perfect 10, I'll have to deduct 6 points, ending you up with a 4, mister professor.
I expect you to correct these mistakes by tomorrow, and add comments and unittests, young man!
1
u/RlyRlyBigMan May 11 '23
I recently learned that since I went to a public uni that my professors' salaries are publicly disclosed. Sheesh ten years in the industry and I'm already making more than the department chair. Professors probably aren't the best coders in town or they'd be coding.
1
u/whyamihere2333 May 12 '23
What in the seven blue fucks is wrong with the conditions? Why is he mixing styles?
1
u/lawappsoftware May 12 '23
Perhaps you're meant to fix it. I am sure there is context.
1
u/zickige_zicke May 12 '23
What if the third open fails?
1
u/brzeczyszczewski79 May 11 '23
It's almost as bad when people from academia want to come to a commercial company.
First you must deal with their code that is designed to work only once, then you must squash their egos during code reviews, so they don't defend their code like a fraking Alamo.
-1
u/nan-000 May 11 '23
Is this a professor of liberal arts?