Write about a pair programming experience for a company

Touch the fish

This should be a common phenomenon. The company may have no tasks in a certain period of time, or the task weight is low, so the nerves are not tense, the hand is free, the group chat starts, and the fishing time of the day begins. Happy time is about to begin, pull the house, pull the car, pull the day, pull the salary, install MOE new, is not to improve themselves. It’s not that you don’t want to improve. Control is no match for laziness.

Pair programming

There is a very interesting phenomenon. Many people like to play video games, but they don’t want to play alone. That could be the reason for the greater desire to touch fish. Coding alone is always boring. Hence the agile development model – pair programming. Two programmers work together on a computer. One person typed the code, and the other reviewed every line of code he typed. The person who enters the code is called a driver, and the person who reviews the code is called an observer (or navigator). The two programmers often switch roles.

experience

I recently teamed up with other colleagues to program. I was the pilot and he was the pilot.

1. We need to know what is 2 to the 32nd power? The first thing that came to my mind

const m = (a,n) = > n==0?1:a*m(a,n- 1);// If n is positive
m(2.32);
Copy the code

Then a colleague wrote the code:

2**32
Copy the code

Me:?? How can you do that? I suspect I was a fake three years front end.

2. A colleague always uses a. indexof (b)>-1 to check whether a string contains b characters. Here’s one of my favorites:

var a = 'abc';
console.log(~a.indexOf(b))
Copy the code

Use the bit operator ~. When b does not exist in a, the indexOf method returns -1, ~ -1 equals 0, and 0 in js is false. This kind of reverse transposition can make the code look cleaner and have higher B bars. Of course there are extensions, ~~ can be used to take integers, like math.floor ().

3. See colleagues brush force buckle, there is a very interesting topic.leetcode-cn.com/problems/zi… My colleagues and I were distracted. We didn’t even look at other analytical thoughts. I think I’m just going to look for patterns, and I’m going to take the string and concatenate it with Index. Through drawing and comparing, I found the pattern. Each long row differs from the previous row by numRows-1 * 2, and the code comes out

/** * @param {string} s * @param {number} numRows * @return {string} */
var convert = function(s, numRows) {
    if(numRows==1) {return s
  }
  var dvalue = (numRows - 1) * 2;// Find the vertical difference
  var str = ' ';
  for (var i = 0; i < numRows; i++) {
    var df = true; // If index exceeds s length, the polling will be jumped
    var dd = true; // The broken line rule reflects the condition parameter
    var num = i
    while (df) {                                                       
      if (s[num]) {
        str += s[num]
      } else {
        df = false;
      }
      if (i == 0 || i == numRows - 1) {
        num = num + dvalue;
      } else {
        num = dd ? num + dvalue - i * 2 : num + i * 2; dd = ! dd } } }return str;
};
Copy the code

Just as I was gloating over my colleague’s success, I found that he had a completely different way of thinking than I did. He creates an array with the number of rows, then splits and populates the array and returns the data. The following code

/** * @param {string} s * @param {number} numRows * @return {string} */
var convert = function(s, numRows) {
    if(numRows==1) {return s
    }
    var ar = [];
   for(var i =0; i<numRows; i++){ ar.push([]) }var plus = true; // String padding data direction
    var nm = 0;
    for(var i =0; i<s.length; i++){ ar[nm].push(s[i]);if(nm==numRows- 1 || nm ==0){ plus = ! plus }if(plus){
             nm++;
        }else{ nm--; }}return ar.reduce((a,b) = >a+b.join(' '),' ')};Copy the code

Code concise and clear thinking. However, behind my time, it can be found that the navigator can think about the problem through drawing and other ways, which takes longer time to think and write method code earlier than the pilot.

Sometimes when I was a driver, FIRST of all, I seldom used some API, and some people were a little nervous about the code quality, so I searched Baidu for the structure and usage of API for many times. At the same time, I remember this API more deeply.

Advantages and disadvantages

1. Pair programming allows you to discover the difference between how you think and how others think. A complement to oneself in thinking mode. 2. Pair programming helps you memorize rare apis. 3. Pair programming is a great learning opportunity. 4. Pair programming produces less bad code. 5. Pair programming is best done only in free time, which is a waste of manpower. 6. Personality mismatch not suitable for pair programmingCopy the code

conclusion

There will always be free time, and it’s nice to pet a fish once in a while. However, I prefer pair programming if I don’t want to learn for a long time.

Original link:Tech.gtxlab.com/together.ht…

* Author profile: Shuan Zhang, Renhe future big data front-end engineer, focusing on HTML/CSS/JS learning and development.