Not just beginners, I see this in production code more often than you’d expect. They code what they want the code to do, but that’s where the thinking stops and they move on.
Also some people prefer it as it aids readability for them. It’s their preference and I don’t care enough to grumble. I would change it if I’m working in the area though
I'm honestly not surprised lol. I'm a CS student and I have the feeling that the number of people who care about formatting their code and simplifying it in my class is 1 (me).
My teacher even said to me that he doesn't think compiler will even let me do it. The if statement: (Bool_Variable), what he wanted: (Int_Variable == 1). The variable is only 1 or 0 depending if cube is in front of sensor last time checked. But he is more of a hardware engineer programming machines to assemble things and making blueprints for something.
In Java, Kotlin, C# and Rust that's right, but in C, C++, Python and JavaScript you can implicitly convert to boolean and all integers but 0 are truthy.
I'm lost honestly. I don't understand what the function is supposed to do in the first place? Like why compare bullets and dragons to find if something is a hero? Pls explain
A hero has to kill dragons, with each dragon taking two bullets to kill. If there are a certain amount of dragons, can the hero kill all of them with the amount of bullets he has.
This is a Kata, a code practicing technique. The idea is to write a test then write code that passes that test then write another test. Its intended to get you to write bad solutions first that only pass one unit test specifically even if the code is really bad. This way you can learn why this code is bad.
I've also seen ones that start with "should" or "do", though those are usually properties. Also, some people use boolean returns to denote success or failure (when the error is not serious enough for an exception), especially in languages like JavaScript. Technically those are boolean functions, but they are named like normal ones.
That’s relatively idiomatic C++ to keep exceptions local to the the application boundary with a publicly exposed function that returns a [bool] instead of throwing.
37
u/Speykious Dec 26 '21
Too many beginners don't know that conditions and booleans are basically the exact same thing.
4
u/Zanderax Dec 27 '21
This is a Kata, a code practicing technique. The idea is to write a test then write code that passes that test then write another test. Its intended to get you to write bad solutions first that only pass one unit test specifically even if the code is really bad. This way you can learn why this code is bad.
9
u/linadvonapets Dec 26 '21
Just two bullets per dragon
1
u/RedditAccuName Dec 28 '21
And I just did Where(b => b == true) ...
12
u/bistr-o-math Dec 26 '21
This is hilarious. Everyone should know that Boolean functions must start with “is” or “are”.
Fix:
3
u/trollsmurf Dec 26 '21
return bullets >= 2 * dragons ? bool.parse("true") : bool.parse("false");
0
u/[deleted] Dec 26 '21
[deleted] — view deleted comment
12
u/[deleted] Dec 26 '21
[removed] — view removed comment