Python Refactor :
Rules:
General :- Follow PEP8 Standard
- DRY - Dont Repeat Yourself ie., not more than 2 times
- 1 Function / method should perform only 1 thing
- Use helper functions (small functions)
- Simplicity (logic , naming etc )
- big functions are bad
- group related methods / variables in a file
- split or merge methods / functions using refactor feature in IDE
- Hide/Expose logic using middeleman classes
- Separate general and specific code in a class
- objects should not talk to strangers
- Use parent-child class whenever method/variables is repeated across different classes
- breaking 1 big expression in conditional statements into simple statments
- Use named tuple to mini classes for data segregation
Tools:
- Linters (Quality Checker): Pylint , PEP8 and McCabe
- Formatters (Formatter): Autopep8
- Style Checkers(recommendations): pycodestyle , pydocstyle Eg: pycodestyle file.py
- Pycharm(Refactor): In Editor > Rt ck >refactor
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Anti Patterns :
1. Too much code in a function/method2. Implementation of OOP / Not correct application of OOP /Too much OOP
3. Duplicate Code
4. Avoiding intertwine code
5. Rename variables ie., do not use v1 ,v2 etc.,
6. Avoid useless /non-operational code
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Design Patterns in OOP:
1. If subclasses need not be modifiedby others , than instantion of child object inside parent class and make parent method static.2. Instead of having checks to returm certain value , you can create a null class which returns null /empty/relavent value and instantiate that instead.Instead of actual classs.
3.Replace conditional with subclasses
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Law of Demeter
the Law of Demeter for functions requires that a method m of an object O may only invoke the methods of the following kinds of objects:O itself
public class ClassOne {
public void method1() {method2();}
public void method2() {}
}
m's parameters
public class ClassOne {
public void method1(ClassTwo classTwo) {
classTwo.method2();
}
}
class ClassTwo {
public void method2() { }
}
Any objects created/instantiated within m
public class ClassOne {
public void method1() {
ClassTwo classTwo = new ClassTwo();
classTwo.method2();
}
}
class ClassTwo {
public void method2() { }
}
O's direct component objects
public class ClassOne {
private ClassTwo classTwo;
public void method1() {
classTwo = new ClassTwo();
classTwo.method2();
}
}
class ClassTwo {
public void method2() { }
}
A global variable, accessible by O, in the scope of m
public class ClassOne {
public void method1() {
classTwo.STATIC_INSTANCE.method2();
}
}
class ClassTwo {
public static final ClassTwo STATIC_INSTANCE = ...;
public void method2() { }
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- OOP is mainly used to organize your business logic while
- AOP helps to organize your non-functional things like Auditing, Logging, Transaction Management , Security etc. This way you can decouple your business logic with non-fictional logic that makes code cleaner.
No comments:
Post a Comment