One in the Series of 5 Top Coding Skills to improve programming skills
We pointed 5 top skills for every developer to improve the programming skills In this post
We will discuss the first point from that article in this post: Descriptive Variable Naming
It's easy to write software that works as expected as the machine understands the code. But it is very difficult to write reliable, understandable, and maintainable code. An important aspect is to use good variable and function names because that will be maintained by fellow developers.
Some properties that variable names should hold are like:
- 1. Understandable
- 2. Descriptive
- 3. Extravagant
- 4. No comment required - Self Documentation Code
Sometimes it's contradictory to say when comments are needed, the code is not well written. Don’t use comments on every line of code; The code should be self-documenting. But of course, comments are needed for the bigger picture and not every aspect can be put down to code [Business Aspect]. But we should try to maintain as much as self-documentation of the code.
Here is an example where I applied the technique of self-documentation code:
from BeautifulSoup import BeautifulSoup, Tag
def replace_a_href_with_span(soup):
links = soup.findAll("a")
for link in links:
tag = Tag(soup, "span", [("class", "looksLikeLink")])
tag.contents = link.contents
link.replaceWith(tag)
General rules of variable naming are:
- 1. Classes begin with uppercase.
- 2. Variables, objects, members, member functions, etc. start from the bottom.
- 3. Use plural notation (personid) for the container, use the singular notation (personid) for container items.
- 4. Do not use the Hungarian hint now, where the name indicates its type.
Few Examples:
Let's say we need a variable to check errors occurred or not:
One way to name the variable is: "checkForError"
Another way to name the variable similar is: "isError"
Similar we can plan the return types that we expect from function return for example:
Let's say there is a function named isEmpty that should return true or false
The use case for this function:
if(isEmpty(){
//Execute this block - If Empty
}
Another use case where we turn around the table like:
if(!isEmpty(){
//Execute this block - If Not Emtpy
}
The above case can be handled in a way where we can have another clear named function that will check if the data is not empty then it will return true else false.
Similar thing with a different way of saying it.
if(isNotEmpty(){
//Execute this block - If Not Empty
}
Keep Coding and Keep Smiling :)