Why do many teams put a "m_" in front of many variable names

I am the only programmer and new this year for my team. Previous years’ programmers have just gotten by, and I am trying to improved things for future years.

While going through a couple of other teams’ code I keep finding this consistency and I cannot figure out why it is done. At first I thought it stood for motors but it is not always attached to variable names for motors and speed control groups.

Please help.

1 Like
4 Likes

m_ is to say that the variable is from the class which you are working on. There are multiple variables throughout, and perhaps some similar names. By putting m_ in front, you can easily tell where it came from

1 Like

m_ stands for class member variable.

A related convention would be to use p_ to denote variable passed into a function as a parameter.

As others have said, it typically denotes a member variable. Our main reason for using it is to maintain consistency with WPILib’s conventions, in particular so new members have an easier time navigating dependencies.

1 Like

Our team’s convention operates under the same principle, but we use mVariableName to better fit in with our camelCase typing.

We also use kName for any constants, which are typically defined in Constants.java as static final types.

1 Like

While this convention is used by wpilib, it is not required and is definitely not universal. It’s basically just a personal preference, but whatever styles teams decide to use it’s very helpful to remain consistent.

One real life example is the Google Java style guide that explicitly does not use identifier prefixes.

4 Likes

Ok… I actually have no idea, but good luck being the only programmer on your team! Chiefdelphi is a great place to ask questions, so don’t hesitate!

Putting character’s in front of variable names is very much a C++ style practice and not really the Java standard. But, as @forbes said in a previous post it doesn’t matter what style you use, as long as it’s consistent.

3 Likes

5024 made a language switch, but kept (most of) our styleguide. So, we still use m_ and s_ all over the place.

No more capital method names now though (yay!!)

One year our auto-aiming code had a member variable called “mIsAligned”. This caused a lot of confusion when a programmer thought that this variable should be true when the robot was misaligned.

7 Likes

There can be good reasons for wanting to distinguish, at a glance, between variables that belong to – I.e. are a member of – a class object and local variables of a method. The exact reasons can vary from language to language and even programming styles. Prepending a variable name with an “m” is one way of accomplishing this. To accomplish the same goal, I typically use the features of the IDE to display different kinds of variables using different fonts; e.g., different colors, italicized, bold, etc.

The goal is always to maximize readability. This in turn increases development velocity, which is almost universally a good thing. To do this, concretely:

Whenever I’m editing existing code, my priority is always “maintain existing conventions”.

Whenever I’m writing code from scratch, I pick my convention according to a particular style guide.

To reiterate: Consistency is key.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.