Programming Naming and Formatting Tips for Beginners

November 2, 2019

If you are just starting to program, naming and formatting can be pretty confusing. I've compiled a list together to hopefully make it clearer and easier.

Why I Came Up with these Tips

Back when I started programming, I had no idea how to name or format variables, functions, classes, properties and methods. Even after my first few years as a developer, I still had troubles. One day, I finally decided to sit down and solve this problem. What I came up with was patterns that have helped me make my code more consistent and easier to read.

Note: All of the code examples that I have are written in JS.

Formatting Tips

Variables and properties must be small letters. If it has 2 or more words, use underscores.

const customer     = "something";
const order_number = "123";

PascalCase for classes and objects.

class Customer {}
const OrderInfo = {};

Use underscores for functions and methods.

function get_order()

class Customer {
    get_address() {}
}

The list here allowed me to quickly identify a variable from a function or a method from an object. I've also been able to debug and read through my code more efficiently. This same experience was experienced by anyone that has read through my code.

Naming Tips

Avoid Abbreviations

As much as possible avoid abbreviating your code. How many times have you written code, tabled it for a few days, came back to it and you have no idea what the abbreviation means? So, just avoid it in the first place and save yourself from grief later. This will greatly help yourself in the future and make it easier for other developers to read your code.

Here are some common examples where I've seen abbreviation:

/*** Variable ***/
// Bad
const cstmrs = [];

// Good
const customers = [];

/*** Function ***/
// Bad
function invoice_cstmr()

// Good
function invoice_customer()

/*** Object Properties ***/
// Bad
const CustomerInfo = {
    f_name: '', // Does this stand for full name or first name?
    age: ''
}

// Good
const CustomerInfo = {
    first_name: '',
    age: ''
}

Must Use at least a Single Word for Anything New that is Getting Declared

In my day to day job, when doing pull request reviews, I always see variable declaration that look like these:

// Variable
const x = {};

// In a loop
Customers.map((c, i) => {
    if( i = 2 ) {
        c = "Something"
    }
    ...
})

Avoid the examples above, especially the one in the loop. By using at least a word, it gives better context on what is happening on the code and it'll be easier for someone else to understand your code.

Avoid Redundancy

This is harder to catch compared to the ones above. Here's what I mean:

// Bad
const Customer = {
    customer_firstname: 'John',
    customer_lastname: 'Doe'
}

// Good
const Customer = {
    firstname: 'John',
    lastname: 'Doe'
}

If you were to retrieve the first name of the customer using the bad example, the code would read Customer.customer_firstname. There is nothing wrong with this at all. In my opinion, it would read better if it was Customer.firstname. Like I said earlier, at first, this is harder to catch but as you start practicing it, you will start to notice where you do this on your code.

Take Away

Programming as a beginner is tough and I hope that you find the tips above as helpful as I have. When in doubt, always be more descriptive. Remember this is just my opinion and not the law. Do whatever is best for you and your team needs. If you have any questions or need advise on any of the tips above, feel free to drop me an email and I'll get back to you as soon as I can.