|
|
|
|
| Tutorial |
Choice 2:
if(is_user_online($user_name))
{
// do something
}
In the first choice, the function name looks kind of displaced: “If the user status of
John then do something.” Check this against the second possibility, “If user John is
online then do something.”The second option doesn’t break the language flow and
makes much more sense when taking a first look.The first choice leaves questions
open:What status is being referred to and how is that status being returned? The
second function name clearly indicates that this function will check the online status
of someone and return this as a Boolean value.
What if the result of the check would be returned in a variable parameter of the
function?
n Choice 1:
function user_status($user_name, &$status)
{
// retrieve status and return in $status
}
$success = user_status($user_name, $user_online);
n Choice 2:
function get_online_status($user_name, &$status)
{
// retrieve status and return in $status
}
$success = get_online_status($user_name, $user_online);
Although user_status() isn’t a bad choice for a name for this purpose,
get_online_status() suits it better.The word get clearly indicates that the function
retrieves the online status and saves it somewhere—either in a global variable or in a
variable function argument.
For functions that simply do data processing, use active names instead of passive
names. Don’t use nouns such as huffman_encoder() or database_checker()—name
the functions huffman_encode() and check_database() or switch the words into the
opposite order, whichever will best fit your module prefix.
Is Your Code Bilingual? Trilingual?
One of the most common criticisms of code involves “nationalization,” the sprinkling of the programming
language (which usually has an anglophonic origin) with another language. In our case (Tobias being
from Italy, Till being from Germany), when we reviewed projects from local programmers, people liked to
use German and Italian variable and function names instead of English names, which resulted in a
strange mix. As you probably don’t use a mixture of English, French, Spanish, or whatever in your daily
correspondence, please also show consistency while programming and use English names with PHP. It
also helps foreign people to understand what you have written.
|
|
|
|
|
|
| Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist |
|