r/badcode • • Nov 17 '20

js Imagine passing parameters to functions... (9th grader me thought this was apparently fine). Also JavaScript bad

Post image
1.1k Upvotes

98 comments sorted by

104

u/sanderd17 Nov 17 '20

Is there even some logic in the function naming? What is this supposed to do?

103

u/[deleted] Nov 17 '20

There is literally none. I could honestly probably find some even worse shit from this garbage heap... it was an HTML board game, and I think the functions managed different possible spots the user could land on.

93

u/Alvatrox4 Nov 17 '20

At least you started young, I got into programming at college and wish I had started sooner.

49

u/[deleted] Nov 17 '20

Yea I’m still an HS senior rn. I’m glad I found programming early as well.

34

u/Finally_Adult Nov 17 '20

I’m 37 and about a year into learning. Hoping to move to our IT department in another year or so, it’s never too late (I hope)

13

u/Phoenix-108 Nov 17 '20

Never too late! As long as you have a positive attitude towards learning, with a hunger to match, you'll do great. Best of luck!

7

u/Ais_Fawkes Nov 17 '20

It’s never too late! One of our newest hires has been programming for ~1.5 years and he’s in his early 50s!

63

u/chepas_moi Nov 17 '20

I'm not sure what's worse... the code or taking "screenshots" with a phone camera. OP hasn't evolved much since 9th grade imho.

52

u/[deleted] Nov 17 '20

Lol... I’m definitely still an idiot, but at least I don’t code like that anymore

29

u/fakehalo Nov 17 '20

You'll be saying this to yourself every year for the remainder of your life.

Source: I started at your age, 39 now.

9

u/AzIddIzA Nov 17 '20

I feel this. Every time I look back at something I did a year ago I want to rewrite it better. It's a little frustrating sometimes, but I take it as at least I'm improving since I can recognize a better way now.

5

u/chepas_moi Nov 17 '20

When you stop getting that feeling you're done. Not that you've reached the apex but you'll have reached your apex. Then it's time to move to management.

1

u/AzIddIzA Nov 17 '20

That's a fair point. Hopefully I don't need to move into management anytime soon

1

u/Senial_sage Nov 18 '20

Exactly why I never look at code I previously wrote if I can help it at all

2

u/Rudxain let let.let = 'let' Nov 17 '20

I used 4 eval()s in a nested loop. The poor graphing calculator lasted >20s just to graph a crappy Unicode-ASCII sine wave. Weeks later, I optimized it by using a placeholder script-string that is evaluated 1 time by just 1 eval() and that thing was blazing fast. So I can relate lol

9

u/Terrain2 Nov 17 '20

To be fair, this one doesn’t have any reflections covering the code, pretty much a reflection from the flash (?) in the blank space to the right is the only thing that really gives it away, i honestly didn’t notice until you pointed it out

Although generally F for phonecam screenshots, this one is at least passable as an E and A for effort

4

u/chepas_moi Nov 17 '20

It was that piece of, what I'm guessing is, either tobacco or bread crumbs on the second line that gets me. Here i am, again, trying to wipe my screen clean... the fingerprints second. Agreed that the reflection was well done though.

1

u/[deleted] Nov 17 '20

Well I’m 17... so probably not tobacco (also smoking is bad). Probably bread crumbs... idk how the hell that managed to happen but it’s pretty funny.

3

u/Terrain2 Nov 17 '20

oh that stuff at the top? i absolutely didn’t notice omfg that’s so infuriating, okay maybe like a B- on the grade of a phone screenshot then

2

u/[deleted] Nov 17 '20

B- Grade Testing lol

1

u/[deleted] Nov 17 '20

[removed] — view removed comment

1

u/Terrain2 Nov 17 '20

what is your triggerword

1

u/Terrain2 Nov 17 '20

oh a completely new bot seemingly spamming? cool cool might be annoying, how do i opt out? can i read more about this bot?

1

