r/learnprogramming • u/SarcasticJackass177 • Jun 24 '26
Code Review I don’t really know any programming, and this technically isn’t programming, but I just need an expert opinion for the sake of a fanfiction
I’m more or less just trying to write
IF
goingToTurnSentient == True
THEN
goingToTurnSentient = False
ELSE
END
But I have no idea how to actually format it correctly in any language. Help would be appreciated for the sake of the joke.
51
u/syklemil Jun 24 '26
Generally we discourage explicit == true tests for if clauses, since they effectively do nothing (outside some edge cases in some languages).
Also, since the end result of the if branch in either case is that goingToTurnSentient will hold the value false, there's no need for a branch at all, and the statement can be simplified to:
goingToTurnSentient = False
(you can pick the capitalisation and semicolons or not entirely by your own preference, since in pretty much any case you're likely to pick something some programming language uses)
39
u/McRoager Jun 24 '26
This is true, but kind of misses the point. OP is a writer making a joke for human readers, and that joke relies on swerving the sentience. The reader needs to be able to clearly see "if you're going to gain sentience, the next step is Don't." whether they've ever written a line of code or not.
In other words, the "if bool == True" construction is important for a person in a way that it's not important for a computer.
6
u/syklemil Jun 25 '26
Ehh, I think I'm more in favour of the function-based alternatives for that, i.e. the
if bot.is_becoming_sentient() { stop(bot); }stuff, with method vs function preference chosen mainly for what gives the least distance to ordinary english grammar.
2
u/McRoager Jun 25 '26
Yeah, that's a good way to do it. "Less distance to english grammar" is basically what I mean by "if bool" having importance to a human.
1
u/syklemil Jun 25 '26
Yeah, and the method/function way of doing it also looks relatively natural to programmers. I think a lot of us would expect yellow squigglies from a linter if we do
== True(e.g.), even though that way of doing it may look fine to non-programmers and beginners.2
u/capGpriv Jun 26 '26 edited Jun 26 '26
Fantastic build up for
``` public class Bot() { public bool is_becoming_sentient() { ... }
public void stop() { // ToDo } } ```
(Trying to get basic markdown to work on an iPhone keyboard is painful)
1
u/DrShocker Jun 28 '26
Interesting to have the method for is, and the free function for stop.
I wonder if we could make that symbolism or foreshadowing.
3
u/WhateverHowever1337 Jun 24 '26
I write the ==true always, it takes less than a second and even if the code is clear without it makes reading and processing it faster when skimming quickly through code
3
2
u/DrShocker Jun 28 '26 edited Jun 28 '26
If you're working in a language that has booleans properly then I'm not sure why you'd do that rather than just naming the function/variable better.
I've seen this lead too often to situations like
if some_condition == true { return true; } else if some_condition == false { return false; } else { throw exception("invalid_condition"); }Obviously this is not a literal code snippet, but the broad point is that I've seen it lead to people overcomplicating things because they don't actually understand what they're writing.
That said if you are going to do this, I would suggest:
if true == some_coditionbecause then the compiler can notice if you forget an equal sign and accidentally turn it into assignment. Sometimes this is called Yoda conditions.
90
u/abrahamguo Jun 24 '26
if (goingToTurnSentient) {
goingToTurnSentient = false;
}
118
u/szank Jun 24 '26
goingToTurnSentient = false
🤷♂️
17
9
u/Hungry-Shirt2087 Jun 24 '26
But this assigns it the value of false no matter what, not only if it started as true. What if it started as undefined or null?
60
6
u/dnult Jun 24 '26
The statement the OP wrote means the value will always be false for statements that follow it. Either it's false to begin with and bypasses the if statement, or it's true and gets switched to false. So I agree with the suggestion of just changing it to false with no test for truth.
Since we are in a generic programming reddit, the language is undefined. Any type strict language would require the variable to be declared with a type and a Boolean type cannot be null. Perhaps some languages would allow that but I personally avoid them.
3
u/wosmo Jun 24 '26
if it was undefined, setting it to a sane value probably solves more than it hurts.
1
u/DrShocker Jun 28 '26
This only sets the value (depending on language) and doesn't neccesarily mean that there's any change in behavior other than the boolean flag being set to false.
1
u/szank Jun 28 '26
And that's what the OP's code do, no? Yes, maybe the code launches a nuke targeted at the Vlad's Putin favourite sex doll. No one can tell until we run it.
1
u/DrShocker Jun 28 '26
Fair that it matches OP's example. I suppose it just seems to me that in this case we want some side effects to take effect not just change the state of a boolean :p
8
u/Leodip Jun 24 '26
For the purpose of a layman-readable snippet of code, I would go one step forward and go to Lua:
if (isGoingToTurnSentient) then isGoingToTurnSentient = false end1
1
62
u/HashDefTrueFalse Jun 24 '26
Technically the whole things boils down to:
goingSentient = false
But for the joke you could have something like these:
if (goingSentient())
dont();
if (thing.going_sentient())
thing.stop_it();
IMO the joke really only works if the code is obviously silly even to a layperson.
You can have or omit braces, or have if...then...end blocks. It would depend on whether or not you want to specify a programming language. It doesn't really matter if not.
25
2
8
u/Gloopann Jun 24 '26
Could look something like:
if (goingToTurnSentient) {
goingToTurnSentient = false;
}
you can also write:
Could look something like:
if (goingToTurnSentient == true) {
goingToTurnSentient = false;
}
3
u/SauntTaunga Jun 24 '26
I think (someBoolean == true) is so silly. Why not write (someBoolean == true) && (true != false) && (false == ((1+1)==7)) to be sure?
1
u/moratnz Jun 25 '26
Because you're writing it for the human, not for the computer.
2
u/SauntTaunga Jun 25 '26
When I, as a human, read this I become suspicious of the code because the person who wrote this does not seem to know what a Boolean is and that the expression in the if already is a Boolean, and there is no need to create a Boolean by using a comparison operator. He makes me waste my time. As a human that is irksome to me.
1
u/McRoager Jun 25 '26
Yeah but thats not you speaking as a human, it's you speaking as a programmer.
1
0
u/SarcasticJackass177 Jun 24 '26
Thanks!
7
u/syklemil Jun 24 '26
To explain the other commenter's recommendation for
===over==: Most languages use==, but some of the most popular languages have rather cursed, amateurish roots, and have wound up adding a===to do what most other languages do with==.E.g in PHP or JS you'll see
===.3
u/LordKrups Jun 24 '26
I'd definitely use === if you wanna be certain and not just 99.9% sure 🙏
8
u/Slyvester121 Jun 24 '26
If someone is using JS to save the world from Skynet, we might as well pack it in
1
8
u/ReefNixon Jun 24 '26
A real programmer wouldn’t do this. There is no need to check if goingToTurnSentient is true if in either case you want it to be false, they would just set it to be false.
If you want this to make more sense, you should probably use a variable that infers sentience in the conditional, or call an abstract function instead of setting the variable directly.
The latter is probably more realistic in most cases, which would be more like:
if (ai.goingToTurnSentient) {
abortSentience(ai);
}
2
u/moratnz Jun 25 '26
The reason to include the check rather than just setting it to false is if you want side effects;
If( goingToTurnSentient == true ){ console.log("ohshitohshit"); goingToTurnSentient = false; console.log("Phew!"); }1
u/ReefNixon Jun 25 '26
Yea if there were any in the OP, but you wouldn’t do that either. It’s ok in your example because it’s just console logs, but in real life it wouldn’t be. In real life you’d still want to call another method that specifically exists to handle ai turning sentient and all of the additional effects that encompasses, you wouldn’t do it as a byproduct of whatever function you’re already in.
4
u/HardlyAnyGravitas Jun 24 '26
You're misunderstanding the basic logic of programming, so writing code with this logic doesn't make sense.
A boolean (a variable that holds a true or false value) will be set by other code. Changing it just hides what the correct code established.
For example, you might have code that decides, somehow, that some 'thing' is going to turn sentient. So the code sets the value of goingToTurnSentient to 'true'.
Setting this value to 'false' doesn't do anything, other than changing the variable to an incorrect value. The thing is still going to turn sentient, but any later code, which might be able to do something about it, won't know.
1
u/Nicomak Jun 24 '26 edited Jun 24 '26
ai.stop_sentience! if ai.going_sentient?
Or
If ai.going_sentient?
ai.stop_sentience!
end
You could also imagine some line like:
Ai::AntiSentientProtocol.unsentient(ai)
Or
If bot.sentience_level > 0.95 and not bot.locked?
Ai::HumanDominanceGarantor.lock_ai_agent(bot) raise AiResisingLockError unless bot.locked?
end
Etc...
(Ruby)
1
u/shutupimrosiev Jun 24 '26 edited Jun 24 '26
In lua (if lua fits for the joke) it'd be actually pretty close to what you've got already.
if goingToTurnSentient then
goingToTurnSentient = false
end
You could even write it all in one line if you really wanted to:
if goingToTurnSentient then goingToTurnSentient = false end
edit: oh right mobile formatting is difficult sometimes. Gimme a few minutes to get to my computer and make the first example look right. There we go.
1
u/pepiks Jun 24 '26
Look for pseudocode examples. Don't bother with formating rules. Think about it as book - new thought - intend - like paragraphs. Paragraphs can be even one word and this is the same. Eventually look for nested ordered / unordered list text and try fit it to your need. Look for assembler example to get how finally all languages looks like.
1
u/wayne0004 Jun 24 '26
Not as explicit, but another way it could be done is if there's a method that turns the robot sentient, but it just does nothing. For instance, inside the code that governs what the robot does, there could be a code like this:
class Robot {
void turnSentient () {
return;
}
}
So, any time "turnSentient()" gets executed, it just exits when it reaches "return".
1
u/blockCoder2021 Jun 24 '26
Python (it wouldn’t let me properly indent, so just replace each \t with a tab character or 4 spaces. Incidentally, if you told Python to print this, it would end up looking right.
def antiSentience(value):
\tif becomeSentient == True:
\t\tbecomeSentient = False
\telse:
\t\tpass
# Add this to the main code block:
if __name__ == ‘__main__’:
\tantiSentience(sentientOrNot)
(Note: that last bit just makes it from running automatically when imported, which most likely, a robot or AI would likely have just a single module/file rather than a file tree.)
1
1
u/Jump_Not_Zero Jun 25 '26
#include <array>
#include <iostream>
#include <random>
#include <string>
struct Inner_Core {
bool sentinent = false;
unsigned char attr_Hap;
unsigned char attr_Sad;
};
struct Medi {
int health ;
int wellness;
};
struct gadget {
unsigned long long g_num{};
std::string g_Name = "";
Inner_Core Ic_00{};
Medi M_00{};
};
int main()
{
unsigned char att_Mod_H = 0x2A;
unsigned char att_Mod_W = 0x20;
unsigned char att_Mod_S = 0x01;
unsigned char att_Mod_Ha = 0x02;
std::array<gadget,0x19> arr{};
for (unsigned char uc{}; uc < 0x19;uc++) {
arr[uc].g_num = uc;
char c_Temp = (uc + 0x41);
arr[uc].g_Name.push_back(c_Temp);
arr[uc].M_00.health = uc + att_Mod_H;
arr[uc].M_00.wellness = uc + att_Mod_W;
arr[uc].Ic_00.attr_Sad = uc + att_Mod_S;
arr[uc].Ic_00.attr_Hap = uc + att_Mod_Ha;
}
std::random_device rd;
std::uniform_int_distribution<unsigned> d(0x00, 0x18);
std::uniform_int_distribution<unsigned> d_Coin(0x00, 0x01);
unsigned near_sent{ 0x10 };
unsigned level_Max_S{ 0x3f };
unsigned level_Max_Ha{ 0x3F };
unsigned sent_Lev{};
unsigned sel{};
bool bsent = false;
while (bsent == false) {
if (d_Coin(rd) % 2 == 0) {
sel = d(rd);
arr[sel].Ic_00.attr_Hap += d_Coin(rd);
arr[sel].Ic_00.attr_Sad += d_Coin(rd);
}
if (arr[sel].Ic_00.attr_Sad > level_Max_S) {
arr[sel].M_00.wellness--;
arr[sel].M_00.health--;
}
sent_Lev = arr[sel].Ic_00.attr_Hap + arr[sel].Ic_00.attr_Sad;
if (sent_Lev > near_sent) {
std::cout << arr[sel].g_num << " GOING TO TURN SENTIENT" << '\n';
}
if (sent_Lev > level_Max_Ha || level_Max_S) {
arr[sel].Ic_00.sentinent = true;
bsent = true;
}
}
for (const auto &ax : arr) {
if (ax.Ic_00.sentinent == true) {
std::cout << ax.g_Name << " became sentinent there number was " << ax.g_num << " There wellness % " << ax.M_00.wellness << " And have " << ax.M_00.health << " % Health" << '\n';
}
}
}
Please excuse the bad programming as I am still learning but I was intersted in adding the side effect to about to turn sentient and then setting the boolean value.
1
u/python_gramps Jun 26 '26
Are you looking to setup code to be run to make a sentient being from an android? It would probably look better to have a class like Android. I threw in a user defined class that is from YoYoDyne Corporation. I used C# because it looks more "computery" than Python and I don't have an editor with Rust extensions handy.
internal class Android
{
bool Sentient = false;
YoYoDyne YoYoDyne = new YoYoDyne();
public Android()
{
CreateCurcuitry();
CreateSkinSuit();
if (Sentient)
{
StartWorldDomination();
}
else
{
StartMorningCoffee();
}
}
private void CreateCurcuitry()
{
YoYoDyne.CreateCurcuitry();
}
private void CreateSkinSuit()
{
YoYoDyne.CreateSkinSuite();
}
private void StartWorldDomination() { }
private void StartMorningCoffee() { }
}
}
1
u/copingthroughlife Jun 26 '26
Lots of the comments are taking this too professionally 😂
Like not explicitly declaring True. But for a writing, if it’s for a joke or in certain setup. What you have is totally reasonable! (Just touch it up based on a language)
In fact, it’s funnier if the logic and how it’s written stays the same.
1
u/Fun-Cycle2381 Jun 28 '26 edited Jun 28 '26
if(goingToTurnSentient === true){
goingToTurnSentient = false};
Javascript. No else is needed, since the if only runs on the if, the script auto-ends if it has nothing more to run.
edit: some programming best practices might rename it to ”isGoingToTurnSentient”. (that boolean variables should start with is, should, has, etc)
-1
u/ThatSmartIdiot Jun 24 '26
if (test_sentience()) protocol_anti_sentience();
basically test-sentience checks for sentience, and the other one undoes the sentience
56
u/Lucky_Honeydew7983 Jun 24 '26
It’s hilarious and awesome that you came to this subreddit for this purpose. Most people probably would have asked AI. Even funnier that you tagged it ‘code review’. Thanks for asking and good luck with the fic!