Sample best practice example

This example summarizes the best practices using all the elements that we have learned so far, by creating a basic program.

Problem statement: Parse all the countries declared in an array and only print the names of those countries that contain the letter I or letter U in their names:


Program begin:

Comment: This is a sample program to explain best practice
Comment: Author name: Programmer
Comment: Email: [email protected]
Version: 1.0

Comment: The following section declares the list of countries in array countrylist
countrylist=['India','US','UK','France','China','Japan']

function validatecountryname(countryname)
Comment: This function takes the input of countryname, checks if it contains I or U and returns value based upon the result.
if ((countryname contains 'I') OR (countryname contains 'U')
return "Countryname contains I or U"
else
return "Countryname does not contain I our U"

Comment: This is a loop that parses each countryname from the countrylist one by one and sends the variable 'countryname' as input to function validatecoutryname

foreach countryname in countrylist
validatecountryname (countryname)

Comment: Program ends here

The program is self-explanatory, but it is worth noting the support comments such as author, email, and so on. The indentation ensures that any reader has a clear idea of the flow of program.

Additionally, another thing to observe is the use of names that clearly depict the usage of the variable or name. Each variable and function name clearly specifies what it is being used for. The additional comment lines in between add clarity on what each segment is doing and the purpose of the statement or function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset