Okay, about naming conventions

CG
Obviously Programming Something
2 min readNov 24, 2016
Naming conventions Dilbert cartoon

Naming conventions have always been a prominent topic in programming, because everyone has their own opinion on how they should be named. There are many common ones, for example some label strings as “str” then the name, ie: “strUsername” — this is a nice, obvious way to remember the data type, but it can get confusing when data types switch, for example, if you have a function and need to get either a string or array passed to it, and do checking for which is sent, how will it work? Will you name it “strorarr”? Highly unlikely as it makes no logical sense to do that. Therefore, a developer must establish a personal naming convention scheme that is comfortable with them, as well as easily understandable.

Here’s my take on naming conventions:

  1. Naming must be precise, understandable, and easily findable.
    This means no more naming every query by “query” — instead, be more focused, for example for an account question in a function called “getAccountInformation” name the variable $accountInformationQuery instead of $query, therefore if you need to reference it quickly, find getAccountInformation OR $accountInformationQuery.
  2. Be reasonable with naming! We don’t want a 64 character variable or function name, so please, keep it short, simple and unique!
  3. Add a comment! Sure, you may know exactly what that variable/function does, but does another person? It’s always nice to add a little comment that says what it does, for example, $accountInformationQuery could have a comment of “This variable [$accountInformationQuery] uses the Database [$DM] and requests the user’s account information, and searches by their ID number, which is unique for every user.”

Here’s a practical example of the above listed suggestions in PHP:

As you can see, the code is well documented, clean, and easy to understand for anyone who reads it. It has a clean naming convention, and makes sense. So to all the developers out there, get naming!

--

--

CG
Obviously Programming Something

Web Developer / Pentester / Web Designer / Privacy Advocate