The code duplication reducing in the linked list implementationImplement LinkedList class in pythonOptimizing iteration through linked listsSingly linked list implementationLinked list C implementationPython Linked List implementationPython Singly Linked List ImplementationDoubly-linked list implementationLeetcode #146. LRUCache solution in Java (Doubly Linked List + HashMap)Linked List implementation in PythonImplementation of Singly Linked ListLRUCache for integers using dict + linkedlist

What to do when eye contact makes your subordinate uncomfortable?

This is why we puzzle

Fear of getting stuck on one programming language / technology that is not used in my country

Biological Blimps: Propulsion

How could a planet have erratic days?

Probability that THHT occurs in a sequence of 10 coin tosses

A binary search solution to 3Sum

How can I write humor as character trait?

When were female captains banned from Starfleet?

Why does the Sun have different day lengths, but not the gas giants?

Are these expressions not equal? Mathematica output is ambiguous

Add big quotation marks inside my colorbox

How to align my equation to left?

X marks the what?

Pre-mixing cryogenic fuels and using only one fuel tank

Non-trope happy ending?

Mimic lecturing on blackboard, facing audience

What is Cash Advance APR?

Temporarily disable WLAN internet access for children, but allow it for adults

How do you make your own symbol when Detexify fails?

Did arcade monitors have same pixel aspect ratio as TV sets?

How to get directions in deep space?

What is the English pronunciation of "pain au chocolat"?

