r/badcode • • Mar 31 '21

python It should have been a code, not an article

Post image
726 Upvotes

71 comments sorted by

177

u/Pcb95 Mar 31 '21

Seems like a few extra variables could go a long way

63

u/nurley Mar 31 '21

I don’t think this person knows what a reference is.

0

u/[deleted] Mar 31 '21

[deleted]

30

u/pytness Mar 31 '21

Yep, in python, variables are literally references

-21

u/nurley Mar 31 '21 edited Mar 31 '21

Lol. I hope this is /s. I think it is. Either way, you made me laugh.

Edit: Not trying to discourage this person from asking a question. I thought they were giving a genuine witty response and in either case (whether it was intentional or not) their comment made me laugh because I viewed it as a witty, funny response.

0

u/[deleted] Mar 31 '21

[deleted]

9

u/nurley Mar 31 '21

Sorry, I wasn't trying to make fun of you. However, I think it's fair to assume that if you're in this subreddit you're probably a somewhat experienced programmer. References / pointers to memory are pretty basic / standard stuff that get taught in any intro to programming course, or any course where you're going in-depth into any language.

To answer your question, yes it would be something like that. Certain languages do references by default when you write what you've written there (I'm pretty sure Python is one of them)

spriterc = self.entity.components\["SPRITE_RENDERER_COMPONENT"\]

