Clang warns about doing assignments inside an if. You can use double parentheses to shut it up, which also make it clearer it's not a comparison. This method is commonly used when using streams to read a file line-by-line.
C does allow it but doesn't have a === operator, in this case, it sets the variable to '#' and then uses '#' as condotion, which resolves to always true
I mean the way I understand “truthy” is the interpretation of non-boolean values as booleans in evaluating certain expressions. C doesn’t have booleans in the first place, so there’s no truthy weak equivalence, more like it is just true.
Ooh. But besides that, the code isn't that bad. It has good grounds for potential expanding of the methods. I think the coder wouldn't really want to have only one key to support.
Then have an array of keys you do want to support or use a config for changing hot keys. What they want to do can be done in one if statement instead of 3. Plus there’s no need for the runCondition variable.
without any context of what this is actually used for: maybe it's perfectly valid for his solution that it should run after you do press # once and then press any key for debug logging or something
Funnily enough, I recently went old-repo-diving, and I found some pretty good ideas in there, some of which I'll just have to steal for new stuff...
But yeah, that's code from like, three to four years ago, and I somewhat recently found a really awful codebase again that I built back when I was still in school... But there are so many fond memories of that project in particular, because it just has some very endearing flaws because I didn't know how to maths correctly™ back in the day.
runCondition is declared outside of this function, so it is possible that it was true already when the function started. In such case, this is perfectly valid code... maybe except that '== true', but that's just a matter of style.
EDIT: I just saw that 'key = '#''. So yeah, there is a bug.
Indentation, too. And if (key) { will always trigger if if (key == '#') { would. And comparing with true is, while a matter of style, as much of a matter of style as variable names that actually tell you what's in there.
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.
/u/dogutas, 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.
If something isn't 0 it's true, and char is just a 8 bit intiger that we treat differently and \0 is 0, so anything that jis not \0 it's true, that means if you lress anything it would work
Don't we need to type cast to char to int since getKey() returns an int and int is a 32 bit integer? or something like int key = keypad.getKey(); if(key == 0){ ...
getKey returns a char(int8_t) at least in this example, because this is a 4x4 numpad, and ints in arduino are 16 bit long, because arduino is a 8 bit processor.
Should apply to most languages but some just doesn't like it, so it's up to language. Usually most lower level languages has this but higher level languages don't
This looks like an accidental double paste in an IDE with shitty auto-formatting. I refuse to believe that a human who was intellectually capable of operating a computer wrote it like this deliberately.
ok but this is basically how code for games works, its not bad practice when the main loop is constantly repeating every frame and you want to control actions with states
if you read the code from OP you might notice that runCondition is declared elsewhere so this guy's fix is actually completely different behaviour and also doesn't actually fix one of the bugs in it anyway
if you read the code from OP you might notice that runCondition is declared elsewhere so this guy's fix is actually completely different behaviour
I would say that it is fair to assume that the runCondition variable is part of the flawed logic that the other commenter was trying to fix.
and also doesn't actually fix one of the bugs in it anyway
I agree, and I never said that it was a perfect fix. But it improved the code somewhat, at least if one makes the fairly reasonable assumption that the purpose of the code was to print the statement any time the "#" key was pressed.
I would say that it is fair to assume that the runCondition variable is part of the flawed logic that the other commenter was trying to fix.
i mean you are correct based on what the op stated in other comments but without any context, you can't fairly assume that and because it could also be perfectly fine to have the variable be a global toggle
i mean you are correct based on what the op stated in other comments but without any context, you can't fairly assume that
I disagree. I would definitely call that a fair assumption. That is not at all the same thing as claiming it to be true, as in what the original intent was.
and because it could also be perfectly fine to have the variable be a global toggle
Yes, and that is also a fair assumption. There can exist multiple conflicting assumptions about the same code, and all of them can be fair and reasonable in some way.
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.
/u/RGB755, 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.
I'd say this is probably a stub or pseudo-ish code for them to fill in further later. It's not meant to be actual code, but just to give a "Here's how you check to see if a key was pressed and set a variable based on it. Here's how you use that variable later to do something." I've written stuff like this in chat/email/posts to someone who asks a question.
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.
/u/-Killercat103, 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.
100
u/stlenx Feb 23 '21
If(keypad.getkey() == '#') { Ldc.print("hi") }
I can see how this can happen but didnt the single = in the first if break everything?
31
u/barzamsr Feb 23 '21
The opposite of functional programming ladies and gentlemen: no inputs, no outputs, all global everything.
Love it.
501
u/LSatyreD Feb 23 '21
I think the fact that every single line has different whitespacing style bothers me more than the code itself
11
u/wzD_ Feb 23 '21
wat
3
u/gnobling Feb 23 '21
Other than the 'if(key = "#")' , its not wrong, just made easier to understand.
4
u/klemenkeko Feb 23 '21
This is 100% someone learning basics. I want to see your code as beginner.
204
u/Ivesy3000 Feb 23 '21
I have seen a lot worse. Most of it written by me.
39
u/Promant Feb 23 '21
runCondition is declared outside of this function, so it is possible that it was true already when the function started. In such case, this is perfectly valid code... maybe except that '== true', but that's just a matter of style.
EDIT: I just saw that 'key = '#''. So yeah, there is a bug.
7
u/walterbanana Feb 23 '21
I hate the way the curly brackets are placed so much
12
u/Metaquarx Feb 23 '21
Image Transcription: Discord
tas_dogu:
this is the worst code i've ever seen
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
1
u/DogsOnWeed Feb 23 '21
Wouldn't if(key = '#') always return true because it's not a 0 attribution? (I'm working with C)
1
u/Galse22 Feb 23 '21
I don't understand Arduino ( neither C++ )* but would the bad code be that the person could have just written
if(key = '#'){ldc.print("Hi");}?
edit: not sure if it is
key = '#'orkey == '#'but okay.1
u/Crowdcontrolz Feb 23 '21 edited Feb 23 '21
As someone who is learning... this is an actual question, would this have worked instead?
void latch() {
}
1
u/Brief-Preference-712 Feb 23 '21
If key is char, what the heck is if(key)?
1
u/valzargaming Feb 23 '21 edited Feb 23 '21
or something like that
EDIT: Yeah this would work fine. Here's an equivalent without the classes.
1
u/hgsiou Feb 24 '21
Sorry if this is a dumb question but how does the if(key) work since it's not a bool?
1
u/TerabyteF Feb 25 '21
It might be the code of a developer whom says "if it works do not touch".
2
u/6razyboy Feb 23 '21
I would not say that this is the worst code that can be found. I'm sure there is much worse than that.
Yes, it may not be very beautifully written, but C/C++ is an insidious language, and one of the common mistakes was simply made.
2
u/Glutoblop Feb 23 '21
Seems abit petty to complain about such a small code snippet, from clearly beginner programmer.
2
u/Dr_Insano_MD Feb 23 '21
The whitespace bothers me more than anything else. I could see myself writing something like this as a "hello world" proof of concept for something
But the worst part?
if(key = '#')Yeah that won't be confusing why runCondition is always true.
2
u/daterkerjabs Feb 23 '21
Ship it!
1
u/hellbuck Feb 23 '21
Not the worst I've ever seen, but it does make me frown
1
u/[deleted] Feb 23 '21
Looks like my coworkers code
1
u/Blackhaze84 Feb 23 '21
if everywhere
1
u/Bobitsmagic Feb 23 '21
I almost had a stroke reading this...
1
u/Timmy_the_tortoise Feb 23 '21
Aside from the bug and indentation, there’s really not enough going on here to assess whether it’s particularly bad code.
1
u/[deleted] Feb 23 '21
This is horrible in so many levels.
1
u/HumbleMeNow Feb 24 '21
A for effort ... but god damn it I think I just staring at it did lower my IQ. Sigh 😔
1
u/TheWorldWrecker Feb 24 '21
The indentation is also very... disturbing.
1
u/[deleted] Feb 24 '21
I love “runCondition”!
1
u/ProGamer2711 Feb 24 '21
Disgusting. Why not if(keypad.getKey() == '#') { lcd.print("Hi"); }
1
u/[deleted] Feb 24 '21
HiHiHiHiHiHiHiHiHiHiHiHiHi
1
u/toastyghost Feb 25 '21
This looks like an accidental double paste in an IDE with shitty auto-formatting. I refuse to believe that a human who was intellectually capable of operating a computer wrote it like this deliberately.
0
u/onthefence928 Feb 23 '21
ok but this is basically how code for games works, its not bad practice when the main loop is constantly repeating every frame and you want to control actions with states
-8
u/[deleted] Feb 23 '21
lemme try to fix it
if key = "#"){
lcd.print("Hi");}
there you guy buddy
0
u/[deleted] Feb 23 '21
[deleted] — view deleted comment
0
u/[deleted] Jun 27 '21
[deleted] — view deleted comment