@@ -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+ class Developer :
783+ def __init__ (self ,experience :float ,github_link : Text) ->None :
784+ self ._experience= experience
785+ self ._github_link= github_link
786+
787+ @ property
788+ def experience (self ) ->float :
789+ return self ._experience
790+
791+ @ property
792+ def github_link (self ) -> Text:
793+ return self ._github_link
794+
795+ @dataclass
796+ class Manager :
797+ def __init__ (self ,experience :float ,github_link : Text) ->None :
798+ self ._experience= experience
799+ self ._github_link= github_link
800+
801+ @ property
802+ def experience (self ) ->float :
803+ return self ._experience
804+
805+ @ property
806+ def github_link (self ) -> Text:
807+ return self ._github_link
808+
809+
810+ def get_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+ def get_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+ class Employee :
851+ def __init__ (self ,experience :float ,github_link : Text) ->None :
852+ self ._experience= experience
853+ self ._github_link= github_link
854+
855+ @ property
856+ def experience (self ) ->float :
857+ return self ._experience
858+
859+ @ property
860+ def github_link (self ) -> Text:
861+ return self ._github_link
862+
863+
864+
865+ def get_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