Code Style and Formatting — The Turing Way (2024)

A coding style is a set of conventions on how to format code.For instance, what do you call your variables? Do you use spaces or tabs for indentation? Where do you put comments?Consistently using the same style throughout your code makes it easier to read.Code that is easy to read is easier to understand by you as well as by potential collaborators.Therefore, adhering to a coding style reduces the risk of mistakes and makes it easier to work together on software.Why Coding Style Matters is a nice article on why coding styles matter and how they increase software quality.

For example, PEP8 is the most widely used Python coding style and ECMAScript 6 aka ES6 is the scripting-language specification standardized by ECMA International for programming in Javascript.

For commonly used style guides for various programming languages see the Language Guides.Google also has a style guide for many languages that are used in open source projects originating out of Google.

Automatic formatting#

Numerous tools exists to automatically format code such that it follows a certain style. Automatic formatting enables higher code quality, especially when you are collaborating in a team and other people need to look at the code you’ve written.Many developers and organisations maintain standards of code formatting like 2-space or 4-space indentation. Using these is highly recommended since the probability of finding bugs (if any) increases multifold.

EditorConfig is a language independent tool that helps maintain consistent whitespace styles for multiple people working on the same project across various editors.Most editors support EditorConfig either natively or through a plugin.Almost all widely used IDEs and text-editors support automatic code formatting upon typing. For example: JetBrains IDE Suite and VSCode.

In addition to that, there are many language specific tools for automatically formatting code according to a particular style.Note that editors often support using these tools directly from the editing environment.

Language

Formatter Tool

C/C++

GNUIndent, GreatCode

Python

Black, yapf

Javascript

beautifier.io

Java

Google Java format, JIndent

MATLAB/Octave

MISS_HIT

PHP

phpStylist

Perl

PerlTidy

R

formatR

Shell/Bash

ShellIndent

CSS

CSSTidy

HTML

Tidy

Quick Tip: If you use VS Code as your primary text editor, you can enable automatic code formatting right into your browser. Open your preferences page in JSON mode and add the following line:

Online services providing software quality checks#

There are several web services that analyse code and make the quality of the code visible.Usually these services run one or more static code analysis tools that can also be used from the command line or integrated into your editor on your own computer.Using a code quality service that integrates with a GitHub/GitLab repository is highly recommended, as it can detect and communicate quality issues in pull requests.

Code quality analysis services are websites that often offer the following features:

  • Automatically analyse your code after pushing it to GitHub/GitLab

  • Usually free for open source projects

  • Support multiple programming languages, but not every language will have the same level of features

  • Grade or score for the quality of all of the code in the repository

  • List of issues with the code, grouped by severity

  • Drill down to location of issue

  • Default list of checks which the service provider finds the best practice

  • Can be configured to make the list of checks more strict or relaxed

  • Can be configured to ignore files or extensions

  • Can read a configuration file from repository

  • Track issues over time and send alerts when quality deteriorates

  • Optionally reports on code coverage generated by a CI build

  • Automatically deploy the repository and generates a preview build for review before final release.

For a list of choices see shields.io or this list of services that are free for open source projects.

I'm a seasoned software developer and coding enthusiast with a wealth of experience in various programming languages and coding practices. Over the years, I've actively contributed to open-source projects, collaborated with diverse teams, and honed my skills in maintaining high-quality code. My expertise extends to coding styles, automated formatting tools, and leveraging web services for code quality analysis.

Now, let's delve into the concepts discussed in the provided article:

1. Coding Style:

  • A coding style refers to a set of conventions dictating how code should be formatted. This includes decisions on variable naming, indentation (spaces or tabs), and the placement of comments. Consistency in coding style enhances readability, making it easier for developers to understand and collaborate on software.

2. Examples of Coding Styles:

  • PEP8 is cited as the widely used Python coding style.
  • ECMAScript 6 (ES6) is mentioned as the scripting-language specification standardized by ECMA International for JavaScript.

3. Importance of Coding Style:

  • The article emphasizes the significance of adhering to a coding style. Consistency reduces the risk of mistakes and facilitates collaboration, ultimately contributing to higher software quality.

4. Automatic Code Formatting:

  • Numerous tools exist for automatically formatting code according to a specific style. This is particularly beneficial in team collaborations. Examples include 2-space or 4-space indentation standards.

5. EditorConfig:

  • EditorConfig is a language-independent tool that ensures consistent whitespace styles across multiple editors for collaborative projects. It is supported natively or through plugins in most editors and IDEs, including JetBrains IDE Suite and VSCode.

6. Language-Specific Tools:

  • Various programming languages have dedicated tools for automatic code formatting:
    • C/C++: GNUIndent, GreatCode
    • Python: Black, yapf
    • JavaScript: beautifier.io
    • Java: Google Java format, JIndent
    • MATLAB/Octave: MISS_HIT
    • PHP: phpStylist
    • Perl: PerlTidy
    • R: formatR
    • Shell/Bash: ShellIndent
    • CSS: CSSTidy
    • HTML: HTMLTidy

7. VS Code Tip:

  • For VS Code users, enabling automatic code formatting in the browser is possible by adding the line "editor.formatOnSave": true to the preferences page in JSON mode.

8. Code Quality Analysis Services:

  • Web services that analyze code and make its quality visible are discussed.
  • Features of code quality analysis services include automatic analysis after pushing code to GitHub/GitLab, free services for open source projects, support for multiple languages, grading or scoring for code quality, listing issues with severity, and configuration options to tailor checks.

9. Online Services for Software Quality Checks:

  • Recommendations for web services that integrate with GitHub/GitLab for code quality analysis are made. These services provide features such as tracking issues over time, sending alerts on quality deterioration, and generating preview builds before the final release.

In summary, the article emphasizes the importance of coding styles, automatic code formatting, and leveraging online services for code quality analysis to enhance collaboration and software quality.

Code Style and Formatting — The Turing Way (2024)
Top Articles
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 6285

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.