NoobOverflow
NoobOverflow

Reputation: 1288

Objective-C coder moving to C++ - method naming conventions

Being an Objective-C coder, I'm so used to descriptive method names such as

[car insertFuelWithAmount:fuelAmount
                     type:fuelType
  causesCarToSelfDestruct:NO];

How would most programmers name a corresponding method in C++? I have two questions in particular:

  1. My copy of "C++ Primer", 4th edition, mentions (p. 46) that gosh_this_is_an_impossibly_long_name_to_type is a "really bad identifier name". This differs from the mentality in Objective-C, which encourages very descriptive names, even if they are long. Why are long ones not appreciated by C++ coders?

  2. Maybe it's not a matter of just migrating blindly, but rather changing one's mindset completely? If so, what changes need to be made?

Feel free to allow some further general thoughts on your own to float along, if you don't mind. (I know this is not a forum for general and vague questions. Hence, I tried to make things a bit more specific above.)

Upvotes: 3

Views: 226

Answers (5)

Mike Seymour
Mike Seymour

Reputation: 254631

That level of descriptiveness for function parameters is best done by defining types for them, and leaving the function name itself short and readable, for example:

enum class fuel_type { petrol, diesel, coal, coffee };
enum class self_destruct { no, yes };

car.insert(amount, fuel_type::petrol, self_destruct::no);

Since functions can be overloaded for different argument types, this allows other things to be inserted by different overloads:

car.insert(amount, oil_type::crude, self_destruct::yes);

You might want to take this further, and introduce types for numeric parameters as well; especially if you don't have a "type" parameter to overload on:

struct fuel_amount {double litres;};
struct oil_amount {double litres;};

car.insert(fuel_amount{100});
car.insert(oil_amount{3});

Note that these techniques rely on new C++11 features (scoped enumerations and uniform initialisation). Similar techniques are possible, but messier, in older versions of the language.

Upvotes: 2

Ferruccio
Ferruccio

Reputation: 100718

If you want to stick with a simple method call, I would do something like:

car.insertFuel(amount, type, false);

Alternatively, you could create a Fuel class with a simple constructor:

class Fuel {
public:
    Fuel(int amount, int type, bool selfDestruct)
        : amount(amount), type(type), selfDestruct(selfDestruct) {}

    int amount;
    int type;
    bool selfDestruct;
};

car.insert(Fuel(amount, type, false));

You could also create a Fuel class which implements the named parameter idiom:

class Fuel {
public:
    Fuel() : amount(0), type(0), selfDestruct(false) {}

    Fuel& Amount(int amount) {
        this->amount = amount;
        return *this;
    }
    Fuel& Type(int type) {
        this->type = type;
        return *this;
    }
    Fuel& SelfDestruct(bool selfDestruct) {
        this->selfDestruct = selfDestruct;
        return *this;
    }

    int amount;
    int type;
    bool selfDestruct;
};

car.insert(Fuel().Amount(amount).Type(type).SelfDestruct(false));

Upvotes: 0

Lyubomir Vasilev
Lyubomir Vasilev

Reputation: 3030

Personally I (and all places that I worked so far) encourage descriptive method and variable names all over the C++ code. However, most programmers tend to dislike too long names. In such cases, usually a compromise has to be made - sacrificing some of the descriptiveness or thinking of some other words that are shorter or more descriptive in general, in order to cut down the length.

In your case I would do it like this:

car.insertFuelWithAmount(fuelAmount, fuelType, safeDestruct);

Leaving the name of the method as id and skipping the 'causesCarTo' prefix of the last parameter.

Upvotes: 0

Morten Kristensen
Morten Kristensen

Reputation: 7623

As Fernandes points out you need to find a balance of sorts. But remember that it is all about taste, though most C++ programmers go for the shorter but somewhat descriptive method names.

Using extremely long names, for anything, is never desirable because the code will become practically unreadable. And personally I find it very disturbing to my programmer's eye.

The languages are different, of course, but the focus of your transition phase would be removing the extraneous names. Like with your method above. I've noticed Obj-C programmers use the "WithSomething" extensions here and there. One thing to change here is to get rid of those by using overloading, for instance.

The method you described I would probably write like this:

car.insertFuel<int>(amount, false);

If assuming some amount of fuel in integer form.

Upvotes: 5

Justin
Justin

Reputation: 2999

As a Objective-C coder I personally create long method names in other programming languages. When I need to change game from someone else I really have a hard time to understand some code because its encrypted with unreadable method- and variable names.

Nowadays we don't have to watch out for big source-files, se let us be all clear for yourself and for others that may need to change your code.

so for your example I personally would write:

car.insertFuelTypeAndSelfDestruct(fuelAmount, fuelType, NO);

or if you can see the method description I can consider just car.insert

Upvotes: 2

Related Questions