MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/zzhhqr/cute/
r/badcode • u/superstring-man • Dec 31 '22
39 comments sorted by
149
And 2 references in 1000+ files makes it even more useful!
37 u/[deleted] Dec 31 '22 Right click -> refactor -> inline
37
Right click -> refactor -> inline
148
Ashes to ashes, int(to_int(str))
9 u/somefishingdude Dec 31 '22 We know Major Tom’s a junkie
9
We know Major Tom’s a junkie
161
Not good enough. Should be checking the type and raising a value error as well!!
68 u/457583927472811 Dec 31 '22 Just wrap it in a try block and you're goooood! 49 u/dragonfir731 Dec 31 '22 Ahhh the good old 'try except pass' 10 u/eris-touched-me Dec 31 '22 edited Dec 31 '22 But the function will raise an error when the type is wrong (except when already int or float). 5 u/[deleted] Dec 31 '22 Sounds like you’re code is too good enough to be here 😂
68
Just wrap it in a try block and you're goooood!
49 u/dragonfir731 Dec 31 '22 Ahhh the good old 'try except pass'
49
Ahhh the good old 'try except pass'
10
But the function will raise an error when the type is wrong (except when already int or float).
5 u/[deleted] Dec 31 '22 Sounds like you’re code is too good enough to be here 😂
5
Sounds like you’re code is too good enough to be here 😂
47
I don’t understand what is that code doing. Why no comments? Absolutely unreadable!
27 u/ybdiel Dec 31 '22 Don't see any unit tests either
27
Don't see any unit tests either
51
"But what if the behavior of int() changes?"
int()
24
It's pretty stunning how often I see less experienced devs create needless abstractions.
There is a cardinal rule about abstractions: they must provide some value the wrapped abstraction does not provide.
13 u/slobcat1337 Dec 31 '22 I see it more from grads. I had to tell my devs you’re not getting a grade for jamming in as many design patterns as you can. 5 u/fireflash38 Dec 31 '22 Just the other day was reviewing some code that had multiple functions to convert strings to UInts. In golang. And they literally just call strconv.ParseUInt. Whyyyyyy
13
I see it more from grads. I had to tell my devs you’re not getting a grade for jamming in as many design patterns as you can.
Just the other day was reviewing some code that had multiple functions to convert strings to UInts. In golang. And they literally just call strconv.ParseUInt. Whyyyyyy
46
When you can’t remember python syntax so you make shitty functions to help you remember
13 u/froggy_Pepe Dec 31 '22 If you can’t remember int() I can not help you
If you can’t remember int() I can not help you
21
LGTM! Merge away! 🍸🍹
32
Here's a faster version:
to_int = int
10 u/fireflash38 Dec 31 '22 Whoa whoa whoa, that has a different function signature (it takes base as second param IIRC). Can't have that. 9 u/Jonno_FTW shameless Dec 31 '22 I said it's faster, not correcter. 5 u/fireflash38 Dec 31 '22 I was joking -- you're making the code better, not worse!
Whoa whoa whoa, that has a different function signature (it takes base as second param IIRC). Can't have that.
9 u/Jonno_FTW shameless Dec 31 '22 I said it's faster, not correcter. 5 u/fireflash38 Dec 31 '22 I was joking -- you're making the code better, not worse!
I said it's faster, not correcter.
5 u/fireflash38 Dec 31 '22 I was joking -- you're making the code better, not worse!
I was joking -- you're making the code better, not worse!
8
To improve readability, extract the complex logic into own individual methods.
11
Image Transcription: Code
def to_int(string): return int(string)
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!
Just Why...
3 u/window-sil Dec 31 '22 Sometimes I wonder if this is like week 1 student code or did somebody actually find this out in the wild? 1 u/flyhigh3600 Dec 31 '22 May be this is more helpful;def to_int(string, value): try: return Int(string) except: return value
3
Sometimes I wonder if this is like week 1 student code or did somebody actually find this out in the wild?
1 u/flyhigh3600 Dec 31 '22 May be this is more helpful;def to_int(string, value): try: return Int(string) except: return value
1
May be this is more helpful;
def to_int(string, value): try: return Int(string) except: return value
just... why?
2
When life gives u lemons u make needless type conversion functions or whatever the saying goes
/r/inelegantcode
to_to_int(string): return to_int(string)
?
Hahahaha
Could be useful to set a breakpoint there or maybe add logging later or some function call.
0
It is obviously bad code, bz where are a bunch of except statements, to handle the exceptions that never occur.....?🙄
It's hard to comment on this without any other context.
[deleted] — view deleted comment
17 u/ybdiel Dec 31 '22 -> convert_string_to_int_func 6 u/Colebot0107 Dec 31 '22 -> convert_string_to_int_function_accepts_string_returns_int 1 u/0b0101011001001011 Feb 27 '23 edited Feb 28 '23 If unit test writers start writing the actual programs 2 u/CaitaXD Dec 31 '22 I prefer the c wayString to int is now strtoi
17
-> convert_string_to_int_func
6 u/Colebot0107 Dec 31 '22 -> convert_string_to_int_function_accepts_string_returns_int 1 u/0b0101011001001011 Feb 27 '23 edited Feb 28 '23 If unit test writers start writing the actual programs
6
-> convert_string_to_int_function_accepts_string_returns_int
1 u/0b0101011001001011 Feb 27 '23 edited Feb 28 '23 If unit test writers start writing the actual programs
If unit test writers start writing the actual programs
I prefer the c way
String to int is now strtoi
149
u/SuuurfiiinNeeerd Dec 31 '22
And 2 references in 1000+ files makes it even more useful!
148
u/barzamsr Dec 31 '22
Ashes to ashes, int(to_int(str))
161
u/[deleted] Dec 31 '22
Not good enough. Should be checking the type and raising a value error as well!!
47
u/CodeSmell12 Dec 31 '22
I don’t understand what is that code doing. Why no comments? Absolutely unreadable!
51
u/TheUnlocked Dec 31 '22
"But what if the behavior of
int()changes?"24
u/GodIsOnMySide Dec 31 '22
It's pretty stunning how often I see less experienced devs create needless abstractions.
There is a cardinal rule about abstractions: they must provide some value the wrapped abstraction does not provide.
46
u/Dankinater Dec 31 '22
When you can’t remember python syntax so you make shitty functions to help you remember
21
u/databasehead Dec 31 '22
LGTM! Merge away! 🍸🍹
32
u/Jonno_FTW shameless Dec 31 '22
Here's a faster version:
8
u/JKMikkelsen Dec 31 '22
To improve readability, extract the complex logic into own individual methods.
11
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 31 '22
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!
5
u/flyhigh3600 Dec 31 '22
Just Why...
3
u/[deleted] Dec 31 '22
just... why?
2
u/Weary-Ad8825 Dec 31 '22
When life gives u lemons u make needless type conversion functions or whatever the saying goes
3
u/F54280 Dec 31 '22
/r/inelegantcode
1
u/machine3lf Dec 31 '22
?
1
u/Head-Bed2065 Jan 09 '23
Hahahaha
1
u/CppMaster Dec 31 '22
Could be useful to set a breakpoint there or maybe add logging later or some function call.
0
u/69-weeb Jan 04 '23
It is obviously bad code, bz where are a bunch of except statements, to handle the exceptions that never occur.....?🙄
0
u/[deleted] Feb 08 '23
It's hard to comment on this without any other context.
0
u/[deleted] Dec 31 '22
[deleted] — view deleted comment