Skip to main content

Source Code Formatting

· 4 min read

What is source code formatting

Source code formatting refers to the process of organizing and structuring the code in a consistent and readable manner. It involves adding whitespace, indentation, and line breaks to make the code more visually appealing and easier to understand.

Proper source code formatting can improve the readability and maintainability of the code, as well as make it easier to debug and modify.

Different programming languages and development environments may have different conventions and best practices for source code formatting.

There are several tools available that can automate source code formatting, such as code editors, integrated development environments (IDEs), and code formatter plugins.

These tools can apply formatting rules automatically or allow developers to customize formatting settings to match their preferences.

Example: Poorly formatted C Code

Below is an example of poorly fomatted source code:

#include<stdio.h>
int main(){
int i,j,k;
for(i=1;i<=5;i++){
for(j=1;j<=5-i;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("* ");
printf("\n");
}
return 0;
}

As you can see, the code lacks proper indentation, making it harder to read and understand the structure of the loops.

Additionally, there is no spacing between the function arguments, making it harder to see where one argument ends and the next begins.

Example: Well formatted C Code

Below is the same code formatted in a more readable manner:

#include <stdio.h>

int main() {
int i, j, k;

for (i = 1; i <= 5; i++) {
for (j = 1; j <= 5 - i; j++) {
printf(" ");
}

for (k = 1; k <= i; k++) {
printf("* ");
}

printf("\n");
}

return 0;
}

Here, the code is properly indented and uses consistent spacing to make it more readable.

The code is also broken up into smaller, more manageable chunks, making it easier to understand how the loops and print statements are related.

Advantages of source code formatting

Following are the advantages of source code formatting:

Improved readability

Proper source code formatting can make code easier to read and understand, which can save time and effort during development and maintenance.

Consistency

Consistent source code formatting can help ensure that all code written by a team follows the same conventions, making it easier to collaborate on projects.

Maintainability

Proper source code formatting can make code easier to modify and update, which can help reduce errors and make the code more flexible and adaptable.

Debugging

Well-formatted code can make it easier to identify and fix bugs, as it can help pinpoint the location of issues more quickly.

Compliance

Proper source code formatting can help ensure compliance with coding standards and best practices, which can be important for certain industries or regulatory requirements.

Efficiency

Source code formatting tools and automation can help save time and effort by automating formatting tasks, which can allow developers to focus on other aspects of software development.

Professionalism

Proper source code formatting can make code look more professional and polished, which can help build trust and confidence in the software product.

Common code formatting tools

Below table shows some of the most common code formatting tools and the programming languages they support:

ToolSupported Languages
PrettierJavaScript, TypeScript, CSS, HTML, JSON, Markdown, GraphQL, YAML
BlackPython
Clang-FormatC, C++, Objective-C, ProtoBuf
RuboCopRuby
SQL FormatterSQL
ESLintJavaScript, TypeScript
GofmtGo
SwiftFormatSwift
RustfmtRust
ScalafmtScala
TerraformHashiCorp Configuration Language (HCL)
note

Please note that some of these tools may support additional languages beyond what is listed here. Additionally, there may be other code formatting tools available that support different programming languages.