Oneupweb : OOP Naming Conventions

Posted on in Blog

While working with different Object Oriented Programming (OOP) languages and frameworks, I’ve seen quite a few approaches on how to format various items. The line between personal preference and “best practices” has become blurry when dealing with naming conventions.

I have no idea what I'm doing: dog on a computer.Some decent advice on that subject is to adapt to the style of the current environment. However, even within languages it’s not uncommon to find inconsistencies. For example, take a look at the concatenated acronyms in XMLHttpRequest for JavaScript. XML is all uppercase, while Http is mixed upper and lowercase. There is no clear text treatment, making it unintuitive and more difficult to remember.

For this reason, it is a good idea to treat acronyms as words. While not immediately clear, it eliminates the guess work when attempting to recall a particular class, method or attribute; especially since acronyms tend to evolve. For example, the phrase “Away From Keyboard” eventually became “AFK” and is commonly typed as “afk”. By treating it as a word, you won’t have to guess which casing to use, just follow your chosen convention.

So, what if you have the chance to write your own library from the ground up? Perhaps something portable that shouldn’t be married to one particular language or uses many languages; what naming practices should you follow then? Of the examples I’ve seen and used in the past, here is what I personally prefer on a basic level.

class ClassName
{
  public static const MAX_USERS = 15;

  public var variable_name = "foo";

  private var _variable_name = "bar";

  public function methodName()
  {
    // do something
  }

  private function _methodName()
  {
    // do something
  }
}

ClassName

Classes use upper camel case, meaning each concatenated word begins with a capital letter followed by lowercase letters.

class ClassName
{
  // stuff
}

methodName

Methods use lower camel case, meaning the first word is all lowercase, while each additional word begins with a capital letter followed by lowercase.

public function methodName()
{
  // do something
}

variable_name

Variables use lowercase words, separated with underscores. They are dynamic objects that are usually defined on a per instance basis.

public var variable_name = "hello world!";

CONSTANT_NAME

Constants use uppercase words, separated with underscores. They are unchanging values and are typically also static (accessible directly from a class).

public static const CONSTANT_NAME = "foobar";

// accessible through the containing class without a need to instantiate.

var variable_name = ClassName.CONSTANT_NAME;

_private_variable and _privateMethod

Private variables and methods are prefixed with “_” (underscore). They are accessible only from within the object instance in which they are used. By placing an underscore before the name, the scope is instantly recognizable, keeping them separate from public variables and methods.

private var _variable_name= "foo";

private function _methodName()
{
  return "bar";
}

There are other considerations that play even deeper into personal preference, such as curly-bracket “{}” placement for functions vs loops vs if-statements. Spacing for within and around parentheses “()” is equally preferential. How you format your code is ultimately up to you and how you find it the most readable.

Again, what I mentioned above is very generalized and won’t necessarily stand well in some languages. Adapt to your environment and please, do leave comments below. I enjoy reading other programmers thoughts and opinions.

Up Next

The private sector has invested substantially in lowering carbon emissions and creating lasting, impactful changes for how they do business. The focus on sustainability isn’t about marketing; it’s about doing the right thing. Still, brands need to communicate their eco-friendly accomplishments, policies and goals to accelerate industry-wide change and appeal to consumers who care –...

Read More