Every programming language has its own set of stylistic conventions that are meant to promote readability. It is a very important aspect of software development that is heavily stressed by many companies, including: Google, Amazon, Microsoft, Mozilla, Apache, Chromium, TensorFlow, React Native, HyperLedger, Visual Studio Code to just list a few. Your future employers value good style and hence, the CSE department is committed to help you develop the skills required for a successful career
While everyone agrees that improving readability of a program is desirable, there are different ideas on how to achieve that. For example, Acme Software Inc. may insist that their C++ programmers prefix all data members with "m_" (e.g., m_examScore, m_name), while another company may not have this requirement. An Internet search on programming conventions will yield dozens of stylistic conventions, for dozens of different languages. Some of these documents will approach being one hundred pages long!
This document is not meant to be yet another all-encompassing document on coding conventions. Instead, it provides references to widely used style guidelines for different programming languages. In addition, it summarizes a small set of key ideas from them with the objective of guiding beginning and intermediate level programmers in some of the fundamental aspects of programming style. Although, there is not a concrete set of rules, you can generally rely on the following rules:
For those interested in the motivation and rationale behind many of these style guidelines, please refer to "The Art of Readable Code", by Dustin Boswell and Trevor Foucher (ISBN: 978-0596802295). All Miami students, faculty, and staff have free access to the electronic version of this textbook via the: Safari on-campus link for "The Art of Readable Code" or Safari off-campus link for "The Art of Readable Code"
Here are a few existing coding conventions for some of the common programming languages that the CSE department uses:
Most IDEs will typically allow you to configure your style preferences and automatically format your code to fit these guidelines. Ensure you check your IDE's settings to ensure it is consistent with CSE department’s guidelines.
Your instructor might have additional or modified versions of these requirements. When in doubt, check with your instructor.
Indentation and whitespace should be used to improve the readability of your programs. This is a principle that holds in all programming languages. Indentation should be used to emphasize the nesting of control structures, while adhering to the following expectations:
int area=Math.PI*Math.pow(r,2);
for(int i=0;i<10;i++)
int area = Math.PI * Math.pow(r, 2);
for (int i = 0; (i < 10); i++)
Note: Most IDEs will alow you to configure your style preferences and the IDE will automatically format your code, making it very convenient. Ensure you check out your IDE options before manually reformatting your source code.
The bodies of conditionals and loops must be surrounded by braces, {}
. The following examples recommended bracing style based on programming language(s):
if (a == 0) {
System.out.println("zero");
} else {
System.out.println("non zero");
}
C#if (a == 0)
{
Console.WriteLine("zero");
}
else
{
Console.WriteLine("non zero");
}
Note: even if the body of the if-else constructs has only a single statement, the braces must be used. Additionally, do not adopt a style that deemphasizes, or hides, the bracing:
// Terrible style. Never write code as shown below:
if (a == 0) {
System.out.println("zero"); }
else {
System.out.println("non zero"); }
Regardless of the language, every file should have the following information. When possible, you should use any conventions that allow the automatic generation of documentation (e.g., C++, Java and C#):
Each function or method will typically have the following information (see examples in source code further below):
There are three common conventions for naming programmatic entities. These are Pascal case, camel case, and uppercase. Pascal case capitalizes the initial letter of each word. Camel case is similar, but does not capitalize the first word. Uppercase uses all uppercase characters, but separates the words with an underscore. Here are some examples:
I
")
501 E. High Street
Oxford, OH 45056
1601 University Blvd.
Hamilton, OH 45011
4200 N. University Blvd.
Middletown, OH 45042
7847 VOA Park Dr.
(Corner of VOA Park Dr. and Cox Rd.)
West Chester, OH 45069
Chateau de Differdange
1, Impasse du Chateau, L-4524 Differdange
Grand Duchy of Luxembourg
217-222 MacMillan Hall
501 E. Spring St.
Oxford, OH 45056, USA