Is there a way to get `mathscr' with lower case letters in pdfLaTeX?



The code duplication reducing in the linked list implementation


Implement LinkedList class in pythonOptimizing iteration through linked listsSingly linked list implementationLinked list C implementationPython Linked List implementationPython Singly Linked List ImplementationDoubly-linked list implementationLeetcode #146. LRUCache solution in Java (Doubly Linked List + HashMap)Linked List implementation in PythonImplementation of Singly Linked ListLRUCache for integers using dict + linkedlist













0












$begingroup$


I am writing the linked list data structure, using this question's requirements (the methods set).



I have written two working methods, which do the similar thing and differs by only few lines. So, I tried to refactor them and although I move the duplicate part to the separate function, the code became more verbose. Also, it added two function calls overhead, that is not good for data structure implementations.



The question:



  1. Do you see another way to duplication elimination?

  2. I understand, that in this particular case, it will be better to leave everything as it is, without refactoring. But from the point of best production practices and "pythonic" way, which variant will be better?

Before refactoring



def insert_before_key(self, key, value):
new_node = Node(value)
prev, curr = self._find_key(key)

if curr:
if prev:
new_node.next = curr # differs
prev.next = new_node # differs
else:
new_node.next = self.head # differs
self.head = new_node # differs

def insert_after_key(self, key, value):
new_node = Node(value)
prev, curr = self._find_key(key)

if curr:
if prev:
new_node.next = curr.next # differs
curr.next = new_node # differs
else:
new_node.next = self.head.next # differs
self.head.next = new_node # differs


After refactoring



# Common part was moved to this function
def insert(self, key, value, with_prev, non_prev):
new_node = Node(value)
prev, curr = self._find_key(key)

if curr:
if prev:
with_prev(prev, curr, new_node)
else:
non_prev(prev, curr, new_node)

# put two little chunks of code to the nested functions
# and pass them to the "insert" function, that implement the logic
def insert_before_key(self, key, value):
def with_prev(prev, curr, new_node):
new_node.next = curr
prev.next = new_node

def non_prev(prev, curr, new_node):
new_node.next = self.head
self.head = new_node

self.insert(key, value, with_prev, non_prev)

def insert_after_key(self, key, value):
def with_prev(prev, curr, new_node):
new_node.next = curr.next
curr.next = new_node

def non_prev(prev, curr, new_node):
new_node.next = self.head.next
self.head.next = new_node

self.insert(key, value, with_prev, non_prev)








share







New contributor




MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$
















    0












    $begingroup$


    I am writing the linked list data structure, using this question's requirements (the methods set).



    I have written two working methods, which do the similar thing and differs by only few lines. So, I tried to refactor them and although I move the duplicate part to the separate function, the code became more verbose. Also, it added two function calls overhead, that is not good for data structure implementations.



    The question:



    1. Do you see another way to duplication elimination?

    2. I understand, that in this particular case, it will be better to leave everything as it is, without refactoring. But from the point of best production practices and "pythonic" way, which variant will be better?

    Before refactoring



    def insert_before_key(self, key, value):
    new_node = Node(value)
    prev, curr = self._find_key(key)

    if curr:
    if prev:
    new_node.next = curr # differs
    prev.next = new_node # differs
    else:
    new_node.next = self.head # differs
    self.head = new_node # differs

    def insert_after_key(self, key, value):
    new_node = Node(value)
    prev, curr = self._find_key(key)

    if curr:
    if prev:
    new_node.next = curr.next # differs
    curr.next = new_node # differs
    else:
    new_node.next = self.head.next # differs
    self.head.next = new_node # differs


    After refactoring



    # Common part was moved to this function
    def insert(self, key, value, with_prev, non_prev):
    new_node = Node(value)
    prev, curr = self._find_key(key)

    if curr:
    if prev:
    with_prev(prev, curr, new_node)
    else:
    non_prev(prev, curr, new_node)

    # put two little chunks of code to the nested functions
    # and pass them to the "insert" function, that implement the logic
    def insert_before_key(self, key, value):
    def with_prev(prev, curr, new_node):
    new_node.next = curr
    prev.next = new_node

    def non_prev(prev, curr, new_node):
    new_node.next = self.head
    self.head = new_node

    self.insert(key, value, with_prev, non_prev)

    def insert_after_key(self, key, value):
    def with_prev(prev, curr, new_node):
    new_node.next = curr.next
    curr.next = new_node

    def non_prev(prev, curr, new_node):
    new_node.next = self.head.next
    self.head.next = new_node

    self.insert(key, value, with_prev, non_prev)








    share







    New contributor




    MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      0












      0








      0





      $begingroup$


      I am writing the linked list data structure, using this question's requirements (the methods set).



      I have written two working methods, which do the similar thing and differs by only few lines. So, I tried to refactor them and although I move the duplicate part to the separate function, the code became more verbose. Also, it added two function calls overhead, that is not good for data structure implementations.



      The question:



      1. Do you see another way to duplication elimination?

      2. I understand, that in this particular case, it will be better to leave everything as it is, without refactoring. But from the point of best production practices and "pythonic" way, which variant will be better?

      Before refactoring



      def insert_before_key(self, key, value):
      new_node = Node(value)
      prev, curr = self._find_key(key)

      if curr:
      if prev:
      new_node.next = curr # differs
      prev.next = new_node # differs
      else:
      new_node.next = self.head # differs
      self.head = new_node # differs

      def insert_after_key(self, key, value):
      new_node = Node(value)
      prev, curr = self._find_key(key)

      if curr:
      if prev:
      new_node.next = curr.next # differs
      curr.next = new_node # differs
      else:
      new_node.next = self.head.next # differs
      self.head.next = new_node # differs


      After refactoring



      # Common part was moved to this function
      def insert(self, key, value, with_prev, non_prev):
      new_node = Node(value)
      prev, curr = self._find_key(key)

      if curr:
      if prev:
      with_prev(prev, curr, new_node)
      else:
      non_prev(prev, curr, new_node)

      # put two little chunks of code to the nested functions
      # and pass them to the "insert" function, that implement the logic
      def insert_before_key(self, key, value):
      def with_prev(prev, curr, new_node):
      new_node.next = curr
      prev.next = new_node

      def non_prev(prev, curr, new_node):
      new_node.next = self.head
      self.head = new_node

      self.insert(key, value, with_prev, non_prev)

      def insert_after_key(self, key, value):
      def with_prev(prev, curr, new_node):
      new_node.next = curr.next
      curr.next = new_node

      def non_prev(prev, curr, new_node):
      new_node.next = self.head.next
      self.head.next = new_node

      self.insert(key, value, with_prev, non_prev)








      share







      New contributor




      MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I am writing the linked list data structure, using this question's requirements (the methods set).



      I have written two working methods, which do the similar thing and differs by only few lines. So, I tried to refactor them and although I move the duplicate part to the separate function, the code became more verbose. Also, it added two function calls overhead, that is not good for data structure implementations.



      The question:



      1. Do you see another way to duplication elimination?

      2. I understand, that in this particular case, it will be better to leave everything as it is, without refactoring. But from the point of best production practices and "pythonic" way, which variant will be better?

      Before refactoring



      def insert_before_key(self, key, value):
      new_node = Node(value)
      prev, curr = self._find_key(key)

      if curr:
      if prev:
      new_node.next = curr # differs
      prev.next = new_node # differs
      else:
      new_node.next = self.head # differs
      self.head = new_node # differs

      def insert_after_key(self, key, value):
      new_node = Node(value)
      prev, curr = self._find_key(key)

      if curr:
      if prev:
      new_node.next = curr.next # differs
      curr.next = new_node # differs
      else:
      new_node.next = self.head.next # differs
      self.head.next = new_node # differs


      After refactoring



      # Common part was moved to this function
      def insert(self, key, value, with_prev, non_prev):
      new_node = Node(value)
      prev, curr = self._find_key(key)

      if curr:
      if prev:
      with_prev(prev, curr, new_node)
      else:
      non_prev(prev, curr, new_node)

      # put two little chunks of code to the nested functions
      # and pass them to the "insert" function, that implement the logic
      def insert_before_key(self, key, value):
      def with_prev(prev, curr, new_node):
      new_node.next = curr
      prev.next = new_node

      def non_prev(prev, curr, new_node):
      new_node.next = self.head
      self.head = new_node

      self.insert(key, value, with_prev, non_prev)

      def insert_after_key(self, key, value):
      def with_prev(prev, curr, new_node):
      new_node.next = curr.next
      curr.next = new_node

      def non_prev(prev, curr, new_node):
      new_node.next = self.head.next
      self.head.next = new_node

      self.insert(key, value, with_prev, non_prev)






      python-3.x linked-list





      share







      New contributor




      MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 mins ago









      MiniMaxMiniMax

      1463




      1463




      New contributor




      MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          MiniMax is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216018%2fthe-code-duplication-reducing-in-the-linked-list-implementation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          MiniMax is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          MiniMax is a new contributor. Be nice, and check out our Code of Conduct.












          MiniMax is a new contributor. Be nice, and check out our Code of Conduct.











          MiniMax is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216018%2fthe-code-duplication-reducing-in-the-linked-list-implementation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          瀋陽號驅逐艦 目录 接收與服役 配置反潛直升機 武進三型性能升級 歷史 除役 參考資料 外部連結 导航菜单Taiwan Air Power海疆老兵-陽字號驅逐艦沿革World Navies Today: Taiwan (Republic of China)DD-839 USS POWER

          Memorizing the KeyboardThe Norwegian Foreman''If the B…''The Consonant EaterThe Cherry TreeElle Rend Le Coeur Plus AmoureuxFill in the blanks with the number in wordsState of the UnionFind the missing elementsCircuit DiagramWhat's the name of the game show?

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局