That is dumb, but even dumber if solution is an object with an __eq__ which might make it equal to -1. Which would mean this code is not equivalent to print(solution). And yes, I have seen stuff like that.
Yes, but that's the sort of stuff that messes with people's head and makes it mentally draining to read. Same for setters that isn't the inverse of a getter:
obj.prop = a
if obj.prop != a:
print("this can happen if the setter is devious")
if not isinstance(obj.prop, type(a)):
print("even worse")
There are several methods that are expected to work in a very specific way __eq__, __repr__, __lt__, __hash__ (and friends) and whenever they do something special it can really trip you up.
This shit is the reason people hate python. Things would be fine if you all would just behave, but because someone can eventually someone will even if they really shouldn't but if it works sometimes you can get away with it.
Then some poor asshole is up till 2am debugging some fucking thing he can't figure out because the code is deceptive.
You can have different data types without them being based on an object-oriented paradigm. For instance, haskell allows supplying implementations of various operators for user-defined types through type classes (which are not the same thing as classes in an object-oriented language like C++ or Java). In haskell, operators are just functions, so this is just a special case of ad-hoc polymorphism using type classes more generally.
Just wait until you see some JavaScript someone got clever on. The language has so many built in bad practices it's rare to not have to worry about them lol.
Some of my favorite reasons:
You can redefine undefined in older language versions
Setting a high index on an array changes the length of the array to index+1. 1 element at index 1000? Cool the length is 1001, and everything else is undefined.
All kinds of type coercion weirdness if you ever forget triple equals.
Anything on any object can be hot patched swapping out methods, properties, whatever and can be done in completely unrelated code. Want String to operate different? Go for it. I could go on.
The one thing I will say about that is that at least it is so pervasive in JS that all of the developers are trained not to trust the code and most behave accordingly.
Which is fine until someone who doesn't know JS very well comes along and fuck's everything up.
There are. I'm pretty sure if you purposefully implement an operator wrong in Haskell the compiler can't detect it (if it can, please tell me how to solve the halting problem too).
It's when people ignore the rules that shit goes bad.
You're right, it's not provable in the general case, but in most cases it is provable, and at the very least it's always testable, and libraries like QuickCheck can generate a few thousand test cases for you automatically.
if x == y:
return True
elif x != y:
return False
else:
return False
def main():
first = input("enter first number")
second = input("enter second number")
result = equals(first, second)
if result is not False:
print("True")
else:
print("False")
This is not bad, just not finished. If I find an if(true) somewhere in our codebase I'm not thinking "hahaha whoever wrote this was so stupid that they didn't know how if statements work". I'm thinking more like: "Oh no, this poor soul had no time to clean up after some bug fix". edit: typo
I think this code would trigger a static code analyzer, which would make the issue persistently visible.
Of course I don't know if the coder really had this in mind, but I am quite sure that I would write the exact same code in certain circumstances, and I have 15 yoe. I would however also make a ticket in Jira and stick a link to it in a comment. Since the coder didn't do this, I think he had good intentions, good thinking, but most likely not over 5 yoe.
Also, just a comment would not trigger anyone, and this thread is proof for that. But a code like this really catches coders' attention.
You call it "subtle", I call it "machine readable".
Reddit uses a form of markdown, in which a # at the start of a newline is interpreted as a main heading (equivalent to an h1 tag in HTML). So while 'big text' is the result, in terms of semantics it's better to think of it as a heading. For smaller/sub headings use more #'s
for parameters, i prefer not to use sentinel values when possible - thereās None for that; but there are cases where you have no solution other than to use a sentinel value, yet that would be a secret object(), not something magic like -1 or 0 or "impossible" for return values, sentinels are totally unnecessary - use None or exceptions
well, mypy makes it quite easier to track None if you use Optional[...]; but what it is still missing (and i see no simple solution to that) is the difference between None and Some(None) in terms of Rust
iāve heard of dry-python/returns but had no chance to check it out yet
No worries, friend! The problem is that they carved out special logic for the case where the value of the variable solution is -1 but that code branch does the exact same thing as the branch where the value of the variable is not equal to -1. This could have been simplified by always printing solution without the if/else. Hope this helps!
You dont know the person writing the comment, he might have just picked up programming and is trying to learn, absolutely nothing wrong with that. You know what is wring though? Gatekeeping.
Well, technically, this does do something. It technically only tests if the solution is the equivalent of -1. If the solution is not a signed int type with an exact value of -1, but is equivalent to that, technically this program attempts to sanitize it. When is that ever useful in Python? Nah.
374
u/youngbull Jan 15 '22
That is dumb, but even dumber if
solutionis an object with an__eq__which might make it equal to-1. Which would mean this code is not equivalent toprint(solution). And yes, I have seen stuff like that.96
u/[deleted] Jan 15 '22
I want to know what was going through their head when writing this.
36
u/aaahsellschun Jan 15 '22
This is not bad, just not finished. If I find an if(true) somewhere in our codebase I'm not thinking "hahaha whoever wrote this was so stupid that they didn't know how if statements work". I'm thinking more like: "Oh no, this poor soul had no time to clean up after some bug fix". edit: typo
63
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 15 '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!
40
u/flavius-as Jan 15 '22
The code is bad for other reasons, but it's way more clear than straight up
print(solution)At least this code raises the signal when reading it.
My first impression of the coder doing this is rather good: they were time-constrained and did their best to put a "warning".
I would have done almost the same under time pressure, with a #TODO <ticket> before the if.
15
u/[deleted] Jan 15 '22
I'm afraid to ask but what's wrong with this code / logic?
21
u/SiJayBe86 Jan 15 '22 edited Jan 15 '22
solution = "findAnotherJobWithoutSemicolon"
5
u/EkskiuTwentyTwo i -= (i - (-1)) - i Jan 16 '22
I like to add decorative branches to my code. It makes the code more like a nice tree.
4
u/InsertMyIGNHere Jan 15 '22
Isn't this literally what yandere Dev does? But like, unironically
4
u/[deleted] Feb 03 '22
People die if they are killed
3
u/racecardiver Jan 15 '22
So elegant and beautiful.
3
u/reuben_bichage Jan 16 '22
Haha. The interesting part is if you use Jetbrains' IDEs, they will basically warn you along the lines of "whatever you're writing is silly"
3
u/cheesycheese24 Feb 03 '22
Aw hell nah
4
u/qwazwak Jan 15 '22
This shit is why I love strong typing
2
u/NewWavpro Jan 16 '22
I feel like I've done this at some point
2
u/[deleted] Jan 18 '22
why not a switch with every possible solution?
2
u/PixelPerfect41 Nov 06 '22
Of course -1 is an exception state you have to write another condition for that
2
u/geeshta Jan 15 '22
I mean solution can be a custom class with the
__eq__and__str__methods overloaded.So when you compare it to a number but printing it shows something else.
2
u/Jackarduino Jan 15 '22
Seems like a beginner had i Great idea: lets show how a good programmer i am by adding an else statement.
1
u/Dustinisgood Jan 16 '22
I can actually tell why this one is bad.
1
u/[deleted] Jan 16 '22
This is straight up r/EverySixtySecondsInAfrica
1
u/account_552 Jan 16 '22
Im a total amateur that knows surface-level C# exclusively, but that just outputs the same shit either way doesnt it
1
u/turboom Jan 21 '22
does python have special handling for ==?
1
u/Belfast_ Feb 10 '22
Pythonš¤¢š¤®
1
u/PixelPerfect41 Nov 06 '22
3 am code indeed
1
u/blue_apostrophe Jan 16 '22
Well, technically, this does do something. It technically only tests if the solution is the equivalent of -1. If the solution is not a signed int type with an exact value of -1, but is equivalent to that, technically this program attempts to sanitize it. When is that ever useful in Python? Nah.
0
u/yourgirl696969 Jan 15 '22
This made me laugh so hard for some reason
0
u/AlexLovesBeans Jan 15 '22
print errors if try to print -1 not as int literal???
0
u/deftware Jan 16 '22
i.e.
-2
u/david_ranch_dressing Jan 15 '22
This canāt be real