Be Stylish
Explore how to write code in a clear and maintainable style by using meaningful names, consistent indentation, and effective comments. Understand conventions for exits and exceptions, and learn to adapt your style to project or language standards. This lesson helps you improve code readability and maintainability, making programming more efficient and reducing errors.
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 ...