Code Blocks: Why and How to Use Them

While using code in your content is smart, formatting it can be a bit tricky. In this article, we walk you through creating code snippets and blocks.

Sometimes just talking about your code isn’t going to cut it; you need to show, rather than tell.

Because DZone’s readers prefer highly technical and detailed content, even down to the level of code itself, we’ve made it easy to include code in your articles.

Let’s go over how to add a code snippet and a code block to your post.

How to Create a Code Snippet

Perhaps you want to insert a docker command into your article. You decide to write:

“As an example, you’d run:

docker pull nginx”

Shoot! You’d like your command to be formatted differently than the rest of your text so it stands out.

To format this single line of code, click on the “</>” icon in the menu bar of the article editor.

A dropdown menu will appear. Hit “Code Snippet,” and your code format will change to this:

Congrats! You just formatted your code snippet!

How to Create a Code Block

To format code longer than a single line, we recommend creating a code block rather than a snippet, as this allows for easy formatting and syntax recognition. You write the following:

“You’ll now see that the working directory on the host contains the files privateKey.key and CSR.csr. If you want to use them to create a self-signed certificate, you’d run something like:

docker run –v $PWD:/work –it nginx openssl req –x509 –sha256 –nodes

-days 365 –newkey rsa:2048 –keyout privateKey.key –out

/work/certificate.crt”

Now you want to make the code easier to read. First, copy your code. Select the space above or below it, then go back up to that “</>” button. Pick “Code Block,” and a pop-up will appear. In the pop-up, paste your code:

In the field above the code, select the language (Shell, in this case). We have more languages than we care to count in our code block selector — you can just click in the field and start typing until you find what you want.

Hit the green “Save” box at the bottom, and you’ll end up with this:

Now you can delete the old text and leave the code block! To edit the code, just click on the block, and it will pop back up.

Well done! You’ve now created a well-formatted, easy-to-read code block!

Code Beautifiers

Want to make your code even prettier? There are plenty of online tools to help you do that. One of our favorite code beautifiers for Java is the Online Java Formatter. A great one to use for C# is C# Formatter Online. Search online for your language of choice, and you’re sure to find a tool for formatting your code.

Why Should I Use Code Blocks?

The DZone audience responds well to highly detailed and specific content. There’s no need to shy away from the “nitty gritty” of tech and software; in fact, we encourage you to embrace it! By including specific code references in your content, you’ll be establishing yourself as an expert with real-world experience — something our readers truly value.

As always, we’re here to help if you experience “technical difficulties” in editing your articles. Let us know if you’re having issues formatting your code, and we’ll do our best to assist you.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.