Programming Fundamentals/Programming Style
Overview
editProgramming style is a set of rules or guidelines used when writing the source code for a computer program. Following a particular programming style will help programmers read and understand source code conforming to the style, and help to avoid introducing errors.[2] Each language has its own set of rules. It is important to follow the correct guidelines to make sure the code functions properly. For example, Javascript doesn't necessarily need an indent inside of a function. It is good practice to indent so it is visible to the user. In python, code must be indented for the program to run.
Discussion
editWithin the programming industry there is a desire to make software programs easy to maintain. The desire centers on money. Simply put, it costs less money to maintain a well written program. One important aspect of program maintenance is making source code listings clear and as easy to read as possible. To that end we will consider the following:
- Documentation
- Vertical Alignment
- Comments
- Indentation
- Meaningful Identifier Names Consistently Typed
- Appropriate use of Typedef
The above items are not needed in order for the source code to compile. Technically the compiler does not read the source code the way humans read the source code. But that is exactly the point; the desire is to make the source code easier for humans to read. You should not be confused between what is possible (technically will run) and what is okay (acceptable good programming practice that leads to readable code).
For each of these items, check style guides for your selected programming language to determine standards and best practices. The following are general guidelines to consider.
Documentation
editDocumentation is usually placed at the top of the program using several comment lines. The amount of information would vary based on the requirements or standards of the company who is paying its employees or independent contractors to write the code.
Vertical Alignment
editYou see this within the documentation area. All of the items are aligned up within the same column. This vertical alignment occurs again when variables are defined. When declaring variables or constants many textbooks put several items on one line; like this:
float length, width, height;
However common this is in textbooks, it would generally not be acceptable to standards used in most companies. You should declare each item on its own line; like this:
float length; float width; float height;
This method of using one item per line is more readable by humans. It is quicker to find an identifier name because you can read the list vertically faster than searching horizontally. Some programmers list them in alphabetic order.
The lines of code inside functions are also aligned vertically and typically indented two or four spaces from the left. The indentation helps set the block off visually.
Comments
editExperts have varying viewpoints on whether, and when, comments are appropriate in source code. Some assert that source code should be written with few comments, on the basis that the source code should be self-explanatory or self-documenting. Others suggest code should be extensively commented, with over 50% of the non-whitespace characters in source code being contained within comments).[3]
In between these views is the assertion that comments are neither beneficial nor harmful by themselves, and what matters is that they are correct and kept in sync with the source code, and omitted if they are superfluous, excessive, difficult to maintain or otherwise unhelpful.[4]
In addition comments are not supposed to be to long. You can change the length by having the comment in several lines or create multi line comments by adding adding a delimiter.
All comments should be specific about what a certain part of a program is supposed to do. It is important that you comment is understandable and simple to anyone who reads to make it easier for yourself or others.
Indentation
editFor languages that use curly braces, there are two common indentation styles:
function(parameters) { // code }
function(parameters) { // code }
In either case, it is important to maintain vertical alignment between the start of the code block and the closing curly brace.
The number of spaces used for indenting blocks of code is typically two or four spaces. Care should be taken to ensure that the IDE or code editor inserts spaces rather than tab characters for indents.
Meaningful Identifier Names Consistently Typed
editAs the name implies “identifier names” should clearly identify who (or what) you are talking about. Calling your spouse “Snooky” may be meaningful only to you. It’s why referring to your spouse as “Snooky” to other people like interviewers at a potential place of employment could lead to confusion, questions and even the false rumor you’re in love with Nicki Minaj. If you don't say a name that has meaning to people, everyone will come up with their own idea of what the name refers too whenever that idea is true or not. This is why most people might need to see her full name (Jane Mary Smith) to appropriately identify who you are talking about. The same concept in programming is true. Variables, constants, functions, and other identifiers should use meaningful names. Additionally, those names should be typed consistently in terms of upper and lower case as they are used in the program. Don’t define a variable as: Pig and then type it later on in your program as: pig.
A good rule of thumb for identifiers in procedural programs (as opposed to object-oriented programs) is to use verb-noun combinations for function identifiers and use noun or adjective-noun combinations for constant and variable identifiers. If a function name requires two verbs or two nouns to fully describe the function, it should probably be split into separate functions.
Key Terms
edit- braces
- Used to identify a block of code in languages such as C++, C#, Java, and JavaScript.
- consistent
- A rule that says to type identifier names in upper and lower case consistently throughout your source code.
- comments
- Information inserted into a source code file for documentation of the program.
- documentation
- A method of preserving information useful to others in understanding an information system or part thereof.
- indention
- A method used to make sections of source code more visible.
- meaningful
- A rule that says identifier names must be easily understood by another reading the source code.
- vertical alignment
- A method of listing items vertically so that they are easier to read quickly.