Better Code Part I
Good Names in Code
Names are meant to be meaningful, should understand what it contains without knowing whats happening behind the scenario.
This you will be naming often
- variable/contants
- Contains dataset, results of a function… anything - use Nouns(userdata) or shortphrases with adjectives(isValid)
- function/methods
- Use verbs(sendData) or short phrases with adjectives(isInputValid)
- classes
- These are used to make an object - use Nouns or shortPhrases(User, RequestBody)
Naming convections
- SnakeCase - hello_world(python - variables, function/methods)
- camelCase - helloWorld( java or script - variables, function/methods)
- pascalCase - HelloWorld(python/java - Classes)
Varaible, Constants and Properties
- Values is an object.
- Describe the object
- User
- database
- Provide More information of the variable
- Authentic User
- Sql Database
- Describe the object
- Value can be boolean.
- Tell True or False
- isValid
- loggedIn
- Provide More information of the variable
- isUserLoggedIN
- Tell True or False
- Value can be number or string.
- Describe the value
- name
- age
- Provide More information of the variable
- firstname
- age
- Describe the value
Functions
- Functions perform an operation
- Describe the operation
- GetUserInfo
- Response.send()
- Describe the operation
- Computes a boolean
- Know about the status
- isValid()
- Purchase.isPaid() Also you can add more information about to the name like EmailResponse.send(), this tells you are working on some email responding operation.
- Know about the status
Classes
- Describe the Object
- UserProduct- it can become customer or course.
Tips
- Be consistent about the code names.( use get over fetch if you like it.)
- Dont use bad language.
- Dont overadd or under add the required info requried for the variable.
Comments
Bad comments
- Giving redundant information.
- The variable names and the comments give different information.
- Large comments that will block the code.
- Commented code is scary in production and can lead to misleading information. Use version control system to bring the old code and delete the bad code.
Good Comments
- Legal Information
- Explanation that cant be delivered by variable names.
- Important warnings while running the code.
- Import docstring that are essential for API.
Code formatting
- Vertical formatting
- Spacing of lines
- Grouping of lines
- Horizontal formatting
- intendation
- width of code lines.
- Space between the code.
Vertical Formatting
- Multiple classes in a single file, split it into multiple files.
- Adding lines between the codes, we have autoformatters to do that. Similar concepts shouldnt be seperatad by spaces.
- Stack the function in a proper classes, so the search becomes easy.
Horizontal Formatting
- Lines should be readable without even scrolling.
- Long lines can be done multiple shorter ones.
- Dont use longer variable names, so the length of the line is shorter to read.
Enjoy Reading This Article?
Here are some more articles you might like to read next: