Hi everyone, coming back to Developer series after long, I will be writing today about, a very basic thing every coder, actually a new coder miss. This is the right way to code that follows some rules and acceptable and understandable to all. So let’s start.
The Right Way to Code and Syntax
As we all know coding is to provide solutions to problems. But during the development process, there are more problems coming with the development phase, Actually saying, for solving a simple problem, we create more problems and do the fixing and reach to a solution. There can be many issues to the problems coming. So I will be telling these more in details.
You all must be thinking now what could be the right way I am going to write about. So let me break it into two parts. First, we will look over semantics and then Syntax. So let’s do divide the topic into two parts:
As we know we write code to do our task and optimize it. So let take the example of students. There are many things related to students like class, subject, teachers, course and much more. We create some application that manages all data similarly like as we may do without application in real life manually. So if we are not clear with what our fields be, we have a problem of understanding, more in team projects.
Let we have two developers who working on modules for the same project but coding separately, without talking to each other and explaining. Let say for roll number field. one set property as roll_no and other st_roll, they both appear different and not understandable. But if we simply write, student_roll_no. It explains itself and needs no explanation. So the meaning of a word, sentence or any letter is the semantics. If semantics are good, no need to explain, no code conflicts and productivity is more.
Similarly like during creating methods, some may save name as stMarkSave() that creates some confusion of what the function does, but if we have written like saveStudentMarks(), it is self-explanatory and needs no one to explain. So if we use correct semantics, then there will be no confusion.
Some may think, that writing method names or declaring variables with shortened keywords like st in place of the student are good, but it leads to complexity when a project grows up and you need to go through each and every file of the project to check what and why it was for, This increases complexity, debugging becomes difficult and cost grows to maintain and develop. So better to use full words, as we today have no limits on storage or like as issue that was before a decade ago or more.
So some tips for good semantics are:
1. Don’t shorten the words.
2. Use self-explanatory names.
3. Use Cases for names with multiple words,
4. Use declarative names like saveStudent in place of sv_stud or anything short.
5. Use variables with the full name like student_roll_no or studentRollNo, it reduces confusion.
Just like semantics, the syntax is also a source of confusion. The syntax is the way we write code and multiple words together like student_class or studentClass.There are different types of case for syntax:
1. CamelCase – In this the initial of the first word starts as small case, and capital for later like studentClass, studentRollNo etc.
2. Snake case – In this all letters are small joined by an underscore (_) like student_name, class_name, marks_of_maths.
3. Kebab case – In this we join letters by (-) sign like class-name, student-name
4. Pascal Case – Subset of camelcase but the initial letter is also capitalized like StudentClass.
Along semantics and syntax, there are many things that can help in writing good code. The cases like using a single method for multiple logics is bad, but writing method that does a single task is good like a method that calculates simple interest as well compound interest both is bad, but if we have two methods, one for simple interest and one for the compound interest. These are the basic principles for good programming. Below is the link you can visit to read more about this.
Solid Principles of Coding – Link 1 LInk2
I hope you like the post, if yes then do share with your friends and comment down what you think of this one, and let us know so we can improve our content. Keep sharing as sharing is caring. Stay connected and keep checking for more updates coming.