-
Notifications
You must be signed in to change notification settings - Fork 1
Coding Standard 01
Iffat Ara Sanzida edited this page Nov 30, 2023
·
2 revisions
Iffat Ara Sanzida (IA)
1. General:
- Avoid overly general or wordy names.
- Bad Practice:
data_structure
,my_list
,info_map
,dictionary_for_the_purpose_of_storing_data_representing_word_definitions
. - Good Practice:
user_profile
,menu_options
,word_definitions
.
- Bad Practice:
- When using CamelCase, capitalize all letters of an abbreviation (e.g.,
HTTPServer
).
2. Packages & Modules:
- Package names in lowercase, use underscores for multiple words.
- Prefer one-word names.
3. Classes:
- Follow PascalCase convention.
class Faculty(models.Model):
name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class ObeMark(models.Model):
course = models.ForeignKey(AssignCourse, null=True, on_delete=models.SET_NULL)
student = models.ForeignKey(AssignStudent, null=True, on_delete=models.SET_NULL)
def __str__(self):
return "%s %s " % (self.student.student.name, self.course.course.course_name)
- Class model name:
ProductModel
. - Class form name:
signUpForm
,productForm
. - Class model attribute:
m_productId
,m_productName
. - Class form attribute:
f_productId
,f_quantity
.
4. Variables:
- Instance and global variables in snake_case.
- Example:
exam_roll
,student_id
.
- Example:
- Non-public instance variables start with a single underscore.
class Dog:
def __init__(self, name, _age):
self.name = name
self._age = _age
5. Methods:
- Method names in lowercase (single word) or snake_case (multiple words).
def main(request):
students = Student.objects.all()
teachers = Teacher.objects.all()
def student_account(request):
student = request.user.student
form = StudentForm(instance=student)
- Non-public methods start with a single underscore.
class Calculator:
def __init__(self):
self.result = 0
def _add(self, num):
self.result += num
6. Method Arguments:
- Instance methods should have the first argument named 'self'.
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
self.speed = 0
7. Functions:
- Function names in all lowercase (single word) or snake_case (multiple words).
8. Constants:
- Constant names fully capitalized, words separated by an underscore.
9. Specific Names:
- Use 'is' prefix for Boolean variables and methods (e.g.,
isVisible
,isOpen
). - Plural forms for collections (e.g.,
values[]
). - 'n' prefix for variables representing a number of objects (e.g.,
nPoints
,nLines
).
1. Indentation:
- Use 4 spaces per indentation level.
- Prefer spaces over tabs.
# Add 4 spaces (an extra level of indentation) to distinguish arguments from the rest.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
2. Maximum Line Length and Line break:
- Limit lines to a maximum of 79 characters.
- Indent continued lines appropriately.
- Permissible to break before or after a binary operator. For new code, Knuth’s style is suggested.
# Correct:
# easy to match operators with operands
income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)
3. Blank Lines:
- Surround top-level functions and class definitions with two blank lines.
- Method definitions inside a class have a single blank line.
- Use blank lines sparingly to indicate logical sections.
4. Whitespace in Expressions and Statements:
- Avoid extraneous whitespace in various situations.
- Immediately inside parentheses, brackets, or braces:
- Immediately inside parentheses, brackets, or braces:
# Correct:
spam(ham[1], {eggs: 2})
# Wrong:
spam( ham[ 1 ], { eggs: 2 } )
- Between a trailing comma and the following close parenthesis:
# Correct:
foo = (0,)
```python
# Wrong:
bar = (0, )
- Immediately before a comma, semicolon, or colon:
# Correct:
if x == 4: print(x, y); x, y = y, x
# Wrong:
if x == 4 : print(x , y) ; x , y = y , x
- If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator:
# Correct:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
# Wrong:
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
5. Documentation String:
- Write docstrings for all public modules, functions, classes, and methods.
"""Return a foobang
Optional plotz says to frobnicate the bizbaz first.
"""
- For one-liner docstrings, keep the closing
"""
on the same line.
"""Return an ex-parrot."""
6. Comments:
- Comments that contradict the code are worse than no comments.
- Keep comments up-to-date when the code changes.
x = x + 1 # Increment x
7. Access Modifiers:
- Public Access Modifier: The members of a class that are declared public are easily accessible from any part of the program. All data members and member functions of a class are public by default.
-
Protected Access Modifier: Data members of a class are declared protected by adding a single underscore ‘_’ symbol before the data member of that class.
# super class
class Student:
# protected data members
_name = None
_roll = None
_branch = None
# constructor
def __init__(self, name, roll, branch):
self._name = name
self._roll = roll
self._branch = branch
# protected member function
def _displayRollAndBranch(self):
# accessing protected data members
print("Roll: ", self._roll)
print("Branch: ", self._branch)
-
Private Access Modifier: Data members of a class are declared private by adding a double underscore ‘__’ symbol before the data member of that class.
class Geek:
# private members
__name = None
__roll = None
__branch = None
# constructor
def __init__(self, name, roll, branch):
self.__name = name
self.__roll = roll
self.__branch = branch
# private member function
def __displayDetails(self):
# accessing private data members
print("Name: ", self.__name)
print("Roll: ", self.__roll)
print("Branch: ", self.__branch)
References: