r/badcode • • Jun 29 '20

python The universal getter.

Post image
1.8k Upvotes

65 comments sorted by

222

u/MasterThief34 Jun 29 '20

Maybe they were paid per LOC

45

u/Kasper4131 Jun 29 '20

Nah this is some project I made for fun four years ago. I wasn’t getting paid. This was actually me just not knowing why OOP languages had getters

0

u/[deleted] Jun 29 '20

[deleted]

2

u/ray4585 Jun 30 '20

Don’t @ me brooooo

74

u/inxaneninja Jun 29 '20

That would only add two lines of code though (i guess anything matters)

94

u/MasterThief34 Jun 29 '20

Little by little, you fill a pouch.

49

u/brylee123 Jun 29 '20

It'll easily double your code if you call it everyline

20

u/inxaneninja Jun 29 '20

and discard the result? oh god

34

u/greenpepperpasta Jun 29 '20

but then they can do

def returnvar(var):
    return get(var)

def getvar(var):
    return returnvar(var)

25

u/green_codes Jun 29 '20

Yer stack is burnin’

10

u/EkskiuTwentyTwo i -= (i - (-1)) - i Jun 29 '20

Perhaps paid by character or filesize?

11

u/inxaneninja Jun 29 '20

paid by the average length of a line

1

u/Terrain2 Jun 30 '20

actually 4 if they follow PEP-8, which hyperskill forces me to otherwise i get publicly humiliated

1

u/[deleted] Jun 29 '20

[removed] — view removed comment

1

u/MasterThief34 Jun 29 '20

Yes we were taught at school that it happens. One positive aspect is that it is quite quick to measure I guess.

1

u/MassiveFajiit Jun 30 '20

Step one: don't use python if you're paid by the line lol

3

u/kimjeongpwn Jun 29 '20

Pardon me as it might be a dumb question. Still newbie in Python. Is this written in Python, and if yes, what makes it bad code? Thank you.

6

u/Kasper4131 Jun 29 '20

So basically it’s bad because it doesn’t do anything. It takes the value of var and returns it meaning that if you wrote something like print(get(“example”)), it would do the same exact thing as print(“example”) since get will just return what you gave it. Therefore it’s completely useless

4

u/[deleted] Jun 29 '20

Is this written in Python

Yes

if yes, what makes it bad code?

the fact that there is a function called get that returns the value it's given

152

u/DariusDerStar Jun 29 '20

Thats me in college when I didn't understand why I need getters and setters but my prof tells me they are mandatory

51

u/ndgnuh Jun 29 '20

Just make stuffs public and directly access them instead of writing those trivial getter, setter.

62

u/nagesagi Jun 29 '20

One thing I like about C# is that I can make things properties and then have a public getter and private setter and have it syntactically work as a variable

9

u/[deleted] Jun 29 '20

Yeah that’s kinda cool, nice for limiting access with keywords too.

-10

u/yaxamie Jun 29 '20

Python uses “slots” in the same way. No need for all that.

12

u/nagesagi Jun 29 '20

From what I can read, Python slots are completely different in that they are used to specify that a class will only use specific variables instead of using a dictionary as opposed to limiting the tread and write access to an object's variables.

Properties in C# are syntactic sugar for getter/setter methods. They are used like variables, but are methods that can be changed later.

11

u/RushilU Jun 29 '20

Python has something similar to what you describe, conveniently called properties, although you can’t easily make “private” ones.

4

u/nickcash Jun 29 '20

Python does have that feature. It also has slots. But they're not the same thing.

1

u/Pythagorean_1 Jun 29 '20

No, it doesn't.

9

u/woah_m8 Jun 29 '20

