CS253: Software Development with C++

Spring 2023

Coding Style

Show Lecture.CodingStyle as a slide show.

CS253 Coding Style

:

Communication in English

Communication in Programming

Style Conventions

Names

Names

variables, methods, functions
start with az, continue with az, AZ, 09 or _: foobar, foo_bar, fooBar
user-defined classes
start with AZ, continue with az, AZ, 09 or _: Foobar, FooBar
constants via constexpr or #define
start with AZ, continue with AZ, 09 or _: FOOBAR, FOO_BAR

Beginners love to make their variables ALL_UPPER_CASE, because it looks more computery. This confuses experienced coders.

Names

Spacing

if (3 < 4) {
    count--;
    cout << "Hooray!\n";
}
else {
    cout << "Math is broken\n";
}

Spacing

int x = 2+2;
int x, y, z;
foo(x, y+z)
hypot = sqrt(x*x + y*y);

Braces

if (3 < 4) {
    count--;
    cout << "Hooray!\n";
}
if (5>6)
    cerr << "oops!\n";
for (int i=0; i<10; i++) {
    cout << "i=" << i << "\n";
    do_other_thing();
}

Comments

// The destroy function takes a size and eliminates
// all widgets of that size.  The hint argument will be
// implemented someday.
void destroy(int size, string /* hint */) {
    if (size <= 0)  // nothing can be that size
        return;
    alpha(size);            // explain why
    beta(size);             // we are calling
    gamma(size);            // all these functions!
}

Indentation

Tabs

Now, not Later