Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it confusing to omit the "private" keyword from a class definition?

I recently removed a private specified from a class definition because it was at the top, immediately after the class keyword:

class MyClass
{
private:
    int someVariable;
// ...

I thought that it was redundant.

A coworker disagreed with this, saying that it effectively "hid" the private nature of the data.

Most of our legacy code explicitly states the access specifiers, and usually intermingles them inconsistently throughout the definition. Our classes also tend to be very large.

I'm trying to make my newer classes small enough so that my class definitions are similar to:

class MyClass
{
    // 3-4 lines of private variables
protected:
    //  3-4 lines of protected functions
public:
    //  public interface
}

which would allow omission of the redundant access specifier while (hopefully) keeping the private members close enough to the struct/class keyword for reference.

Am I sacrificing readability for brevity, or are the struct/class keywords sufficient?

like image 953
mskfisher Avatar asked Feb 10 '11 21:02

mskfisher


People also ask

Why do we use private in Java?

The private keyword is an access modifier used for attributes, methods and constructors, making them only accessible within the declared class.

How do you declare a class without definition?

It is possible to declare a class without defining it (forward declaration) as long as it is defined later on within the translation unit. In the case of functions, one can declare a function without defining it within the translation unit, and the linker will link it to its definition in a different translation unit.

Are structs better than classes?

There is no difference between classes and structs. Structs are classes; only default access is flipped from private to public.

What is the difference between a class and a structure in C++?

A structure is a grouping of variables of various data types referenced by the same name. In C++, a class is defined as a collection of related variables and functions contained within a single structure. If no access specifier is specified, all members are set to 'public'.


2 Answers

If you are very familiar with all the default access levels then you probably won't see any difference in readability if you omit them whenever they are unnecessary.

However you will find that many people you work with aren't 100% sure about the default access level rules. This is especially true for people who regularly use different languages where the rules might be different in the different languages. As a result they might get the rules mixed up.

Always specifying the access is the safest option, if only to help the people you work with have one less thing to worry about.

like image 126
Mark Byers Avatar answered Oct 28 '22 19:10

Mark Byers


Technically, "private" at the beginning of a class or "public" at the beginning of a struct is redundant, however I personally do not like the intermingled style but rather like to order by access and by declaration type. Readability is more important to me as brevity. So I would have a section "public methods", "private attributes" and so on and I format them as such:

class A
{
public: // methods
private: // methods
private: // attributes
};

This of course also generates redundant access declarations. Also, I like putting "public" stuff first because that's most important to users of the class. So, I need an access specifier at the beginning anyway. And I put "public" at the beginning of a "struct" as well.

like image 33
Tilman Vogel Avatar answered Oct 28 '22 19:10

Tilman Vogel