Programming style can make or break a program when it comes to readability. It’s basically how your code appears to both you and anyone else that might have the fortunate opportunity of reading your assembled gold.
Here is a summary of what this page covers:
- Quick tips for Programming Style
- How to Create or Name Variables in Programming?
- How to Use Constants in Programming?
- What are Increment/Decrement Operators and How to Use Them?
- How to Use Shorthand Assignment Operators?
Here are some quick tips for assuring your style is better than anything an intern might create:
- White Space
- Include space between important things, objects, variables, etc. How would you feel if this entire article was just one really long horizontal line you had to scroll through?
- Indention
- This is kind of like white space. Basically, you want to indent child elements and all code between corresponding curly braces.
- Header Comments
- We covered this in the previous section (block comments). You want to include the author’s name, the date, and a brief description of your code at the top.
However, the name doesn’t matter all too much cause your manager will take credit for all your code regardless.
- We covered this in the previous section (block comments). You want to include the author’s name, the date, and a brief description of your code at the top.
- More Comments
- Add comments before important / big blocks of code.
- Why are you doing this?
- Why do you have to do it this way?
- What does this do?
- Add TODOs or FIX ME LATERs
- Variable Names
- Give your variables meaningful names. Try and stay away from single character or short variable names, unless they are inside of loops (coming later).
- Use “camel case” on normal variables: The variable name imboredalready would translate to: imBoredAlready
- Good Examples: resultValue, tempCalculation, inputValue, needMoreCoffee
- Bad Examples: i, value, variablevalue, i2hdapdfnvhdskf, notTrue
- Constants
- If you have a value, parameter, String, etc. that you use over and over again in your code, instead of creating it a bunch of times, you can use a constant. A constant means many things between different languages, but it’s basically a variable that isn’t changed. Typically they are all capital letters and are declared/intialized at the top of your code:
- Examples: COUNTRY_CODE, TIME_ZONE, MAX_NUMBER, PATIENCE_LIMIT
- Try to be consistent
- Even if your style isn’t the greatest, that’s okay, but try to be consistent. A program written by one person shouldn’t look like it was written by 10 different people.
These are just some basic guidelines, let’s go through and tackle more details for some of these.
How to Create or Name Variables in Programming?
- Must be comprised of letters, digits, and/or underscores
- A letter or underscore must be the first character
- In most situations, variable names are case sensitive
- Must not be a “keyword”. A keyword is reserved in the language/compiler with special meaning. Some examples include:
- main, char, int, float, return, switch, case, import, var, and package
- Note: A keyword varies between each language
- It’s also a good idea to choose a variable name that specifies the use of the variable
- For example, if you have a variable that held a list of a employees, a good name would be: employees or even employeeList
- Make your variable names at least 3+ characters long, unless it’s an extremely temporary variable (such as a counter in a loop) or an abbreviation.
- For example: An employee counter might be just emp, but naming a variable such as employeeList as just el is a bad idea.
- Use camel case for variables that may change, and ALL CAPS for constants (variables that don’t change)
How to Use Constants in Programming?
Constants should be used instead of typing out the literal value for a few reasons:
- A symbolic name (such as COUNTRY_CODE) has more meaning than its value
- If for any reason you need to change the value of the constant, you only need to change it in one place.
- Honestly, it just saves time instead of having to write the same lengthy value/string over and over again.
So why not just use a normal variable?
- To prevent change of the value during the execution of the program
- To distinguish between what values may change in a program and which ones do not.
What are Increment/Decrement Operators and How to Use Them?
First, let’s recognize some common operators in programming:
+ , - , * , /
In order, these are addition, subtraction, multiplication, and division. These are used in programming expressions, if you use them elsewhere, they might have a different effect.
Let’s take an example:
int x = 2;
x = x + 1;
So line 1, is declaring a variable named x as an int type. Line 1 is also initializing that variable to a value of 2.
Note: When giving a variable an initial value, that’s the only time you’d say it’s getting initialized, otherwise, you would just be setting the variable.
On line 2, this is a programming statement. To the left of the parenthesis is our variable that we just declared and initialized (to the value of 2) and to the right is a programming expression. This contains an operation that evaluates, and then is given to x.
This programming statement is of the form:
<variable> = <variable> <operation> <expression>
Technically, the = is an assignment operator, but more on that later.
Now finally, we can talk about increment and decrement operators.
Let’s take the following example, which builds off the previous one.
int x = 2;
x = x + 1;
x++;
x--;
Wow, what just happened. You can’t do that! Well actually…you can!
Line 3 is doing the exact same thing as line 2. We are incrementing the value of x by 1.
Line 4 is doing something similar, except we are decrementing the value of x by 1.
Here is an outline of the value of x inside of this example:
- Line 1 : x is the value of 2
- Line 2 : x is the value of 3
- Line 3 : x is the value of 4
- Line 4 : x is the value of 3
How to Use Shorthand Assignment Operators?
Now we briefly talked about operators above, but there are many other operators that come in handly. My favorites are shorthand assignment operators. These are similar to the increment/decrement operators, but can be applied in a better scenario.
These vary from language to language, but generally this is how they work. Here are some examples, using the same operators as above, but switched to shorthand operators:
// normal operators
x = x + 9;
x = x - 10;
x = x * 4;
x = x / 2;
// the same operations above, but with shorthand operators
x += 9;
x -= 10;
x *= 4;
x /= 2;
Okay, that’s enough on operators for now. I will update this page with better links later on or even another page I create myself.
Here are some reference sheets on operators per language that I found through google:
Please don’t think these guidelines are set in stone. They can vary between languages, scenarios, environments, etc. If you’re having trouble naming variables, creating constants, simply look for some examples. The operator sheets above took me less than 2 minutes to find on google and there were plenty more.
That’s an end to this page, thanks for reading!