Write code or write code in right way...
Writing a coding solution to a problem can be done in many ways. If the solution can be obtained with less number of code lines, then it is easy to understand and maintain.
But what if code is large with having millions of lines. Moreover maintenance and readability becomes difficult as the code base grows... Right?
Aspects to consider while coding:
- Naming convention
- Cyclomatic Complexity
- SOLID principles
- DRY Principle
- Managing dead code
- Comments
Naming conventions:
Every programming language mandates few norms in order to name variables, classes and functions etc. For ex: Java uses class names first letter to be a alphabet and rest to be lower case letters.
But if we dig little bit more, naming conventions are not limited to only upper case or lower case letters and using alphanumeric letters etc.
Naming a variable must be enough concise as naming your first child's name...
Few bad names for variables and constants:
int final static MAX_LENGHT = 2; [Correct]
Cyclomatic Complexity:
Cyclomatic complexity is a measure or metric to estimate complexity level of a snippet or block of code.
Cyclomatic complexity and best practices:
- Never put more than three conditional checks (&& operator and || operator etc.) in one if condition
- Never exceed more than thirty lines in one function or method.
- Never exceed more than 1000 lines in one class or one file.
SOLID principles:
Usually developing a large and scalable application, requires better design. Adhering to SOLID principles yields better scalable and easily maintainable applications.
There are five SOLID principles. They are:
- Single Responsibility Principle
- Open/Closed Principle
- Liskov's Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
DRY Principle:
DRY stands for Do Not Repeat Yourself. Sometimes, There could be a need to use same or similar set of lines of code to be used multiple times in the same application. For ex. String utilities, DB connections or updating records in database etc.
Writing frequently used code snippets multiple times is not a good practice. Instead move this code to a function or method and use this method in places where ever required.
Simple use cases are:
- DB Connections
- File IO Operations
- String Utilities etc.
Managing Dead Code:
Sometimes there are some lines or instructions in code base, which are completely not executed in all cases at all. This is considered as dead code.
Dead code reduces the quality with respect to the readability.
Comments:
Comments increases the readability of the program for programmers. Comments are written at class level, method level and variable level [Constants, instance variables and class variables] differently.The reason for this difference is, the pattern of comments is identified by API document generator tools and generates API documents.
Hey! I realize thgis is sort of off-topic however I had to ask.
ReplyDeleteDoes building a well-established blog like yours require
a lot of work? I am brand new to operating a blog however I do wrte in my journal everyday.
I'd like to start a blog so I can easily share
my experience and views online. Please let me know if
you have any kind of suggestions or tips for brrand new
aspiring bloggers. Appreciate it!
Hi,
DeleteFirst of all, thanks for your appreciation. The content must be unique and not plagiarized.
All the best! ◉‿◉
I like the valuable information you provide in your articles.
ReplyDeleteI will bookmark your weblog and check again here frequently.
I am quite sure I'll learn plenty of new stuff right here!
Good luck for the next!
Hi,
DeleteThank you. I added few more articles. Hope you like them. :) Happy coding!