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
Jun 29 '20
Is this written in Python
Yes
if yes, what makes it bad code?
the fact that there is a function called
getthat 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
-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
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
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
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
29
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
12
11
3
3
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
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
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
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
1
1
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
1
1
u/miketwo345 Jun 29 '20
When you want to pretend your locally-scoped variable is a fancypants class variable.
1
0
Jun 29 '20
[deleted]
5
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};anduse 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
Jun 29 '20
[deleted]
2
0
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
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.
222
u/MasterThief34 Jun 29 '20
Maybe they were paid per LOC