u/chepas_moi Nov 17 '20

Testing 1, 2, testing "omfg that's so infuriating".

0

u/fromcj Nov 18 '20

Considering they said they’re now a senior in high school I think maybe you’re being a tad harsh.

-2

u/[deleted] Nov 17 '20

It’s much easier. Basically go to reddit, new post, upload directly from camera.

Also I think it looks better. It’s more natural, like a photo of someting is prettier

1

u/GhostSniper018 Nov 17 '20

You can do just the same with a keyboard shortcut and without the need for a second device. Assuming the second statement is trolling.

13

u/dilfmagnet Nov 17 '20

90% of the reason JavaScript is bad is because people do noodle shit like this with it instead of learning the language for like five minutes

7

u/[deleted] Nov 17 '20

I had never coded before when I wrote this, and I didn’t understand why formatting and commenting was important (I barely knew they existed). But I would actually agree that JavaScript isn’t really that a bad language (except for the spaghetti code like mine) - it’s just a meme

3

u/dilfmagnet Nov 17 '20

I know it’s a meme, I just push back when I can because I think it’s actually a fun language to work with. You can definitely do good things with it, but it is a nasty tool

199

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Nov 17 '20 edited Nov 17 '20

Image Transcription: Code


else if (turn == 1 && ppos1 == 17){rfunc17()}
else if (turn == 1 && ppos1 == 18){rfunc18()}
else if (turn == 1 && ppos1 == 19){rfunc5()}
else if (turn == 1 && ppos1 == 20){rfunc20()}
else if (turn == 1 && ppos1 == 21){rfunc21()}
else if (turn == 1 && ppos1 == 22){rfunc8()}
else if (turn == 1 && ppos1 == 23){rfunc23()}
else if (turn == 1 && ppos1 == 24){rfunc10()}
else if (turn == 1 && ppos1 == 25){rfunc25()}
else if (turn == 1 && ppos1 == 26){rfunc14()}
else if (turn == 1 && ppos1 == 27){rfunc27()}
else if (turn == 1 && ppos1 == 28){rfunc28()}
else if (turn == 2 && ppos2 == 1){rfunc01()}
else if (turn == 2 && ppos2 == 2){rfunc02()}
else if (turn == 2 && ppos2 == 3){rfunc03()}
else if (turn == 2 && ppos2 == 4){rfunc04()}
else if (turn == 2 && ppos2 == 5){rfunc05()}
else if (turn == 2 && ppos2 == 6){rfunc06()}
else if (turn == 2 && ppos2 == 7){rfunc07()}
else if (turn == 2 && ppos2 == 8){rfunc08()}
else if (turn == 2 && ppos2 == 9){rfunc09()}
else if (turn == 2 && ppos2 == 10){rfunc010()}
else if (turn == 2 && ppos2 == 11){rfunc011()}
else if (turn == 2 && ppos2 == 12){rfunc012()}
else if (turn == 2 && ppos2 == 13){rfunc05()}
else if (turn == 2 && ppos2 == 14){rfunc014()}
else if (turn == 2 && ppos2 == 15){rfunc015()}
else if (turn == 2 && ppos2 == 16){rfunc010()}
else if (turn == 2 && ppos2 == 17){rfunc017()}
else if (turn == 2 && ppos2 == 18){rfunc018()}
else if (turn == 2 && ppos2 == 19){rfunc05()}
else if (turn == 2 && ppos2 == 20){rfunc020()}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

14

u/[deleted] Nov 17 '20

The work of a god

28

u/[deleted] Nov 17 '20

Poor guy had to write out all my shitty code. Doing the lord’s work.

6

u/Rudxain let let.let = 'let' Nov 17 '20

F in chat

RIP u/MurdoMaclachlan

Will be forever remembered as "The good entity"

4

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Nov 17 '20 edited Nov 18 '20

Aw, thank you! But don't have my funeral just yet! These posts won't kill me, they aren't half as bad as having to do presentations for uni.

