4

I'm currently writing a small language of my own, and have been considering the difference between the C++ style, where the access modifier applies to a block of members, and the C#/Java style, where the access modifier is specified separately for every member. I'm not going to ask which is better; I realize that's very opinion based.

Why does C# require an access modifier for each member?

I'm not trying to focus just on C# - it may have just been following Java or another language's convention, in which case, the question could become 'Why does that language require an access modifier for each member?'

I'm looking for any documentation, supporting quotes, etc in which the language designers discuss the pros and cons behind each syntax, and the reason they chose one over the other. I've had a look around on Google, and couldn't find anything, but I am aware that some members of Stack Overflow have actually been a part of the .Net development team, so I was hoping I might be able to find some answers here.

1
  • Another consideration is different access modifiers on properties (values), getters (read) and setters (writers). C++ doesn't have that on properties (different read and write access), though, of course, it does on the methods (so you can protect the setter, but make the getter public). Still, C#, at least, has syntax sugar where a property can be defined with access levels, and the compiler crates the backing value and the getter/setter methods. This can be quite nice, particularly for predominately struct like classes. – Kristian H Mar 7 '20 at 17:51
15

I think it is pretty obvious why in C# and Java - different from C++ - the designers choose to have access modifiers for each member. In C++, access modifiers are used in the header file of a class exclusively, where they are applied to member signatures which require seldom more than one or two lines, so grouping them in sections of public, protected and private members is sensible and manageable. Note the order of the members in the implementation file can differ from the one used in the header file.

But in languages where the code of a class is not split up into a header file and an implementation file, "sectional" access modifiers in a "C++"-alike syntax would become hard to read and error prone, since the sections where they apply would become way much longer.

C# or Java code with access modifiers like C++ would code look like this:

class MyClass
{
public: // or protected, internal, or private

    // ~100 lines of different methods of the same access level
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ~ still these methods
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    //  even more code of these methods
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // and now, for the next method to implement here, one has to scroll back 100 lines 
    // to find out about the active access modifier.

I don't think I have to explain how this would affect readablity and maintainability.

5
  • 1
    Thanks Doc. I guess I got caught up thinking about this from the perspective of member variables, and hadn't thought of method bodies – Andrew Williamson Mar 7 '20 at 7:20
  • I think this argument lacks of objective foundation. nobody writes classes like that in C++. Mostly, you’d write the access specifier in a class definition and the member’s implementation appart, oustide the class body. This leaves the class easy to read and to grasp. If you then have a 100 lines in the class definition, that would be a lot of members for a single class, and very probably some more separation of concerns would be needed. Do you have any objective reference on this design choice? or the fact that the grouped access is error prone? – Christophe Mar 7 '20 at 18:01
  • 5
    @Christophe: "Mostly, you’d write the access specifier in a class definition and the member’s implementation appart, outside the class body. This leaves the class easy to read and to grasp." - that is exactly my point. In C++, that's possible because there is usually the class defintion in the header file, and the classes implementation in the cpp file. So there access modifiers for sections (instead for each single member) make a lot more sense than in C# or Java. Maybe there you misunderstood something in my answer. – Doc Brown Mar 7 '20 at 18:37
  • If you use proper formatting and syntax-highlighting, things don't look nearly as dire. Which doesn't mean everything would be peachy, especially with longish methods. – Deduplicator Mar 7 '20 at 18:45
  • @DocBrown oh! sorry! I misundesrtood your argument! Well then +1 I agree – Christophe Mar 7 '20 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.