Just make all your variables static (don't forget cryptic names so no one can ever confuse them). Also include a txt file where you explain what every variable means.

\s

21

u/carbaretta Jun 29 '20

Who needs encapsulation, right? 😎

0

u/[deleted] Jun 29 '20

[deleted]

-3

u/carbaretta Jun 29 '20

Brøthêr

5

u/wischichr Jun 29 '20

You shouldn't do that in most compiled languages. If your language doesn't support 100% compatibility when switching from public field to getter/setter you should always use a getter/setter right from the start or you might break dependencies if you later have to change it.

2

u/DariusDerStar Jun 29 '20

No, you failed the course when you did that :D

1

u/Def_Your_Duck Jun 29 '20

College you should learn about macros. I haven't written a non-complex getter/setter in my life.

1

u/Rainfly_X Jun 30 '20

This fool stole your code! Identify theft is not a joke, Jim.

29

u/redfournine Jun 29 '20

Shit, I never would have thought of this

17

u/The_Northern_Light Jun 29 '20

The optimist in me thinks this was introduced to play nice with some generic code.

But I know better.

12

u/accuracy_frosty Jun 29 '20

template <TypeName T>

T get(T Var) {

return Var;

}

4

u/Friz64 Jun 29 '20

fn get<T>(var: T) -> T { var }

3

u/CodenameLambda shameless Jun 30 '20
get :: a -> a
get = id

12

u/causticacrostic Jun 29 '20
def really_get(var):
        return get(var)

11

u/TastyDumplingSoup Jun 29 '20

Genius. I’ll never have to write another getter in my life.

3

u/yosrational Jun 29 '20

equal trade

3

u/[deleted] Jun 29 '20
var = 3
def get(x):
    return x
var = get(var)

Edited.

4

u/AutoModerator Jun 29 '20

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.

valtzz00, 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.

2

u/WhatnotSoforth Jun 29 '20

Not gonna lie, OP, I threw up I my mouth a little.

2

u/LeJoker Jun 29 '20

The first language I learned was Python. I enjoyed it, it made some sense to me.

Then years later I learned C#, and I love it, it's very intuitive to me.

Now this code just looks... wrong. It makes me uncomfortable.

2

u/Isvara Jun 29 '20

You said you wrote this code, so I'll accept your word that it's useless, but I also want to note that it's not obvious just from the snippet that it's useless. It could be part of a protocol that says "a module implementing this protocol shall have a get function that...", where the actual implementation of said function would be different in each module. (In such a case, the thing wrong would probably be that it should be a class, but who knows.)

2

u/ray4585 Jun 30 '20

Don’t @ me brooooo

2

u/Attack_Bovines Jun 29 '20

I’m not sure of the intent because of the way it’s named, but this happens to be the identity function, which is very useful in functional programming.

0

u/[deleted] Jun 30 '20

[deleted]

1

u/Attack_Bovines Jun 30 '20

Relax. You can write functionally in any lang. I clearly say that I don’t know the intent because of the naming but wanted to provide a fun fact.

1

u/granttwin2 Jun 29 '20

Thats a long tab.

1

u/MikeRoma Jun 29 '20

is that... 8 SPACE TAB????

1

u/redLamber Jun 29 '20

The title got me

1

u/ashcatchum21 Jun 29 '20

The only thing I can think of is that this makes future refactoring easier when you actually have to perform some modifications to the passed var.

So in the future instead of refactoring the code everywhere from var being accessed directly this will involve very minimal refactoring.

1

u/Ractorius Jun 29 '20

Probably not here but i used simmilar code for the future feature (localization). I knew I’ll do it later but meanwhile it returned its input. But as I say, this does not seem to be the case

1

u/[deleted] Jul 13 '20

gotcha

1

u/[deleted] Nov 03 '22

😭😭

1

u/miketwo345 Jun 29 '20

When you want to pretend your locally-scoped variable is a fancypants class variable.

1

u/GamesMaxed Jun 29 '20

Identity functions do have some uses tough

0

u/[deleted] Jun 29 '20

[deleted]

5

u/[deleted] Jun 29 '20

[removed] — view removed comment

2

u/CodenameLambda shameless Jun 30 '20

I think I'm not a newbie given how much experience I already have with programming, but I still sometimes format somewhat randomly. For instance, I have a hard time deciding between the two import styles use std::{collections::HashMap, ops}; and use std::collections::HashMap; use std::ops, so it's pretty much random (on a per project or sometimes for private projects per module basis).

And the randomness persists in other details as well. But at least such details ultimately don't really matter, so ¯\(ツ)/¯

0

u/[deleted] Jun 29 '20

[deleted]

2

u/corepipeline Jun 29 '20

But this is not in a class.

0

u/[deleted] Jun 29 '20

[deleted]

9

u/general_dubious Jun 29 '20

I mean, an identity function can be useful is numerous cases when you have higher level functions that expect a function somewhere but you want it to be neutral in your use case. This is more often met when using functional languages. See for example https://stackoverflow.com/questions/3136338/uses-for-haskell-id-function.

0

u/[deleted] Jun 29 '20

[deleted]

1

u/general_dubious Jun 29 '20

Of course, I was just replying to that other person who seemed to think identity functions are never useful.