We had a C++ programming club in elementary school in grade 5, and needless to say I just copied what was in the board. I thought the insertion operator was C++’s version of quotation marks. I got really confused on why you would do std:: before everything instead of using namespace. I did search up how to make a for loop after and printed out 1-10.
Some schools used to play VERY lightly with ActionScript in Flash, but no, mostly any of us who learned coding at that age did so on our own.
My father gave me his C++ Bible and I would take it into school to paper code during study hall and whatnot though, so I guess I technically learned some of it in school, just not from the school.
This isn't bad code. This is someone learning how boolean logic works by working out the examples. And it's done correctly, too. Would you ever use this in production? Obviously not, but this isn't in prod, is it? This is solid basic learning.
You could argue that working with integers just demonstrates the knowledge that booleans are just integers under the hood, that a true === 1 and false === 0
Also, from my experience in C, being able to mix boolean and integer operations can let you build really efficient logic. This is something that can't really be done in Java.
This data type represents one bit of information, but its "size" isn't something that's precisely defined.
Like, we could argue that "byte" booleans are bad practice, where they should be implemented as bits instead. That way, 64 booleans could be compacted into a single 8-byte word.
Within the Java programming language, true is definitively not1 or any other integer value. It's an abstracted concept, so implementation specifics don't matter when logic'ing within the language.
The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.
The specifics for evaluating conditions or casting from bool to int may vary depending on the language, but it appears statements such as if (2) { ... } will work in some languages like C, but will fail in Java.
Its the same thing, sure if you want to do it conventionally the best then yes boolean would be better but for understanding stuff this code is absolutely perfect for beginners
is valid criticism. I see some more advanced logic gates in there like XOR and NAND, and it's possible that those aren't built in, but I still don't understand in what context one would be using ints to return truth or falsehood
Bad code in production is code that shouldn't be in production. Bad code in an early coding exercise is code that shouldn't be in an early coding exercise. This code shouldn't be in production, but it's not in production, so whether it's bad for production is irrelevant. It's good for an early coding exercise, and it in fact is an early coding exercise, so it's good. Whether code is good or bad depends on what it's being used for, and for the OP's purposes, the code is fine.
To be fair: I wouldn’t that consider bad code for a beginner. The naming is correct, it does something mildly useful (note how it doesn’t work with booleans, but rather with integers). That could have been part of a digital circuit simulation library.
public static int or (int a, int b)
{
if (a > 0 || b > 0)
{
return 1;
}
else
{
return 0;
}
}
public static int and (int a, int b)
{
if (a > 0 && b > 0)
{
return 1;
}
else
{
return 0;
}
}
public static int nor (int a, int b)
{
if (a > 0 || b > 0)
{
return 0;
}
else
{
return 1;
}
}
public static int nand (int a, int b)
{
if (a > 0 && b > 0)
{
return 0;
}
else
{
return 1;
}
}
To be fair, this looks like one of those programs you write when you're first learning how functions work, so you may have been writing out various functions for practice
Styling doesn’t really matter. Code can be on one line. Sure, conventionally Java would have its brace in inline while C# would have its brace on a new line, but it all compiles the same, and in this case (coding exercise) int would actually be encouraged. learncpp.com uses the new line when learning to make it easier to read.
22
u/ComfortableEye5 Nov 22 '21
Wtf you learn coding in middle school?? The only thing I learned in middle school is how to color text in microsoft word
198
u/Knuffya Nov 22 '21
When you get paid by lines of code
146
u/xiipaoc Nov 22 '21
There's absolutely nothing bad about this?
This isn't bad code. This is someone learning how boolean logic works by working out the examples. And it's done correctly, too. Would you ever use this in production? Obviously not, but this isn't in prod, is it? This is solid basic learning.
34
u/DelusionalPianist Nov 22 '21
To be fair: I wouldn’t that consider bad code for a beginner. The naming is correct, it does something mildly useful (note how it doesn’t work with booleans, but rather with integers). That could have been part of a digital circuit simulation library.
37
u/onichama Nov 22 '21
Image Transcription: Code
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!
3
u/polmeeee Nov 22 '21
I still don't have the courage to look at the code I wrote from when I first started programming.
3
u/LegoClaes Nov 22 '21
I fucking wish I started programming in middle school. Even if the code would have turned out bad.
2
u/[deleted] Nov 22 '21
I think the main problem is the redundancy. I.e.:
is easily just the one liner
If in the language you can use ints as bools anyway, it's even simpler
These could be useful for like a reduce, or for treating integers as booleans if that's not already an option in the language.
1
u/Espiring Nov 22 '21
Implement gates with expressions? Like going from rock to wood
1
u/Explosive_Eggshells Nov 22 '21
To be fair, this looks like one of those programs you write when you're first learning how functions work, so you may have been writing out various functions for practice
1
u/Emotional_Snow_8291 Nov 22 '21
learning coding in middle school? since when was this a thing
1
u/[deleted] Nov 23 '21
I mean this is not bad it could have been done in one line but I have seen worse lol
-22
u/InsertMyIGNHere Nov 22 '21
The worst part about this code is the new line for curly brace
0
u/[deleted] Nov 22 '21
[deleted] — view deleted comment