Check the values of all data from external sources

When getting data from a file

  • Verify that if it has valid extensions/file type
  • Verify that it does not exceeds certain file size

When getting data from a a user, the network, or some other external interface

  • Check to be sure that the data falls within the allowable range.
  • Make sure that numeric values are within tolerances
  • Strings are short enough to handle.
  • If a string is intended to represent a restricted range of values.
  • Be sure that the string is valid for its intended purpose;

Otherwise reject it.

Functions in JavaScript are objects

Functions in JavaScript are objects.

How to know in the browser?

Navigate to the console of the browser near you and type this simple function Person shown below and hit enter. Next, type Person followed by dot (.) see the methods on Person function. Pretty simple.

Thus the function is an object and has the methods as other objects in JavaScript.

We can call functions on function. Let’s invoke call function and see the output.

Invoking call method on function Person.

Prototype object:

Objects are keyed collection/associative array that has a hidden link to the prototype object. Try the following code in any browser console and understand it.

Object and prototype object has a linking.

We can access the prototype object using Object.prototype.

We can get an original object back from the prototype object using constructor (Object.prototype.constructor) property.

Pictorial representation of Object and it’s prototype object.

Oh, wait! We were talking about Function.

Like Object, Function is linked to Function.prototype.

Let me show you using Person function, it’s prototype object and their linking in the browser console.

Pictorial representation of Person function and it’s prototype object.

Test case structure

A commonly applied structure for test cases has

  • Setup: Arrange the state of the unit under test in order to run the test.
  • Execution: Act on the unit under test so that it performs the expected behavior and receive output if needed.
  • Validation: Assert/Verify the outcome of execution.
  • Cleanup: Restore unit under test to a pre-test state.

Test-driven development benefits

  • Design first: You understand requirement first via analysis, create use cases for it. Thus, the focus is on the requirement before writing the code.
  • Good enough code for now: You write sufficient code against use cases that fulfills the user requirements. You don’t write extra lines of unnecessary code.
  • Avoid rework: Due to design first approach, most of the doubts are cleared before writing code hence rework is avoided.
  • Developer confidence: Continous delivery of requirement in small cycles gives developer a confidence.
  • Regression catching.
  • Safe Refactoring: Once the code is covered by multiple tests, a developer can safely improve code structure with confidence and can focus on simple and elegant design.
  • A shared understanding of the business: Tests are the live documentation that is the communication channel between you and your future version or other developers that talks about business rules.
  • Clean code reminder: IMO, TDD always reminds you to make your code better than you found it. Of course, after covering your code with test, make it better.