C++ Coding Style – Readability vs Using const in Parameters

ccoding-styleconst

When writing some functions, I found a const keyword in parameters like this:

void MyClass::myFunction(const MyObject& obj,const string& s1,const string& s2,const string& s3){
}

often causes splitting a line into 2 lines in IDE or vim, so I want to remove all const keywords in parameters:

void MyClass::myFunction(MyObject& obj,string& s1,string& s2,string& s3){
} 

is that a valid reason to not using const? Is it maintainable to keep the parameter objects unchanged manually?

Best Answer

Readability is a valid reason to learn to use whitespace:

void MyClass::myFunction(
        const MyObject& obj,
        const string& s1,
        const string& s2,
        const string& s3
) {
    return;
}

Located over there the parameters won't get confused with the body of the function. By locating them on a different line you won't have to reposition them when you change the name of myFunction to something more descriptive. Not changing the parameters position when they haven't changed is something source control diff tool users will appreciate.

const means something. Don't throw it out just because you're out of space and ideas. Readability is king but breaking things in it's name is just giving up.