And some languages require you to add a type specifier (e.g. C++, in this case it's the & symbol)

auto& spriterc = self.entity.components\["SPRITE_RENDERER_COMPONENT"\]

In either case a reference just assigns another variable to refer to another variable living in memory. When you modify the reference, you modify the original memory / variable. If you make a copy instead of a reference you can modify the new copied variable and the original memory block will remain unchanged.

https://en.wikipedia.org/wiki/Reference_(computer_science)

1

u/DavidRoyman Apr 01 '21

> the original memory block will remain unchanged.

I'm laughing in Python.

0

u/[deleted] Mar 31 '21

[deleted]

-4

u/nurley Mar 31 '21

Thanks for downvoting me when I give you a genuine response.

Knowing what a reference/pointer is, and those specific words, is something you'd learn in an intro to programming course. It dumbfounds me why someone would be following a subreddit dedicated to getting a kick out of bad code without having any programming experience. You do you kid.

7

u/[deleted] Mar 31 '21

Some people are here as beginners to see examples of bad code and how to avoid replicating those mistakes.

-4

u/nurley Mar 31 '21

TIL

I hope everyone wants to learn how to code, that's great. I think there are better resources and ways to learn for beginners though. Once you have a handle on the basics learning from others mistakes is definitely a good thing to do.

→ More replies (0)

-1

u/cabbagebot Apr 01 '21

I've been seeing examples lately of people posting code that isn't even that bad, presumably beginners that can't tell.

It's a strange dynamic due to the mix here for sure. Gate's wide open though -- no need to gatekeep the sub.

→ More replies (0)

0

u/[deleted] Mar 31 '21

[deleted]

3

u/nurley Mar 31 '21

I'm happy you're on a subreddit trying to learn how to code. I encourage you to look at / join r/AskProgramming or r/learnprogramming if that's something you're really interested in. There are other more language/use-case specific subreddits as well to look at.

I wasn't intentionally a dick nor do I think I was being a dick. I thought you had a genuine witty response saying "oh so you're saying I could just do this! wow omg!" and from that perspective it was a really funny comment.

I assumed the minimum bar-of-entry to this subreddit would be people who are a little experienced at programming, but apparently there are beginners like you on here. That's fine & well, I've learned my lesson. Don't be so sensitive on the internet when there is a mismatch in expectation.

→ More replies (0)

102

u/knightttime Mar 31 '21

Image Transcription: Code


if entity.components.get("SPRITE_RENDERER_COMPONENT") and entity.components.get("TITLE_COMPONENT") is None:
    self.entity.components["SPRITE_RENDERER_COMPONENT"].rect.topleft - entity.components["SPRITE_RENDERER_COMPONENT"].rect.topleft
    self.entity.components["SPRITE_RENDERER_COMPONENT"].image - pygame.transform.scale(entity.components["SPRITE_RENDERER_COMPONENT"].image, (entity
    if self.entity.components.get("SPRITE_RENDERER_COMPONENT").color:
        self.entity.components["SPRITE_RENDERER_COMPONENT"].image.fill(self.entity.components["SPRITE_RENDERER_COMPONENT"].color)
        if self.entity.components.get("SPRITE_RENDERER_COMPONENT").opacity :
            self.entity.components["SPRITE_RENDERER_COMPONENT"].image.set_alpha(self.entity.components["SPRITE_RENDERER_COMPONENT"].opacity)
    if self.entity.components["SPRITE_RENDERER_COMPONENT"].renderable:
        self.app.screen.blit(self.entity.components["SPRITE_RENDERER_COMPONENT"].image, self.entity.components["SPRITE_RENDERER_COMPONENT"].rect)
        # if entity.components.get("LIGHTING_COMPONENT"):
        #   self.app.filter.components["CANVAS_COMPONENT"].image.blit(self.entity.components["LIGHTING_COMPONENT"].light_bg_1, self.entity.compone
        if self.entity.components.get("SPRITE_RENDERER_COMPONENT").opacity:
            self.entity.components["SPRITE_RENDERER_COMPONENT"].image.set_alpha(self.entity.components["SPRITE_RENDERER_COMPONENT"].opacity)
            
if entity.components.get("TILE_COMPONENT"):
    self.app.screen.blit(
        self.entity.components["SPRITE_RENDERER_COMPONENT"].image,
        (
            self.entity.components["SPRITE_RENDERER_COMPONENT"].rect.x,
            self.entity.components["SPRITE_RENDERER_COMPONENT"].rect.y
        ),
        (
            -self.entity.components["SPRITE_RENDERER_COMPONENT"].rect.x + self.camera_entity.components["CAMERA_COMPONENT"].camera_position[0],
            -self.entity.components["SPRITE_RENDERER_COMPONENT"].rect.y + self.camera_entity.components["CAMERA_COMPONENT"].camera_position[1],
            -self.camera_entity.components["CAMERA_COMPONENT"].camera_position[0] + self.app.window_w * 2,
            -self.camera_entity.components["CAMERA_COMPONENT"].camera_position[1] + self.app.window_h * 2,
        )
    )

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!

29

u/Dehast Mar 31 '21

Already looks so much better

37

u/MatthewRPG576 Mar 31 '21

Good human

18

u/mrheosuper Mar 31 '21

Damn i love you.

10

u/knightttime Mar 31 '21

Lol, you're welcome!

23

u/hoyohoyo9 Mar 31 '21

ctrl+c "self.entity.components["SPRITE_RENDERER_COMPONENT"]"

ctrl+v ctrl+v ctrl+v ctrl+v ctrl+v ctrl+v ctrl+v ctrl+v ctrl+v ctrl+v ctrl+v

19

u/knightttime Mar 31 '21

how did you know? XD

5

u/edoCgiB Apr 01 '21

That's how the person who wrote the code did it. We've all been there :)))

131

u/[deleted] Mar 31 '21

I can’t get past that font tho

39

u/djpor2000 Mar 31 '21

Now imagine coding in Comic Sans...

8

u/SphericalMicrowave Mar 31 '21

3

u/[deleted] Apr 01 '21

This is a million times worse than Comic Sans. Only the 'C' is serif! I am going to jump off a mountain.

0

u/[deleted] Mar 31 '21

[deleted]

4

u/LegoClaes Mar 31 '21

That’s there to make you question your life choices.

2

u/TheUnlocked Apr 01 '21

The demo only includes alphanumeric + period + comma. But the full paid version even includes ligatures.

19

u/[deleted] Mar 31 '21

[removed] — view removed comment

14

u/konaya Mar 31 '21

It's more accurate to say that in the context of Windows default font selections, Comic Sans MS is marginally less crappy for certain kinds of dyslexia.

7

u/djpor2000 Apr 01 '21

Yes, one of the benefits of Comic Sans is that the letters "p", "q", "d" and "b" are not flipped versions of each other. The British Dyslexia Association also recommends "sans serif fonts, such as Arial and Comic Sans, as letters can appear less crowded."

But I cannot ever imagine coding in ANY font that isn't monospaced. 😅