2

u/[deleted] Nov 18 '20

do OCR engines work on those ?

1

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Nov 18 '20

Not really, unfortunately. They can sometimes do short things like tweets, but they're not reliable, and especially not with code.

There is actually a bot on our sub that tries to transcribe text posts, but generally it does such a bad job it's not even worth copying and correcting.

114

u/yloose Nov 17 '20

Flair checks out

66

u/chepas_moi Nov 17 '20

Bad human! You completely ignored ppos2 which starts on line 13. How on earth would the visually impaired ever make sense of this now!?!?

70

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Nov 17 '20

Such an assumption to assume I'm human just because I said so. Also, thanks for pointing it out, fixed now.

9

u/--B_L_A_N_K-- if (true == true) {return true;} else {return true;} Nov 17 '20 edited Jul 01 '23

This comment has been removed in protest of Reddit's API changes. You can view a copy of it here.

5

u/veryusedrname Nov 18 '20

Fuck, you just ruined the entire internet for me. :(

4

u/Rudxain let let.let = 'let' Nov 17 '20

Well, at least the entity (human/robot/animal/whatever) tried to do its best

24

u/draftjoker Nov 17 '20

Good human

3

u/--B_L_A_N_K-- if (true == true) {return true;} else {return true;} Nov 17 '20 edited Jul 01 '23

This comment has been removed in protest of Reddit's API changes. You can view a copy of it here.

4

u/FilipForFico Nov 17 '20

At least use switches

6

u/OnlySeesLastSentence Nov 17 '20

No thanks. I tried programming on a switch and it sucked. Phone and PCs only for me. Maybe TI84s

3

u/[deleted] Nov 17 '20

Lol. Better get back to that neural net I was writing on my calculator, though.

2

u/bonedangle Nov 17 '20

I've seen some object factory builder code in an enterprise system that looked very similar

1

u/[deleted] Nov 17 '20

That’s very sad... this was like a week into me learning how to code

16

u/R3D3-1 Nov 17 '20

9th grader me might just barely have had a concept of what "function" means, and I have a PhD in Physics now, so I'd say that's fine :)

7

u/chepas_moi Nov 17 '20

I'm an SE and after many years working with physicists (and bioinformatics, pathologists, oncologists, computational biologists and geneticists...) I can't say that your PhD helps you write clean code in the least! Of course you can get magic done but let's not pretend it's in anyway elegant, scalable, portable, or even readable :D Physicists and computational biologists are for some reason extremely fond of 1 character variable names and I just love when they run out and resort to x1, x2, x3, etc :$

4

u/R3D3-1 Nov 17 '20

Personally, I have done half a bachelor's worth of CS/SE lectures, and practical programming experience across 10+ programming languages, so I don't really fit the stereotype ;) Maintaining a perl visualization script over a few years is great for learning how "clever" solutions become "unreadable" solutions...

So from that perspective: No, short variable names are not the big issue. Software design is.

Short variable names make sense in equations, since they are usually well defined by convention and make the equations genuinely easier to read, never mind write by hand. This depends on domain knowledge -- but so does debugging SQL statements. Of course, short variable names should not be used outside of those areas... It makes perfect sense to define a matrix product as "C(i,j) = sum(A(i,k)*B(k,j), j = 1, A.size(2))", since the abstract matrices don't really have a nameable meaning -- but there's no reason to call a list of input files "ifl".

A much bigger issue are things such as excessive use of global state for passing around data. Or, worse, people realizing globals are an issue but not understanding why, and instead passing around a set of mutable god-objects everywhere -- effectively behaving like globals, only even less clearly. It definitely doesn't solve the "must call routines in exactly the right order" issue... Or heavy over-optimization for performance over readability for code parts, that don't contribute to the computational cost in any significant way. Or a lack of automated testing, that makes refactoring badly written code in any way a "high-risk, will take a few months" operation, and thus effectively impossible. Or data-duplication abounding, contributing to the "call functions in correct order" issue, as the data will become inconsistent easily.

