1.about Parameters Checkout
Traditions to check the received parameters.
For instance, if we want to get a array of string standing for network Internet card (namely, NIC), we may compose t He following interface:
Vector<nic> nic_query (char *pch);
We need to check the parameters as follows:
if (PCH) return 0 // just return with success;
But, this is far from Enouth, we should make sure the function can run at any condition!
This is, we should make sure the following points:
- Totoal buffer is less than our buffer given
- Single NIC name was less than our buffer given
- Can not contains illegal characters, such as *, =, | And so on
- The totoal NIC number should less than our buffer number
- Others
2.about Operation Result
In the face, we is easy-to-return with opertion result when dealing with such interface as:
int setmode (int mode);
But considering the following conditions:
(1)
int getinputnum ();
We have no other impossible value indicating the operation "get" result, as our result was overlapping with Operation ResU lt!
(2)
nic* query (char* pname);
When we want to query the NIC information from database, what to indicating the operation result and query result?
We may fail to return with NULL, but is the really work all well?
Consider we just query a not existed NIC, which is, we get the query result NULL, but our program may take it as Operati On failed!
We had better separate the operation result with process result!
3.about What if Operaiton failed
Consider the following situation, we need to connect port A with Port B, the operation are to call for A's method to Poin ter to B and call for B's method to pointer to A.
So here give the simple implement code:
int Connect (Nic A, Nic B) { ConnectTo (b); Connect A to B
ConnectTo (B,a); Connect B to A
return 0; Success
What if one of the function failed?
For instance, we have ConnectTo (A, b), and it failed while connect (b,a), should we deal as follows:
int Connect (Nic A, Nic B) { if (ConnectTo (b) <0) return-1;
if (Connectot (b,a) <0) return-1;
return 0;}
The above code is treated as general Operaiton and it has flaws!
The Connect () function has a side effect as it fails but already ConnectTo (A, B), so when user know the Connect () fails, BU T
He does not known the ConnectTo (A, B) has make effect already!
We need to restore the initial status once any of the process failed, on condition that's operation is backward-able.
So, what's the recommneded solution?
int Connect (Nic A, Nic B) {
if (ConnectTo (A, b) <0)
{
if (Disconnectto (A, b) <0)
{
printf ("Restore failed!");
}
return-1;
}
if (ConnectTo (b,a) <0)
{
if (disconnect (b,a) <0)
{
printf ("Restore failed!");
}
if (Disconnectto (A, b) <0)
{
printf ("Restore failed!");
}
return-1;
}
return 0;}
Yes, this is Robost code, if one of Opeartion failed, it would restore to the initial status.
Some may wonder, what if the restore operation function failed?
Actually, we have no good ways to fix the dis-working of restore function, exception for just printf ("Restore work Faile D, I am so sorry. ").
This is usually enough-condistion, if not, we can also locate the error message.
However, when a operation includes a great many steps, and the worst condistion are, the last step fails, so we Need to restore all steps!
And you should choose a-wise solution to solve the problem, but not only for restore step by step.
4.0=sucess and-1=fail VS 0=fail, 1=sucess
In fact the above 2 tradition are usually common on nowdays programming.
We can see 1=success on application level, and in Linux Kernle, 0=sucess are the standard?
The question, who is better?
I had to admit, 1=sucess was better to understand, consider the following code:
if (read (buff) == 1) { printf ("%s", Buff); }
This is easy-to-understand, here we give another style of code:
if (read (buff) == 0) { printf ("%s", Buff);}
You'll find to understand the code easier when treat the return value as error code, which ' s
If the error code of the Read function is NULL (0), then that ' s no error occurred.
and consider the following condition:
if (is_status_on () = =1) { printf ("Current staus are on");}
The return value is about process result, when dealing with result, the code would look like:
if (is_status_on (&status) = =1) { printf ("currentstatus is%s" , status); }
The correspond code can be implemented as:
if (is_status_on () = =1) { printf ("currentstatuswas on");}
if (Is_status_on (&status) <0) { printf ("");} printf ("currentstatus is%s", status);
You can find both is easy to understand!
Some notes about industrial code