Lalit Kumar is a software developer and programmer and has been developing websites and softwares since 2008.
Importance of Writing Clean Code
When you learn a programming language, you learn various functions, syntax, variable definition, etc. and you familiarize yourself with all aspects of that programming language. But even with that proficiency level and skills, your actual code can get obfuscated. Writing hard-to-read code is easy, but maintaining it and debugging it makes the task difficult and it shows the unprofessional-ism towards industry standards. The quality of your code is not just in its execution but also in its appearance. There is no strict coding style guideline to adhere to. It is extremely personal, and everyone has their own preferred style. You can see your style by looking back at your code you have written.
Heads Up !!
"Everything should be made as simple as possible, but not one bit simpler."
— Alber Einstein
Coding Style and Structure
It is not advisable to write hard-to-read code even if it is written only for your own. Badly structured code is unacceptable, and it makes the job very difficult if somebody else has to maintain your code. Debugging of code is a very difficult task, and if it is not written in a particular style or structure, troubleshooting job is almost impossible. If you write code in a clean and structured style, understanding the logic of program will be easy even after many years. So we must use a coding style which is clean and easy to understand, and if you are working in a team, it should be consistent within the team.
When we write some code, its structure and style show our sincerity and dedication towards our work. If you are writing in a particular manner from starting, it is very difficult to change the style. Programming is an ART and if you have started programming recently choose a coding style and stick to it. In no time, it will become your habit, and your unconscious mind trains itself to use that particular style. How you write code is a personal choice, but you have to follow some industry standards already set by master programmers. Your style of writing code should be consistent across all projects, and you should avoid changing if you are comfortable with it.
Coding styles are made up of decisions we take during code writing. These decisions involve
- Use of tabs or spaces for indentation.
- Grouping of code blocks
- Best use of white spaces
- Variable and function naming
- Design patterns to be used
- Using proper comments
"Seek simplicity, and distrust it."
— Alfred North Whitehead
Code Style Guideline
- File Names: When you create a new file, its name must be based on the job that file does. For example, if a file is used to fetch employee data from the database, you should name it like ‘FetchEmployeeData’ or not some random name like ‘NewFile’. It will make tracking file easy in future. Also, you can use camel casing (first word small) like ‘fetchEmployeeData’, if not restricted by the programming language. This is industry standard, but again the choice is yours.
- Line Length: It often becomes very confusing, if you are using very long lines in coding. You should split your line if it is becoming very long and complete code should be visible in your coding. You can define a rule for yourself that horizontal scroll-bar should not appear in your code editor area and split the line if it is appearing.
- Indentation: Indentation is necessary for writing code to define clear code block. It makes code easy to read and define the clear boundary of the code block. You can use tab or 4 white spaces for indentation.
- Using white-spaces: White-spaces can be used to provide support to the logical structure of code block. We can use them to group assignments.
- Control Flow: Always use braces in control flow (conditional and loop statements) and should avoid deeply nested loops.
Guidelines for Variables and Function Names
- Do not use nonsense names for variables. The name of the variable should serve its purpose and must be descriptive in nature.
- Truly global variables and constants should appear in UPPERCASE letters.
- Long-lived variable names should be descriptive whereas the name of the temporary variable should be small like ‘i’,’j’,’k’ used in loops.
- You can use underscore as a separator for variables with multiple names like ‘employee_name’ or can use Camlecaps like ‘employeeName’.
- Function names should follow the rules defined for the variable name.
Guidelines for OOPS
- Class name: The first letter of class name should be capitalized. Underscore should be used for multiple word names, and the first letter of each word should be capitalized. For example ‘Employee_Data’.
- Method name: Camelcaps method should be used and in multiple words name first letter of each word should be capital except first. For example ‘employeeName’.
Documentation and Comments
Apart from standard guidelines referred above, documentation is very important in writing professional code. Good quality codes are well documented with defined internal and external applications and guidelines about code. You can document the code outside the code in extra document or within the code using comments. Inline comments are very useful and can define purpose of a variable, function, class, property inside the code itself. There are software and guidelines available for each programming language on how to use comment within the code and you can generate documents directly from the code by using documentation software.
© 2018 Lalit Kumar