5

u/[deleted] Apr 01 '21

[removed] — view removed comment

3

u/djpor2000 Apr 01 '21

Holy shit, some people have got wayy too much time on their hands XD

36

u/Alberiman Mar 31 '21

For those that aren't sure -If you're looking up the same component that much just fricken import the component reference into a variable and be done with it . Looking up things using strings is already super inefficient but then take into account that you're repeatedly looking up the same variable and jesus.

To make it waaay faster and cheaper to repeatedly look up a string my advice is to have it pre-converted into the hash/integer(depending on how your code deals with it) and name it the same as the string you were reusing.

in this case just convert set
var = self.entity.components["SPRITE_RENDERER_COMPONENT"]

20

u/IrritableGourmet Mar 31 '21

OP walks into a library

OP: "Hello! I'm looking for the third word on the 4th page of the book with this name!"

Librarian: "Sure!" goes to card catalog, searches for book title, copies information to a post-it, walks to the shelf, climbs a ladder, grabs the book, walks back to the desk, and opens the book to the correct page

OP: "Thanks!" hands book back

Librarian: walks back to the shelf, climbs the ladder, puts the book back, throws away the note, and returns to the desk

OP: "Now, can you tell me the second word on the page?"

Librarian: "Sure!" walks to card catalog...

14

u/tangerinelion Mar 31 '21

There's a certain thrill to using bare strings though. You force people to be sharp and pay attention since a single typo anywhere will cause a hard to find bug. You also feel really smart because you can lookup data in these undocumented places and be a super elite hacker.

8

u/[deleted] Apr 01 '21

I used to be the super elite hacker of my former workplace until they figured out my trick of searching the project for "Helen, insert bug here for job security."

22

u/Mr2-1782Man Mar 31 '21

If you hit Ctrl-V more than once you should think about what you're doing. If you're hitting it that many times just give up.

7

u/khalidpro2 Mar 31 '21

I heard that in CS50

3

u/[deleted] Apr 01 '21

It really helps the second time around, though, when you're pasting resumes.

18

u/Cmgeodude Mar 31 '21 edited Mar 31 '21

It's the "I have to do everything in as few lines as possible!" syndrome. It's endemic in leetcode/codewars.

Run-on sentences work and can even be fun and/or impressive to either the listener/reader or the speaker, or maybe even both, but once you get going and have full repeated subordinate clauses that tie in tangential references that enrich but are not essential to your utterance, which I, a former foreign language teacher, define as any single speech act (or written, semantically coherent expression, question, exclamation, statement, imperative, or proposition), it may be time to pat yourself on the back for your amusing linguistic prowess - who doesn't love a language game? - and consider revising and separating your clauses for your audience, including readership, who risk getting lost in your conscious or unconscious, intellectually- stimulating verbosity.

Isn't it nicer to read statements when they're succinct and to the point?

Same thing with code. Each line should mean and do one and only one thing, please.

3

u/IrritableGourmet Mar 31 '21

I was gradually coming to have a mysterious and shuddery reverence for this girl; nowadays whenever she pulled out from the station and got her train fairly started on one of those horizonless transcontinental sentences of hers, it was borne in upon me that I was standing in the awful presence of the Mother of the German Language. I was so impressed with this, that sometimes when she began to empty one of these sentences on me I unconsciously took the very attitude of reverence, and stood uncovered; and if words had been water, I had been drowned, sure. She had exactly the German way; whatever was in her mind to be delivered, whether a mere remark, or a sermon, or a cyclopedia, or the history of a war, she would get it into a single sentence or die. Whenever the literary German dives into a sentence, that is the last you are going to see of him till he emerges on the other side of his Atlantic with his verb in his mouth.

28

u/CSnoobActual Mar 31 '21

Why does that eyes hate my font. 😵😵😵 I can barely read it.

7

u/Sauerbier_ Mar 31 '21

This code is so dense, it would fall out of the cloud

3

u/khalidpro2 Mar 31 '21

which language is this?

5

u/[deleted] Mar 31 '21

python

9

u/khalidpro2 Mar 31 '21

I know python, and the code was so unreadable that I didn't recognize it

6

u/[deleted] Mar 31 '21

