Be Stylish
Learn to write code in a good style, as it is not only read by a compiler but other programmers, too.
We'll cover the following...
Coding in good style
The following two functions do exactly the same thing:
The following program is the same as given above. Both compute the Fibonacci of a number provided as input.
Which would we rather maintain?
Maybe that example is a little extreme. But it illustrates a simple point that a compiler doesn’t just read our code. It’s read by other programmers, too. Writing code in good style is a factor in software quality because we simply can’t maintain illegible code.
Factors to style
The broad term style refers to everything the compiler doesn’t care about, but humans do. Here are some examples:
- Naming of classes, methods, variables, files, and so on
- Arrangement of functions within a file and across files
- Comments
- Braces and parentheses (where optional)
- Choice of control structures (where equivalent)
- Capitalization
- Indentation and other whitespaces
The definition of good style varies depending on the programmers we’re working with, project or corporate style guides, and conventions established by the programming language. However, there are some common themes we’ll look at here.
Why naming matters
Well-written code won’t read like a human language, but it shouldn’t read like alien hieroglyphics, either. Good naming of ...