How can I apply the DRY principle for comments?

I have several functions using the same complex line of code that I needed to comment on.

function thisFunction() {
  [Some Code..]
  // Comment for clarification
  *complex code line*
}

function thatFunction() {
  [Some Code..]
  // Comment for clarification
  *complex code line*
}

function anotherFunction() {
  [Some Code..]
  // Comment for clarification
  *complex code line*
}

The main problem that I see was to explain different functions several times with the same complex code using exact comment .

This is contrary to the DRY principle. My question is: "What would be the best practice to solve this problem and still allow readers to understand my code?"

My general thought was to comment on the first use of this complex line. However, I don't know if it will be 100% intuitive for other people if they read.

EDIT: , . , , , , . ?

+4
2

:

function thisFunction() {
  [Some Code..]
  complexCodeFunction();
}

function thatFunction() {
  [Some Code..]
  complexCodeFunction();
}

function anotherFunction() {
  [Some Code..]
  complexCodeFunction();  
}

//comment for clarification
function complexCodeFunction(){
    *complex code*
}
+8

, , :

function thisFunction() {
  [Some Code..]
  complexOperation();
}

function thatFunction() {
  [Some Code..]
  // Comment for clarification
  complexOperation();
}

function anotherFunction() {
  [Some Code..]
  complexOperation();
}

function complexOperation() {
   //comment for clarification
   *complex code line*
}

, , . DRY , - , . , .

0

Source: https://habr.com/ru/post/1665248/


All Articles