Plus, you can make perfectly unreadable variable names with many characters. "i" is perfectly fine for an index, if the alternative is "iIndex". Definitely better though to use "simulationState(iTimeStep,iEntity)" over "simulationState(i,j)", but personally I'd rather take the latter than "simulationState(iIndex2,iIndex1)".

3

u/chepas_moi Nov 17 '20 edited Nov 17 '20

All valid points!

I'll add to that list for my personal pet peeves that I see time and again:

  • global state indeed, especially seen w/ people who just discovered singletons

  • using functional programming when object oriented programming makes more sense (integrating w/ data sources that will change)

  • using oop when FP makes more sense (data reduction / mapping / filtering of well shaped data)

  • rebuilding a half assed <thing> when there are already so many good open source ones to choose from (event stores, data stores, serialized data formats... DSLs, etc)

  • misuse of basic low level features: threading vs MP. epoll/kqueue vs poll/select. Is a thread even an OS thread or is this some green thread à la python. What a call stack actually means and why you may or may not be allowed to use recursion depending on the implementation of whatever language you're using. A grasp on the performance difference between r/w to cpu cache/ram/hdd/ssd/network... etc.

  • using the wrong data structures or not specializing the the standard libs. ie in python, you have a very versatile dictionary object that can do just about anything. That doesn't mean you should only use dicts for every problem, sometimes you should probably use a set, a simple list, a circular list, a tree, a graph, a counting dict, ... or something exotic like kalman filters or sliding window bloom filters. Knowing what's available and when to use them (a basic understanding of algo complexity) are missed by too many non SE students who then spend years professionally without ever picking them up. That's probably my biggest peeve tbh: know your data structures. If you get those write everything else just tends to fall into place naturally with the added bonus of great performance. I'm glad you're not one of them ;)

As for variable name length, that was a quick example but I still see it all the time. a = array[], ll = LinkedList etc. I prefer the rule that variable name length should be proportional to the size of it's scope. If I see a 3 line for loop impl, it's fine to use i, but if the loop has 50 loc it better be named index, or better index_of_<the_collection_youre_interating>. Nobody wants to have to scroll up to guess what's i and where it came from. I personally prefer the very explicitly named variables with very little doc comments as they become redundant and nobody keeps them up to date. At least when it's in code, you have to keep them up 2 date. One of the few XP principals that I actually appreciate.

Aside that is all the non code SE stuff that, again, scientists, in my experience, never pick up soon enough:

  • using version control (and using it correctly. meaningful commit logs, branching strategies, not bypassing merge/pull requests and reviews whenever you're in a hurry)

  • Basic tcp/ip networking knowledge. At a strict minimum understand your routing tables :)

  • lxc / docker / containerization / bsd jails etc. Nobody wants to install 100 dependencies on their machine to get your pipeline to run. Please give me the docker image instead.

  • unit / integration / e2e testing (as you mentioned). Yes you can run the script and see it works but I'd like to not have to repeat that process after every commit you make in the future.

  • setting up build pipelines. Ok if it's a makefile, bash script, even better if it's a jenkins or gitlab-ci.yml, but have something more than a README.txt to build your stuff.

I've met a few scientists who were actual geeks, too. I just wish there were more of them.

3

u/R3D3-1 Nov 17 '20

