Science Fair Projects Ideas - Programming style

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Programming style

Programming style (also called coding standards or code convention) is a term that describes conventions for writing source code in a certain programming language.

Programming style is often dependent on the actual choice of programming language one is writing in. C style will vary from BASIC style, and so on.

Contents

Characteristics of style

Good style, being a subjective matter, is difficult to concretely categorize; however, there are a number of general characteristics. With the advent of software that formats source code automatically, the focus on how source code looks should yield to a greater focus on naming, logic, and higher techniques. As a practical point, using a computer to format source code saves time, and is possible to then enforce company-wide standards without religious debates.

Appropriate variable names

Appropriate choices for variable names is seen as the keystone for good style. Poorly-named variables make code harder to read and understand.

For example, consider the following pseudocode snippet:

get a b c 
if a < 12 and b < 60 and c < 60
  return true
else
  return false

Because of the choice of variable names, the function of the code is difficult to work out. However, if the variable names are made more descriptive:

get hours minutes seconds 
if hours < 12 and minutes < 60 and seconds < 60
  return true
else
  return false

the code's intent is easier to discern, namely, "Given a 24-hour time, true will be returned if it is in the morning and false otherwise."

Indent style

Indent style, in programming languages that use braces or indenting to delimit logical blocks of code, such as C, is also a key to good style. Using a logical and consistent indent style makes one's code more readable. Compare:

if (hours < 12 && minutes < 60 && seconds < 60)
{
   return true;
}
else
{
   return false;
}

with something like

if(hours<12&&minutes<60&&seconds<60){return true;}
else{return false;}

The first example is much easier to read because it is indented well, and logical blocks of code are grouped and displayed together more clearly.

Looping and control structures

The use of logical control structures for looping adds to good programming style as well. It helps someone reading code to understand the program's sequence of execution (in imperative programming languages). For example, in pseudocode:

 count = 0
 while count < 5
   print count * 2
   count = count + 1
 endwhile

The above snippet obeys the two aforementioned style guidelines, but however the following using the "for" construct makes the code much easier to read:

 for count = 0, count < 5, count=count+1
   print count * 2

In many languages, the often used "for each element in a range" pattern can be shortened to:

 for count = 0 to 5
   print count * 2

Spacing

Free-format languages often completely ignore whitespace. Making good use of spacing in one's layout is therefore considered good programming style.

Compare the following examples of C code.

 int count;for(count=0;count<10;count++){printf("%d",count*count+count);}

with

 int count;
 for (count = 0; count < 10; count++)
 {
    printf("%d", count * count + count);
 }

Python forces the use of indentation to mark control structures. By doing this, the need for bracketing with curly braces ({ and }) is eliminated, and readability is improved while not interfering with common coding styles. However, some programmers do not like being forced to use a style they didn't choose.

Examples of coding conventions

See also

10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice