Jump to content
  • Advertisement
Angelic Ice

C++ Naming convention on interfaces in C++

This topic is 507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello!

I wanted to create a few interface-classes in a rather new project. Other programming languages, as C#/Java commonly agree on writing file names in a way similar to: IMyClass

Personally, I use underscore within C++, just because I tend to go for long variable-names and it improves readability. Hence writing file-names with underscore seems just the native decision. Now, the I-prefix is pascal-case, which astonishes me in general, because iMyClass is more readable, but anyway: I would rather not want to use it and of course, nobody could force me to do so anyway.

Nonetheless, I would like to know how you tackle interfaces in C++ in terms of naming?

Currently, I'm considering:

  • i_my_class.cpp (which looks a bit weird, never seen this before)
  • imy_class.cpp (less readable)
  • interface_my_class.cpp or my_class_interface.cpp

I even thought about omitting the interface/prefix altogether in C++. Curious on how you deal with this!

Thanks for your time!

 

Share this post


Link to post
Share on other sites
Advertisement

A lot of code bases opt for a change in terminology, and name their interfaces "abstract". i.e. my_abstract_class.cpp.

That's one way anyway. Edit: you'd probably want to use my_class_interface though, since you're using an interface and it would be good to keep the distinction (Apoch makes a good point about the interchanging of terms being confusing, best not contribute to that).

In my professional work our interface files are prefixed with I (we have PascalCase filenames). Not something I'm fond of, but that's how they do it.

In my personal code I don't prefix my interfaces and instead go for clear naming. If something is ambiguous I'll tackle it when it comes up (rename derived classes, change the name to something more clear, refactor the architecture if needed). In which case I'd name the files just like everything else.

Edited by Styves

Share this post


Link to post
Share on other sites

An abstract class is a different thing than an interface. Please don't perpetuate the confusion :-)

 

Aside from that:

  • I am slowly losing the habit of using I-prefix anywhere in C++
  • I personally hate naming files after classes
  • Do whatever feels like the least friction for you. I tolerate a huge amount in other people's code that would bother me in my own

Share this post


Link to post
Share on other sites

Use whatever you want, just be consistent. 

The I prefix is fairly common, such as IWidget.

A suffix is also common, such as WidgetInterface or WidgetBase.

 

Share this post


Link to post
Share on other sites

I don't know who invented the "I" prefix, but at least the Collection framework of Java doesn't use it, ie Comparable, Iterator, List. It makes sense to me, tbh, it's an abstract view to a class.

In the end though, like others said, decide something and stick with it, anything is fine as long as you're consistent. We won't ever get universal agreement on how to name things anyway, no point in worrying what others think :P

Share this post


Link to post
Share on other sites
6 hours ago, ApochPiQ said:

I am slowly losing the habit of using I-prefix anywhere in C++

Yeah I don't any more either. Interfaces, abstract-classes and concrete-classes all use the same convention.

Share this post


Link to post
Share on other sites

Systems Hungarian notation is one of the worst cargo cults to have ever been perpetrated in programming.  You should consider not bending the knee to that golden beast and so avoid prefixing the names of interface classes with the letter I.

You should be programming in terms of interfaces anyway, so name your interfaces as the base concept (eg. "class MyClass" is the interface).  After that, qualify your concrete classes that implement the interface (eg. "class MyConcreteClass" and "class MyMockClass" might be concrete classes implementing the interface... I try to stick to English grammatical constructs even though it doesn't group nicely when sorted lexicographically, because I can always use glob sorting if I need to).

Code is literature, make it humanly and humanely readable.

Share this post


Link to post
Share on other sites

I don't use the "I" prefix on interfaces. But if I can't come up with a better implementation name (e.g. because "there is only one" implementation) then I will use an "Impl" postfix on the concrete class.

I.e.

class FooImpl : Foo { };

 

But wherever possible, no warts on names at all:

class GreenFoo : Foo { };
class BlueFoo : Foo { };

 

Share this post


Link to post
Share on other sites
2 hours ago, dmatter said:

But if I can't come up with a better implementation name (e.g. because "there is only one" implementation) then I will use an "Impl" postfix on the concrete class.

Why do you have an interface then? I mean, you can just make the class without the interface, and refactor an interface into the code later, when you have a need for it?

Share this post


Link to post
Share on other sites

It isn't a bad thing.  It introduces a slight cost but keeps options open for extension later through dependency inversion.  If everything is implemented against the base interface the concrete type doesn't matter.

If the cycles were critical then I agree it is wasteful, but for what was described that won't be a concern.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!