TCP/IP: I have a basic understanding of it, but never needed it. I'd say that's domain specific. (Scientists aren't really the ones who should be writing the networking code to begin with...)

Same for docker. Why should a scientist learn it? It is a trend now, but if we are talking about a science related code, those typically evolve on longer time scales due to programming being a side aspect to the theory work that is being implemented in the program. And if the project is large enough for easy deployment to matter, it's probably more effective to hire someone more suitable for that part if the job.

Basic build pipelines should be in place if the project has any scale. But if you're talking about highly specific data crunching code, it might not be the best investment of the scientists time to bother with it.

But for this kind of code it is questionable to begin with, whether it should be used by anyone else... I've dodged a bullet on this, when I rejected a masters thesis, that would have involved inheriting a simulation code from a PhD student. I suspect, even without seeing the code, that starting from scratch would have been faster. Back then, I didn't know to expect this and just happened to find the topic not interesting.

3

u/chepas_moi Nov 17 '20

Docker is à la mode because it solves so many problems so elegantly (the same concept has been possible since well over 20 years already but docker simplified it greatly). To use it best does require basic understanding of networking and routing. That's how containers typically communicate: over networks. This isn't just for deployment. I'd reckon every piece of code you write should be in docker. It notably solves the dirty environment problem (what's in your /usr/lib today? How did it get there?) and the multi versions problem (I need python 2.7 and 3.7 and 3.9 but I don't want conflicts and I don't want to mess with virtualenvs/condas which only solve half the problem).

Next you get a standardized interface to install thousands of "packages" and configure their runtimes. Need a MySQL:5.8, a redis:5 and an openjdk:11? That's ~10 lines of docker-compose config that you can configure once and share with your colleagues to allow them to get identical environments.

Dockerfiles are the go to for published journal work now. They allow easily reproducible pipeline runs where a 3rd party peer can review the code and results without requiring the hassle of understanding every library you installed and how you installed them.

Of course it's not exactly that simple, there's the concept of isolation and single responsibilities that should be respected or you'll end up losing some of the benefits... but it should most certainly be in any scientific toolbox. Our publications, for example, all link to public GitHub repos where dockerfiles allow anybody to rerun the analysis pipelines with a single command. I've forced this onto my colleagues who were resistant at first, which is to be expected, but have all come around. I highly suggest you take a closer look regardless of what you do with code (scientific, artistic, web dev, android app dev, etc) i believe it should be considered a must right next to knowing a VCS. In a large way... it could even reasonably replace VCSes but that's another topic.

2

u/R3D3-1 Nov 17 '20

Given my own work situation having docker images for the build environment would definitely solve a few issues... But given how much we struggle to just get access to internal documentation due to IT policies, I don't get my hopes up :/

2

u/chepas_moi Nov 17 '20

Haha, yeah I had that issue with a previous client in banking/fintech. The irony of "every single lib must first be audited and approved by the (minuscule and snail paced) sec and IT team" meets "we're doing agile development". It's 2015 but we're running production on pre-2000 OSes. Fun times... I lasted 9 months. Best of luck to you ;)

2

u/R3D3-1 Nov 17 '20

I had a job interview once where the told me about how they are doing agile development. But we were in the room with the scrum board and the only note on it said "introduce agile" :)

1

u/OnlySeesLastSentence Nov 17 '20

It's because they pretty much only use mathlab, and it seems everyone trains them to use single letter variables with that language.

1

u/chepas_moi Nov 17 '20

R and Perl from my experience, followed closely by python, matlab, and Fortran for the old timers.

5

u/[deleted] Nov 17 '20

Everyone gets their own function in my utopia.

5

u/DesiresQuiet Nov 17 '20

rfunc10 and rfunc010 oh my god.

1

u/[deleted] Nov 17 '20

Wouldnt a for loop work in this situation.

2

u/[deleted] Nov 17 '20

A loop where I check and pass working conditions as parameters to a single function would certainly have been much better

1

u/[deleted] Nov 18 '20

K that's sort of what i thought i have not coded in a while and am slightly rusty so i was genuinely curious.

1

u/[deleted] Nov 18 '20

It’s still possible that you are right... there’s just no way to know without understanding what the rest of the code does. So in other words... there’s absolutely no way of knowing.

1

u/FuryOfficial Nov 17 '20

you were learning js in ninth grade?

1

u/[deleted] Nov 17 '20

I learned it myself on W3Schools, but I did take an actual class on Java programming in 10th and 11th grade (IB Computer Science)

