preface

Recently, the company is training code design, feeling is to write documentation.

And one of the biggest benefits is,

The most effective way to do development is to convince the product to iterate on that feature.

This blog post will share the results of my four days of training from the requirements stage to the final implementation.

Ready to feel the despair of four days of writing

The requirements phase

Received the demand

Js writes a function that adds two numbers

Demand divergence

  • Two numbers, up to ten billion and down to one tenth of a billion? Product are you going to use this function to calculate the distance from the sun to the Earth plus my chest size?
  • Add, the product you are sure you just want to add, rather than an embedded a small AI classmate AI intelligence, can calculate the chicken and rabbit with cage 100 first 250 feet, a few chicken and a few rabbits?

Requirements of convergence

  • After communicating with chepi for a day, the product said that as long as 1+2=3 common numbers can be added.
  • All we need is the addition function. We don’t need anything else for now.

conclusion

Requirements need to diverge and then converge, exploring requirements from function points and scenarios. Real development scene needs will be clear first, so their own to diverge content is less, more is to communicate after convergence. Convergence can be understood as cutting demand, but more is to clarify demand, to determine what should be done, what should not be done, what is more important, what is just beautification of demand.

The development of

Good, I just need a 1+2=3, implement an add(1,2) function with an output of 3. Then I’m going to start acting. (West Pawnshop example)

function sum(a,b){
    return a + b;
}
Copy the code

Emmmm, real food written in code, no design at all, have you seen the design guidelines?

Now, it’s time to show the real technique (CTRL +6)

testability

  • When it comes to testability, unit testing is best understood
  • Tends to simulate extreme scenarios and various other scenarios, such as mock data
function testSum() {
    console.log("1 plus 2 should be equal to 3.");
    console.log(sum(1, 2) === 3 ? 'pass' : 'failed');
};
testSum();
Copy the code

Create a new testsum.js file to implement the simplest unit test file.

My function is very debuggable and has its own unit debugging (proud)

Debugging can be

  • Testability and debugging, they all sound the same. But testability is mostly in the development process, through a certain way to achieve the migration of defects, bugs exposed at an early stage.
  • Debuggability focuses on your ability to detect errors if an exception occurs while someone else is using your function or component.
  • Focus on exposing problems, status, operation information or error information. Example: the vue – tools
function sum(a,b) {
    if(typeof a ! = ='number'){
        console.error('[sum] The first parameter type of the function should be Number');
        return;
    }
    if(typeof b ! = ='number'){
        console.error('[sum] The second parameter type of the function should be Number');
        return;
    }
    return a + b;
}
Copy the code

When some guy takes your sum function and adds ‘1’ and ‘2’. When he asks why you are not 3, you can feel free to praise him boldly. Young man, are you a person who makes dreams come true with both hands?

maintainability

  • Dr Capability, nGINx load balancing high Dr Mechanism

The product didn’t think it was enough. Here, this function gives me 10W times in 1ms.

Ultimately, it depends on the operating environment. The functional level has less to do with maintainable performance.

scalability

  • The product has changed its mind. I want a function that adds three numbers.
functionsum(... arguments) {let result = 0;
    arguments.forEach(item => {
        if(typeof item ! = ='number'){
            console.error('[sum] The parameter type of the function should be Number');
            return;
        }
        result += item;
    })
    return result;
}
Copy the code

No matter how many parameters you want to add up, go for the old man. Soha, old man can do.

reusability

  • Write notes or documentation, and don’t give teammates a reason to yell at you
  • Every time you say your code needs high cohesion and low coupling, you’re talking about reusability
/** ** @param {... Any} arguments Need to add arguments */functionsum(... arguments) {let result = 0;
    arguments.forEach(item => {
        if(typeof item ! = ='number'){
            console.error('[sum] The parameter type of the function should be Number');
            return;
        }
        result += item;
    })
    return result;
}
Copy the code

reliability

  • Multiple dimensions are repeated to some extent. For example, reliability is the requirement of robustness on one hand. It goes back to testability, you have to check the parameters, you have some kind of check mechanism. Be more user or developer friendly.
  • Another aspect of reliability is the ability to adapt to the current environment. For example, a small program like Digging for gold can run well on all mobile terminals, which is a withdrawal of program reliability.

The last

Having said that, the 1+2 function may not be the same as the 3 lines of code we initially thought of, but this is something we often encounter in development, from the beginning of the requirements design to the final release

On the moon can catch turtles under the sea

to

It’s not like it doesn’t work.

In fact, as a developer, I can do more than my own fish, from the demand stage to divergent convergence, confirm the product should do,

The starting point of requirements is the problem to be solved and the value to be implemented; code is just the means.

Move bricks wisely.


More than a year to the new company, because busy eating, busy moving bricks, busy gaining weight, a lot of things need to be busy and give up the habit of writing blog. To sum up, it’s too lazy. Your blog may get 10+ page views, but don’t give up.