What are the principles for parameter checking of functions and class methods (especially weakly typed languages)

when implementing a method, I always worry that the incoming parameters are illegal, but I don"t want to write a check because it"s redundant.
A simple function, a few lines of things, write a bunch of checks, and the method becomes older. And a row of methods, each with a bunch of validated if else, at the beginning, looks ugly, doesn"t it?
not to mention method nesting calls, A call Bmai B calls C Magi C call D, if each of these four methods write parameter verification internally, wouldn"t it be a waste of resources after four times of verification?
so there must be trade-offs, or norms and principles.
I would like to invite the bosses to talk about the principles in this respect. Thank you very much.

Mar.30,2022

the business method trusts the incoming parameters, and the entry method strictly validates the parameters.

The

entry method generally refers to the method of getting user input, and no external input can be trusted.

because in general, the parameters obtained by the business method will not be the original parameters directly passed down by the entry method, but the valid values will go to the business method only after a series of filtering, conversion, null detection and other verifications. then, as long as the business / entry method is well defined in the development, the need for parameter verification can be greatly reduced.

of course, business methods can verify some key parameters to ensure that there are no resource exceptions such as null pointers, and it is also a principle to terminate / roll back in time when problems are found in the business process.


check who calls. This is the principle


  1. check whether the source is trusted, for example, if it is entered by the user, then you must check
  2. .
  3. look at the specific responsibilities of the function / method. Before writing the check, ask yourself whether the caller or the callee should be responsible for the parameters
  4. .

python.org/3/library/typing.html" rel=" nofollow noreferrer "> type hints function has been provided in Python3.5

  

sail carefully for ten thousand years.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4532c-44af2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4532c-44af2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?