Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit96b60ef

Browse files
authored
Merge pull requestzedr#18 from shahrukhx01/master
add DRY example by shahrukhx01
2 parents3aa1a8b +1b4abfc commit96b60ef

File tree

1 file changed

+137
-1
lines changed

1 file changed

+137
-1
lines changed

‎README.md‎

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,143 @@ print(person.name_as_first_and_last) # => ["Ryan", "McDermott"]
750750

751751
##**Don't repeat yourself (DRY)**
752752

753-
*Coming soon*
753+
Try to observe the[DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) principle.
754+
755+
Do your absolute best to avoid duplicate code. Duplicate code is bad because
756+
it means that there's more than one place to alter something if you need to
757+
change some logic.
758+
759+
Imagine if you run a restaurant and you keep track of your inventory: all your
760+
tomatoes, onions, garlic, spices, etc. If you have multiple lists that
761+
you keep this on, then all have to be updated when you serve a dish with
762+
tomatoes in them. If you only have one list, there's only one place to update!
763+
764+
Often you have duplicate code because you have two or more slightly
765+
different things, that share a lot in common, but their differences force you
766+
to have two or more separate functions that do much of the same things. Removing
767+
duplicate code means creating an abstraction that can handle this set of different
768+
things with just one function/module/class.
769+
770+
Getting the abstraction right is critical. Bad abstractions can be
771+
worse than duplicate code, so be careful! Having said this, if you can make
772+
a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself
773+
updating multiple places any time you want to change one thing.
774+
775+
**Bad:**
776+
777+
```python
778+
from typingimport List, Text, Dict
779+
from dataclassesimport dataclass
780+
781+
@dataclass
782+
classDeveloper:
783+
def__init__(self,experience:float,github_link: Text) ->None:
784+
self._experience= experience
785+
self._github_link= github_link
786+
787+
@property
788+
defexperience(self) ->float:
789+
returnself._experience
790+
791+
@property
792+
defgithub_link(self) -> Text:
793+
returnself._github_link
794+
795+
@dataclass
796+
classManager:
797+
def__init__(self,experience:float,github_link: Text) ->None:
798+
self._experience= experience
799+
self._github_link= github_link
800+
801+
@property
802+
defexperience(self) ->float:
803+
returnself._experience
804+
805+
@property
806+
defgithub_link(self) -> Text:
807+
returnself._github_link
808+
809+
810+
defget_developer_list(developers: List[Developer]) -> List[Dict]:
811+
developers_list= []
812+
for developerin developers:
813+
developers_list.append({
814+
'experience' : developer.experience,
815+
'github_link' : developer.github_link
816+
})
817+
return developers_list
818+
819+
defget_manager_list(managers: List[Manager]) -> List[Dict]:
820+
managers_list= []
821+
for managerin managers:
822+
managers_list.append({
823+
'experience' : manager.experience,
824+
'github_link' : manager.github_link
825+
})
826+
return managers_list
827+
828+
## create list objects of developers
829+
company_developers= [
830+
Developer(experience=2.5,github_link='https://github.com/1'),
831+
Developer(experience=1.5,github_link='https://github.com/2')
832+
]
833+
company_developers_list= get_developer_list(developers=company_developers)
834+
835+
## create list objects of managers
836+
company_managers= [
837+
Manager(experience=4.5,github_link='https://github.com/3'),
838+
Manager(experience=5.7,github_link='https://github.com/4')
839+
]
840+
company_managers_list= get_manager_list(managers=company_managers)
841+
```
842+
843+
**Good:**
844+
845+
```python
846+
from typingimport List, Text, Dict
847+
from dataclassesimport dataclass
848+
849+
@dataclass
850+
classEmployee:
851+
def__init__(self,experience:float,github_link: Text) ->None:
852+
self._experience= experience
853+
self._github_link= github_link
854+
855+
@property
856+
defexperience(self) ->float:
857+
returnself._experience
858+
859+
@property
860+
defgithub_link(self) -> Text:
861+
returnself._github_link
862+
863+
864+
865+
defget_employee_list(employees: List[Employee]) -> List[Dict]:
866+
employees_list= []
867+
for employeein employees:
868+
employees_list.append({
869+
'experience' : employee.experience,
870+
'github_link' : employee.github_link
871+
})
872+
return employees_list
873+
874+
## create list objects of developers
875+
company_developers= [
876+
Employee(experience=2.5,github_link='https://github.com/1'),
877+
Employee(experience=1.5,github_link='https://github.com/2')
878+
]
879+
company_developers_list= get_employee_list(employees=company_developers)
880+
881+
## create list objects of managers
882+
company_managers= [
883+
Employee(experience=4.5,github_link='https://github.com/3'),
884+
Employee(experience=5.7,github_link='https://github.com/4')
885+
]
886+
company_managers_list= get_employee_list(employees=company_managers)
887+
```
888+
889+
754890

755891
**[⬆ back to top](#table-of-contents)**
756892

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp