176
u/Amaracs Nov 22 '19
The other day I found this:
define private public //FIXME PLEASE!!!!!
It was in unitest tho
44
u/b1ack1323 Nov 22 '19
It's things like this that make me wish C++ had hard limits built into the standard
31
u/noop_noob Nov 22 '19
There are. As it turns out, doing this is undefined behavior.
6
u/jimmpony Nov 23 '19
How is that UB? Even if it makes multiple public sections it's fine.
11
u/noop_noob Nov 23 '19 edited Nov 23 '19
Technically correct answer: because the standard says so, which means compilers are allowed to do anything when this happens.
"A translation unit that uses any part of the standard library may not #define or #undef names lexically identical to: keywords... Otherwise, the behavior is undefined." https://en.cppreference.com/w/cpp/preprocessor/replace
(I guess you are technically allowed to do this janky nonsense if you don't use the standard library, and you're careful not to include the same header both normally and as "all public" in the same program.)
More useful answer: When you do defines like that, you might end up compiling your program with a standard library header which is "all public". I believe that, for some compilers, changing private to public cause them to change the order the class fields are laid out in memory. In that case, your program is going to do weird things if the standard library implementation depends on fields being in a specific order.
9
Nov 23 '19
Yeah I see that one a lot actually in unit tests because it lets you test the private members of the class. Otherwise you’d have to make the tester class a
friendthat some see as “intrusive”...But you shouldn’t be testing private members anyway; that’s not the point of a unit test..
0
Nov 23 '19
[deleted]
6
Nov 23 '19
Well said.
Also,
Having private parts big enough to require ...
oh don’t worry. My private parts aren’t big enough to require anything
3
104
u/Kidiri90 Nov 22 '19
Well, CompareBooleans does in fact compare booleans. However, AreBooleansEqual is indeed a mess.
44
u/phord Nov 22 '19 edited Nov 23 '19
This is because some style guide prohibits comparing boolean literals with logical operators to avoid people saying if ( Foo == true )
This looks like a bad compromise.
40
u/spicenozzle Nov 22 '19
Which style guides would do that? That seems so bizarre and obfuscatory.
34
u/alex_0x000a0417 Nov 22 '19
It's also the main point of logical operators. It seems like cutting off your arm just because you dropped something one time
15
u/phord Nov 22 '19
I personally loathe it.
done = triggered != falseis terrible code.11
u/BurnedRavenBat Nov 23 '19
It's terrible because you have a double negative, not because you're comparing to a literal.
Your example could be simplified totriggered == trueor even justtriggered. No need for a comparison or even another variable assignment.2
u/phord Nov 23 '19
That is the point.
return x == true;is actual code I have reviewed.1
u/JewsOfHazard Apr 30 '20
Many years ago caught myself doing this. Never again. It's like damn, the value itself has a Boolean value, so there's no need to compare it.
8
u/ikatono Nov 22 '19
I understand not comparing against boolean literals, but A == B is just XNOR.
4
4
u/kyreannightblood Nov 22 '19
You can just use the Boolean in place of the comparison, no? That’s how we do it in Python. Since the expression evaluates to a Boolean, you can replace it with a Boolean variable.
3
u/noop_noob Nov 23 '19
You can still technically do
(orig && val) || (!orig && !val)2
u/phord Nov 23 '19
That's not bad. What I can't stand is comparing anything to Boolean literals.
3
u/SuitableDragonfly Nov 23 '19
So what exactly is wrong with
orig == val? They even use it in this example they just bury it inside of two functions for some dumb reason.8
u/phord Nov 23 '19
Nothing wrong with that. But since the author wrote
if (orig == val) return true; else return false;instead of simplyreturn orig == val;, I'm assuming he's the type who elsewhere wrote
if (CompareBooleans(triggered, false)) ...2
u/TheUnlocked Nov 23 '19
Comparing with boolean literals in C# actually can be okay in the one case in which your variable is of type
bool?.2
u/purplepharoh Nov 23 '19
Even then you can just check has value and check .Value (without comparing to a literal)
3
u/BurnedRavenBat Nov 23 '19
orig.HasValue && orig.Valuevsorig == trueI'll take the literal approach...
Comparing against false or null is even worse...
!orig.HasValue || !orig.Valuevsorig != true1
u/purplepharoh Nov 23 '19
I mean yes, I never said you should, just you didnt have to compare to true or false. Also there could be a valid reason to check for null as it could mean different logic needs to be processed than simply being false. But this would mean embedded if statements.
18
u/LeJoker Nov 22 '19
As a C# rookie, what is the difference between internal and private methods?
28
u/THE1Jtux Nov 22 '19
Private is private in the class scope which means only things in said class can access it. Internal on the other hand is accessible to anything in the same assembly.
So anything in the same assembly will be able to access internal objects even if it is another class.
10
u/fekkksn Nov 22 '19
can i ask what the “assembly” encompasses and what an example of something not inside of the assembly would be?
10
u/THE1Jtux Nov 22 '19
Yeah for sure. There are probably going to be much better explanations out there, but in summary "assemblies" (sometimes referred to as projects like in Visual Studio) are the bigger chunks that make up the entire application. Assemblies are what compile down to a DLL or an EXE typically.
What that means is you may have multiple .dll files in an application, with maybe an exe that is executed to run the actual application. Each of these compiled files have their own internal components as well as the ability to interact with other components located in other assemblies.
So something that is internal is only available to items in its parent assembly (dll/exe) and something that is public can be accessed by anything in other assemblies. Private just means it cannot be accessed outside of the class it is defined in.
You can think of Public being Walmart's website, Internal being the software they use to manage their warehouse or cash registers etc (internal uses only) and private would be a specific employee's email (although that's probably not really private).
There's a lot of nuances with access modifiers that I would advise you read up on if you're working with C#. Generally public (if you NEED it to be accessed outside of the class) and private (if you don't) are good enough to work with because you can achieve most desired results. When you get into inheritance, you will want to start interacting with protected as well.
I can't speak for every group in the industry, but I personally prefer avoiding internal because I like to be as specific as possible with my access modifiers.
5
0
20
u/ManateeGag Nov 22 '19
Don't do drugs, kids.
9
u/xNeshty Nov 22 '19
Or, if you do, atleast don't do these drugs, which make stuff like that pic happen.
8
u/Beefster09 Nov 22 '19
It doesn't do what it's supposed to do, but they at least made a nand gate, rendering all other boolean operators unnecessary.
2
28
u/ulyssesphilemon Nov 22 '19
Things like this make me wonder if it's a case of bad coders getting paid per line of code.
30
7
u/ChocolateBunny Nov 22 '19
sed -i 's/AreBooleansEqual(\(.*?\),\(.*?\))/\1 != \2/g' *.csWhat's the worst that could happen.
1
u/cphoover Nov 23 '19
This can be the only acceptable answer.
Or using the find and replace tool if you are using an IDE.
4
3
6
4
Nov 23 '19
I was gonna say “couldn’t they have done return orig == value” but then realized you can avoid making a function for that entirely
2
2
1
1
u/SasquatchOnVenus Dec 19 '19
public static bool CompareBools(bool orig, bool val)
{
return !CompareBooleans(orig, val);
}Fixed.
1
u/mmanaolana May 02 '20
Image Transcription: Twitter Post
Nick, @Zorchenhimer
Found this in production today. I need a drink.
[*Screenshot of code that reads:
public static bool CompareBooleans(bool orig, bool val)
{
return AreBooleansEqual(orig, val);
}
internal static bool AreBooleansEqual(bool orig, bool val)
{
if(orig == val)
return false;
return true;
}*]
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!
-2
20
u/Famous_Profile Nov 22 '19 edited Nov 22 '19
C# bro... C#
EDIT: Y'all down voting me but this post had "other language" flair before I pointed out it is C#