How to format Salesforce Codes in the best way possible

The first thing they are presented with is how the code was written, which should provide subtle clues as to the original intent, allowing the reader to easily read and understand code that is foreign to them, especially if they have no idea what exactly a piece of code does. It is a rare occurrence to have complete and up-to-date documentation.

The Best Way to Make Your Code “Prettier”

You can be sure that Prettier will keep your code looking nice and readable, regardless of whether you’re writing Lightning Web Components or Apex backends. Use VS Code, the Prettier Extension can be found on the extension’s marketplace, and we can install Prettier very easily.
The first step towards formatting Apex with Prettier is to install an extension, which is easy, just follow the command below (make sure you have node installed).

1) npm install --save-dev --save-exact prettier prettier-plugin-apex

It rewrites your code in a way that it thinks is best after stripping all styling from it. The code can even be saved automatically every time, which is great as it makes writing our code feel more natural.

In order to conform to a style standard, Prettier does the legwork. What does it consider “Good”? Let’s find out.

Prettier helps keep a consistent style in several ways:

⇰ Adds semicolons;
⇰ Commas at the end of sentences;
⇰ Using quotations consistently;
⇰ Maintain consistent indentation spacing (usually four or two spaces/tabs);
⇰ Function/method declarations and parameters should be spaced consistently;
⇰ Ensure that bracketed items are spaced consistently.

Even though Prettier is great, computers are dumb and need explicit instructions, while our code is full of context and implicit information. While Prettier does a wonderful job, we can help take it to the next level by tweaking our own coding style just a tiny bit.

 

 

To receive the latest news and posts directly in your inbox, subscribe to Fidizzi

Subscribe

* indicates required
cropped-medium-3