1

u/ekolis Nov 17 '20

What happened to 19 and 22? You're calling functions 5 and 8 there...

2

u/[deleted] Nov 18 '20

Lol. Who knows... not like anyone can understand it

1

u/SIGSTACKFAULT Ba'al, the soul-eater Nov 18 '20

Love it or hate it, you'd be hard-pressed to find a computer without a working JavaScript interpreter.

1

u/[deleted] Nov 18 '20

To be fair I can also compile code from most languages to EXE files as well, but I guess it’s still a lot easier to use JS

1

u/jbradford77 Nov 18 '20

If this is the worst thing you did in 9th grade you're probably doing at least a few things right in life

2

u/[deleted] Nov 18 '20

Well I left-aligned all of my code, didn’t comment anything, and never used whitespace... so I probably would’ve been better off smoking crack or something...

1

u/jbradford77 Nov 18 '20

As someone with 20+ years of hindsight from high school years I can assure you the kids who did hard drugs in school are not better off than the kids who made beginner coding mistakes then kept learning at least long enough to look back and recognize their mistakes.

Even after years of coding though, when I look back at something from a year ago I just wonder wtf I was thinking when I did that

1

u/zeGermanGuy1 Nov 18 '20

Would have been so happy if I could have learned coding in year 9.

2

u/[deleted] Nov 18 '20

When did you start?

1

u/zeGermanGuy1 Nov 18 '20

In uni. Informatics course in school was full and I didn’t realise self-teaching was a thing.

1

u/[deleted] Nov 18 '20

Cool. Did you major is CS, cause that’s probably what I’m gonna do too?

1

u/zeGermanGuy1 Nov 18 '20

Didn’t really have the concept of a major, it’s not really common in Germany. I got a bachelor of Science in Business Informatics. Programming sadly was only a relatively small part of it

2

u/[deleted] Nov 18 '20

Wirtschaftsinformatiker sind eh die Besten. 😎

3

u/Blue_Link_34 Nov 17 '20

I agree that you should be using arguments, plus a if for turn then a switch for ppos2 inside would help if not using parameters. And since it's JS, I have a wonderful terrible proposition :

let context; // 'this' object, or 'window' object for example.

if(turn == 1) {
  context["rfunc"+ppos2]();
} else if(turn == 2) {
  context["rfunc"+'0'+ppos2]();
}

//or even better/worse
context["rfunc"+(turn==2?'0':'')+ppos2]();

That's still terrible code, but slightly """better""" since shorter (though an absolute pain to read and maintain still, so never in production).

1

u/[deleted] Nov 17 '20

If it’s stupid and it works, it’s not stupid.

1

u/[deleted] Nov 17 '20

Idk... not if I ever want anyone to see it ever. The game is actually kind of good, but I’ll never put it on GitHub or anything (obviously) because the code is so horrendous

1

u/Farpafraf Nov 17 '20

If it’s stupid and it works then it won't work

1

u/TerribleCoder61 Nov 20 '20

This is like saying that 'my clunker car works today therefore it is not a clunker'. Of course, tomorrow may be another day. :( I have graded college students CS work back in my college days and the grading standard was to give 30% for code that worked. Remaining 70% percent went towards efficiency, style, commenting, readability, maintainability.

4

u/[deleted] Nov 17 '20

Couldn't even do a LOOP?!? Oh lordy. Glad you've gotten better though! :-)

2

u/eaglebirdman Nov 18 '20

YandereDev? Is that you?

1

u/eshad89 Nov 17 '20

Most likely you could solve the problem behind in one line.

1

u/[deleted] Nov 18 '20

Programming style: Indian rural

1

u/TerribleCoder61 Nov 19 '20

Ok for a 9th grader, but I get code like that submitted for code review by professionals.

1

u/[deleted] Nov 22 '20

This is some YanDev type shit but i’m glad you started learning “young” atleast