Pages: 1
  Print  
Author Topic: Will somebody please kill that damn protected keyword!?  (Read 3003 times)
Offline (Unknown gender) luiscubal
Posted on: June 30, 2011, 07:15:37 PM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
There are several things I hate in programming, such as regular expressions, segmentation faults, those annoying Swing exceptions that pop once in a while out of nowhere and contain only Swing/AWT functions in the stack trace, massive compile times, GCC C++ template error messages, the lack of a standard UI library in .NET, Java's pseudo-generics, the loading times of slow IDEs and the protected access modifier.

Today, I feel like insulting protected to hell. Why? Because it's worthless. Because I hate it almost as much as I hate regular expressions, and because I apparently have nothing better to do with my time.
It's worthless in C++(I'm talking about fields/methods, I haven't thought much about "class foo : protected bar" so I won't comment on that). It's worthless in Java. It's worthless in C#. It's worthless in pretty much any language.

Seriously, we can classify classes in two categories: Those that can be inherited, and those that can not.
Often languages provide special keywords(such as "sealed" and "final") to prevent a class from being inherited. Even if that is not used, private constructors can often put a stop to inheritance.

Protected is worthless in both types of classes.

Let's assume field(or method) Foo is declared in class Bar as protected.
Now class Bar can either be inherited in a given context or it can not.

If, in that context, Bar can't be inherited, then Foo is effectively private(or, at most, the equivalent of C#'s "internal", which is a much better approach to this problem).

If, in that context, Bar can be inherited, then one can create a dummy class BarExtension with only dummy constructors, dummy implementations of abstract methods, and functions declared like "TypeOfFoo GetFoo(Bar* bar)" and "void SetFoo(Bar* bar, TypeOfFoo foo)". These functions can possibly be static(or not, whatever, both would work).
Since now one can modify the Foo of ANY Bar with the help of BarExtension(which can be defined/declared pretty much anywhere), this means that Foo is effectively "public".

So, in any given context, Foo is either private or public.
It just gives a false sensation of security, without solving anything.

Just wanted to share this. Feel free to move along with your lives now.
Logged
Offline (Male) Josh @ Dreamland
Reply #1 Posted on: June 30, 2011, 07:28:49 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2949

View Profile Email
Someone hates regular expressions?

Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #2 Posted on: June 30, 2011, 07:43:11 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
protected is extremely useful if you choose to use the extremely useless encapsulation features that C++ provides.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) luiscubal
Reply #3 Posted on: June 30, 2011, 09:01:30 PM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
@RetroX: How is it useful? Like I showed in my previous post, it offers no more encapsulation than public/private. It only makes things uglier to access. Also, I know you could always use pointer arithmetic to access private members, but this method doesn't rely on undefined behavior nor preprocessor tricks.
Logged
Offline (Female) IsmAvatar
Reply #4 Posted on: June 30, 2011, 09:33:13 PM

LateralGM Developer
LGM Developer
Location: Pennsylvania/USA
Joined: Apr 2008
Posts: 877

View Profile Email
In any given context, context is useless.

Logged
Offline (Male) Rusky
Reply #5 Posted on: July 01, 2011, 01:13:08 PM

Resident Troll
Joined: Feb 2008
Posts: 954
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
It's an explicit choice to do something stupid like writing a BarExtension. Protected can be useful, for example, on C++ constructors- The class can be inherited but not instantiated on its own, without having to resort to dummy virtual methods. Think mix-ins/traits.

Like all access control in C++, you can circumvent it easily. It's just there as a statement of intent by the programmer, so that you don't circumvent it on accident.
Logged
Offline (Unknown gender) luiscubal
Reply #6 Posted on: July 01, 2011, 02:56:46 PM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
What about Java/C#? They do have abstract, so the whole constructor thing doesn't apply.
Also, are you *sure* the class can't be instantiated on its own?

What about this?
Code: [Select]
class Foo { protected: Foo() {} };
class FooExt : public Foo { public: FooExt() { new Foo(); } };
Logged
Offline (Male) Rusky
Reply #7 Posted on: July 01, 2011, 04:07:59 PM

Resident Troll
Joined: Feb 2008
Posts: 954
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Java and C#'s abstract is just an extra layer of poop that does exactly the same thing with an extra keyword- you can circumvent it in exactly the same way.

Like I said, you can circumvent these kinds of protection easily. Java and C# have the same issue with FooExt- the point is if someone tries to instantiate Foo itself they'll be reminded by the compiler that "oh yeah, I'm not supposed to do that."

Why don't you just go program in Python or something where you have no warning whatsoever when you try to access something you're not supposed to? Then there'll be no protected or abstract uselessness to get in your way.
Logged
Offline (Unknown gender) TheExDeus
Reply #8 Posted on: July 02, 2011, 04:27:17 AM

Developer
Joined: Apr 2008
Posts: 1860

View Profile
Quote
Why don't you just go program in Python
Because python's syntax sucks ass. If a language is based on tabs, then no thanks.
Logged
Offline (Male) Rusky
Reply #9 Posted on: July 02, 2011, 08:07:12 AM

Resident Troll
Joined: Feb 2008
Posts: 954
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Quote
:troll: or something
Logged
Offline (Male) Josh @ Dreamland
Reply #10 Posted on: July 02, 2011, 10:50:52 AM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2949

View Profile Email
Besides, it doesn't matter if C++ offers a useless keyword here and there; we all know C++ is indisputably the best language known the man, if not the best language of all time.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) TheExDeus
Reply #11 Posted on: July 02, 2011, 05:27:58 PM

Developer
Joined: Apr 2008
Posts: 1860

View Profile
Quote
Besides, it doesn't matter if C++ offers a useless keyword here and there; we all know C++ is indisputably the best language known the man, if not the best language of all time.
Yeah, and I am the supermans bastard son with his laser vision and balls of steel (or was it duke).
Logged
Offline (Female) IsmAvatar
Reply #12 Posted on: July 02, 2011, 07:46:02 PM

LateralGM Developer
LGM Developer
Location: Pennsylvania/USA
Joined: Apr 2008
Posts: 877

View Profile Email
Logged
Offline (Male) RetroX
Reply #13 Posted on: July 04, 2011, 11:38:54 AM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Java is awful because it splits classes apart with shitty keywords.

An interface only has pure virtual (abstract) functions and multiple interfaces can be inherited.
An abstract function can contain mixed pure virtual (abstract) functions and only one can be inherited.
All non-abstract functions are virtual.

Under C++, you can inherit multiple classes, you can have some virtual functions and you can have some pure virtual functions.

C++ is so hard!!!!!111!!11!!!!!
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Pages: 1
  Print