Be consistent in style of a project.
Naming Conventions
It is important to use good names that are short, simple and tell the purpose of the variable/ function / file / data structure.
- Variables - start with lowercase and if multiple names then use CamelCase or snake_case depends on the standard of the programming language.
For example : longVariableName or long_variable_name.
Temporary variables can be 1 letter like "i", "j", "k", "m", "n" (Integer), "c", "d", "e" (Char) or "temp" for any kind of variable.
- Functions - start with lowercase letter and if multiple name then use CamelCase or snake_case depends on the standard of the programming language.
For example : longFunctionName() or long_function_name()
- Classes, interfaces, struct... - start of each internal word is uppercase.
For example, ProductGroup
- Constants - all letters are upper case seperated by underscore.
For Example MAX_FILES
- Files and directories - lowercase with underscore between internal words.
If it is file of class/interface name it with name of the class/interface (use capital letters).
Indents
Indents are important for readability.
- If, try/catch, switch - use whitespace between the keyword to the parenthesis. Like "if (condition)".
- Functions - don't use whitespace after function name before parenthesis. like "func()".
- Do maximum of 1 blank lines space between each line and do that only if the code is unreadable or it is different section in function.
For example :
int x;
int y;
print 'x';
print 'y';
runFunction();
- Declare variables in start of functions, each line will have 1 variable, and don't initialize them in this section .
For example :
int x;
int y;
char c;
x = 0; - {} - use with the programming language standards.
i.e in C language use it in new line (function () \n {code}) , and Java '{' will be in line of the start block (function () space { \n code}). - ; - even if it is unnecessary in the programming language, use it.
- Indent with 4 spaces (2 is also optional but 4 is more readable).
- When function parameters extend line limit, make a new line after comma (",") and the parameters start after opening parenthesis ("(") of function using 1 tab indent instead of space.
For example:
longFunctionName (int x, int y,
int z, int sum)
Short Lines
Instead of using object.property.otherProperty multiple times in the same section use declaration of a variable.
For example:
const nestedProperty = object.property.otherProperty
// use nestedProperty here
Order Of Imports
Import statements should be sorted with the most fundamental packages first, and grouped with associated packages together and one blank line between groups.
The import statement location is enforced by the Java
Up Next
As you continue to refine your programming skills, it's time to delve into the fascinating realm of coding tricks. In this stage, we will explore various techniques and strategies that can help you write efficient, clean, and elegant code. By incorporating these coding tricks into your workflow, you'll not only enhance your productivity but also elevate the quality of your code.