Most identifiable by the use of self and lack of curly braces.

2

u/khalidpro2 Mar 31 '21

there are other languages that has that

4

u/[deleted] Mar 31 '21

Well if you want to go further there is the use of "None" and "and". Also the ":" after the if statements.

2

u/khalidpro2 Mar 31 '21

I agree with you

2

u/Nall-ohki Mar 31 '21

Code isn't countable

1

u/Elusivehawk Apr 01 '21

Ah yes, "a code". One arbitrary unit of code...

1

u/Architector4 Apr 01 '21

Code can be counted using measures such as the amount of characters, lines, functional lines (excluding empty spacer lines and comments), files or individual projects. Sure, not all of these ways to measure code count are effective or meaningful, but they nonetheless allow one to count code.

1

u/Nall-ohki Apr 01 '21

You don't understand the meaning of "countability" in English.

Likewise, "amount" is not used for countables like "characters". Use "number of" instead.

1

u/Architector4 Apr 01 '21

Looking at it again, I guess the reason you've stated that is to provide an underlying fact that should affect one's decision to put "a" before a word or not, i.e. trying to provide a hint to a grammar mistake, but without pointing out the grammar mistake itself. That wasn't obvious, and I thought you were trying to state that one can't measure "more code" or "less code" at all.

1

u/Nall-ohki Apr 01 '21

That's fair! I was specifically referring to the countability of the word rather than the measurability of the actual subject.

2

u/miladiashe Apr 01 '21

If that's article, that's bad article too

1

u/scunliffe Apr 01 '21

Needs more XML.

1

u/PyBind Apr 01 '21

is this tryna be pygame?

1

u/[deleted] Apr 01 '21 edited Apr 01 '21

that's some inconsistencies in getting things, using both ["this"] and .get("this, the get function"). also forgot DRY (repeatedly getting stuff, that's how)

i'll be back later here when I finish copying the code and extract the functions to a variable to showcase variables with the thing

EDIT: I just did it. I did use some other software to extract the code, and isn't perfect. Thanks u/knightttime for the transcription

sprite_renderer = entity.components["SPRITE_RENDERER_COMPONENT"]
camera = self.camera_entity.components["CAMERA_COMPONENT"]

if sprite_renderer and entity.components["TITLE_COMPONENT"] is None:
    self.sprite_renderer.rect.topleft - sprite_renderer.rect.topleft
    self.sprite_renderer.image - pygame.transform.scale(sprite_renderer.image, (entity
    if self.entity.components.get("SPRITE_RENDERER_COMPONENT").color:
        self.sprite_renderer.image.fill(self.sprite_renderer.color)
        if self.sprite_renderer.opacity :
            self.sprite_renderer.image.set_alpha(self.sprite_renderer.opacity)
    if self.sprite_renderer.renderable:
        self.app.screen.blit(self.sprite_renderer.image, self.sprite_renderer.rect)
        # if entity.components.get("LIGHTING_COMPONENT"):
        #   self.app.filter.components["CANVAS_COMPONENT"].image.blit(self.entity.components["LIGHTING_COMPONENT"].light_bg_1, self.entity.compone
        if self.sprite_renderer.opacity:
            self.sprite_renderer.image.set_alpha(self.sprite_renderer.opacity)
            
if entity.components.get("TILE_COMPONENT"):
    self.app.screen.blit(
        self.sprite_renderer.image,
        (
            self.sprite_renderer.rect.x,
            self.sprite_renderer.rect.y
        ),
        (
            -self.sprite_renderer.rect.x + camera.camera_position[0],
            -self.sprite_renderer.rect.y + camera.camera_position[1],
            -camera.camera_position[0] + self.app.window_w * 2,
            -camera.camera_position[1] + self.app.window_h * 2,
        )
    )

1

u/AutoModerator Apr 01 '21

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

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/JacksonChen666, it would be appreciated, but not required, if you could edit your 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.

1

u/[deleted] Apr 01 '21

buzz lightyear magic strings.. everywhere

1

u/Oxey405 Apr 04 '21

Right click ; format ; wait for the microwave bell sound and then you have a beautiful code !

1

u/ReignX2_Tenshi Apr 06 '21

That font makes it 5x harder to read