Over 10 years we help companies reach their financial and branding goals. Engitech is a values-driven technology agency dedicated.

Gallery

Contacts

411 University St, Seattle, USA

engitech@oceanthemes.net

+1 -800-456-478-23

.net development Quick Tips

php-four

It’s evident that during code reviews, there’s often a need to remind team members to adhere to established best practices within our project, team, or company. This process recurs with each new addition to the team. However, we can significantly streamline these efforts by implementing editor configurations, particularly within Visual Studio. These configurations serve as a proactive solution, ensuring that fundamental coding standards are consistently applied without the need for manual intervention or repetitive reminders.

In the fast-paced world of software development, efficiency is key. As developers, we constantly strive to optimize our workflows, minimize distractions, and maximize productivity. A significant aspect of this optimization lies in customizing our coding environment to suit our individual preferences and coding styles. Fortunately, Visual Studio, Microsoft’s flagship integrated development environment (IDE), offers a powerful tool for achieving this: EditorConfig.

We’ll explore EditorConfig in Visual Studio in depth, covering everything from its basic concepts to advanced usage scenarios. By the end of this journey, you’ll have a thorough understanding of how to leverage EditorConfig to streamline your coding environment and supercharge your development workflow.

What is EditorConfig?

EditorConfig is an open-standard file format that allows developers to define and maintain consistent coding styles across different editors and IDEs. By creating a simple .editorconfig file in your project’s root directory, you can specify a set of rules and preferences for code formatting, indentation, tab spacing, and more. These rules are then applied by compatible editors and IDEs, ensuring uniformity in coding style across your entire development team.

Getting Started with EditorConfig in Visual Studio

To begin using EditorConfig in Visual Studio, follow these simple steps:

Create an EditorConfig File: Start by creating a new file named .editorconfig in the root directory of your project.

Establishing Rules and Preferences:

There are two methods to achieve this objective: either by modifying the editorConfig file within any text editor,

OR

by utilizing the functionality of the Visual Studio IDE, which offers a user-friendly interface for adjusting and implementing these settings.

We will jump on first how to set preferences using vs GUI.

To open the UI for the editors config you just have to double click on the file added to your solution, You will see a UI windows opens up like below.

As we can see in the above screenshot there are four tabs of preferences.

  1. Whitespace
  2. Coding Style
  3. Naming Style
  4. Analyzers
File hierarchy and precedence

When you add an .editorconfig file to a folder in your file hierarchy, its settings apply to all applicable files at that level and lower. You can also override EditorConfig settings for a particular project, codebase, or part of a codebase, such that it uses different conventions than other parts of the codebase. Doing so can be useful when you incorporate code from somewhere else, and don’t want to change its conventions.

Follow these guidelines:

  • To override some or all of the EditorConfig settings, add an .editorconfig file at the level of the file hierarchy you want those overridden settings to apply. The new EditorConfig file settings apply to files at the same level and files in any subdirectories.

  • If you want to override some, but not all of the settings, specify just those settings in the .editorconfig file. Only those properties that you explicitly list in the lower-level .editorconfig file are overridden. Other settings from any higher-level .editorconfig files continue to apply.
  • If you want to ensure that no settings from any higher-level .editorconfig files are applied to this part of the codebase, add the root=true property to the lower-level .editorconfig file.

How you can make sure, the settings only apply to specific file format.

[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]

 indent_size = 2
Whitespace

Explore a myriad of customizable features within this tab, offering meticulous control over the presentation of your code. Tailor the appearance of white space, define the arrangement of curly braces encasing classes, and specify the formatting of array square brackets during initialization.

Delve deeper into customization options such as positioning ‘else’ statements on separate lines, or introducing space between method names and their opening braces. Unlock a realm of settings tailored to your preferences, facilitating a seamless and intuitive reading experience of your codebase.

Referencing the accompanying screenshots, visualize the array of settings discussed, empowering you to craft a coding environment aligned with your unique preferences and coding style.

 

Coding Style

Now, let’s delve into the Coding Style tab, where the evolution of Microsoft Visual Studio’s features and .NET coding styles unfolds. Despite the advancements in newer versions, your team may still adhere to older syntax conventions. This tab empowers you to initiate a transition to contemporary practices.

Consider the declaration of namespaces as an example. Traditionally, namespaces were block-scoped, whereas the newer approach embraces file-scoped declarations. Leveraging the editorconfig settings, you can mandate the adoption of file-scoped namespace declarations by categorizing deviations as “Error.”

By steering your team towards modern syntax conventions, you not only align with industry standards but also enhance code clarity and maintainability. Explore the potential of this tab to streamline your development process and foster a culture of continuous improvement.

OLD:

namespace GettingReady.Data{
  public class Class1{
  }
}

New:

namespace GettingReady.Data;

public class Class1
{
}

Consider another scenario where your team’s coding practices include an excessive number of blank lines in check-in code. Such superfluous spacing can disrupt code readability and adherence to established conventions. However, with the capability offered by the editorconfig settings, you can enforce stringent guidelines.

By designating an abundance of blank lines as errors within the editorconfig settings, you instill a discipline that promotes cleaner, more concise code. This not only enhances the overall quality of your codebase but also fosters a culture of precision and consistency within your development team.

Empower your team to optimize their coding habits and elevate the standard of code hygiene through meticulous configuration of the coding style tab. By identifying and rectifying such inefficiencies, you pave the way for smoother collaboration and more efficient code reviews.

Naming Style

This section addresses the crucial aspect of naming conventions within your development team. It is not uncommon for new team members to overlook or disregard established naming conventions, which can result in prolonged code reviews and inefficiencies in the development process.

Adherence to consistent naming conventions is paramount for ensuring code clarity, maintainability, and scalability. By establishing clear guidelines within the coding style tab, you provide a framework that fosters cohesion and standardization across your codebase.

Furthermore, consistent naming conventions facilitate easier collaboration among team members and reduce the cognitive overhead associated with understanding and maintaining code. By emphasizing the importance of adhering to these conventions, you promote a culture of professionalism and excellence within your development team.

In conclusion, it’s essential to understand the broad applicability of the editorconfig file across various coding environments. Whether you’re a seasoned developer or just starting your coding journey, leveraging the power of editorconfig can streamline your workflow and promote consistency in coding practices.

 

By embracing editorconfig in your preferred coding environment, you can foster collaboration, enhance readability, and maintain code quality across projects. Regardless of your preferred editor, editorconfig offers a universal solution for achieving coding consistency and efficiency.

The following editors are compatible with editorconfig files, enabling users to implement and benefit from standardized coding styles effortlessly:

And there you have it, folks! Wrapping up this journey through the wild world of coding style with a sprinkle of humor. Remember, while coding style might seem like a serious affair, there’s always room for a little laughter amidst the curly braces and semicolons.

As you venture forth into the vast expanse of code, armed with newfound knowledge and maybe a few chuckles, may your bugs be minimal, your coffee strong, and your code always as clean as your favorite programming meme.

Until next time, happy coding and may your error messages be as cryptic as a fortune cookie!

×