Do you really understand the code that you read ?

function random(): number {
return Math.random();
}

The Brian’s problem

  • Hi, Brian, how are you ?
  • Fine, sir.
  • Cool. I will give you a function, and you will tell me if you understood it, ok?
  • Yes, sir !
  • Ok, Brian, here is the function :
/**
/* Returns a number multiplied by two.
/**
function multiplyByTwo(a: number): number {
return a * 3;
}
  • Brian, did you understand this function ?
  • Yes, sir !
  • Ok, Brian, please stop to tell me “Yes, sir”, and just tell me if you are really sure that you understood it.
  • Yes, sir, I’m sure !
  • Brian…
  • Uh, sorry sir ! I’m sure of it, because I can predict that this function will always return a given number multiplied by 3.
  • And… there is not a problem with this function ?
  • No, why ?
  • There is no bug ?
  • No, sir, this function will never crash, I verified ! And it will always return a number multiplied by 3.
  • Brian… are you sure that you want to become a developer ?
  • Yes sir, why ?
  • Simple curiosity…

Bugs

In the example above, it is clear that the author of the function made a mistake: there is a contradiction between the name of the function, its description and its implementation.

Conclusion

And now, the best for last ! Assuming that the role of the function is the same for its author and for the client, we can assert the following result:

  • Brian, where are you ? I have a question for you…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gilles Fabre

Gilles Fabre

TypeScript expert. Specialization in static code analysis and in software complexity