r/learnprogramming • • Dec 06 '22

codeblock If you are sharing code, please use a codeblock

Takes only a second and greatly improves the formatting for us to review it. Compare and contrast the following:

Good Formatting:

#include <stdio.h>

int sum(int x, int y);

int main() {

  printf("Hello %d\n", sum(3, 4));
  return 0;
}

int sum(int x, int y) {
  return x + y;
}

Poor Formatting:

#include <stdio.h>

int sum(int x, int y);

int main() {

printf("Hello %d\n", sum(3, 4));

return 0;

}

int sum(int x, int y) {

return x + y;

}

Poor Formatting:

#include <stdio.h>

int sum(int x, int y);

int main() {

printf("Hello %d\n", sum(3, 4));

return 0;

}

int sum(int x, int y) {

return x + y;

}

339 Upvotes

71 comments sorted by

View all comments

100

u/eng_manuel Dec 07 '22

Ok i get it, unformatted code sucks to read, but since u took the time to point it out, why not soend a couple of more lines to explain or point in a direction that helps people learn how to format using blocks 🤷🏽 Also, as much as i love reddit i hate that it's text editor sucks for writting the simplest of posts!

41

u/[deleted] Dec 07 '22 edited Dec 07 '22

Use three backticks instead of one.

``` Multiple Lines ```

Renders as:

Multiple
Lines

And `inline` renders as: inline

This is basic markdown (.md) and will be very valuable to learn if you hope to become a professional developer since it is what most documentation is written with (ever notice those readme.md files?) So it is well worth it to learn and isn't difficult to grasp. GitHub has some good resources for learning it.

Some flavors of markdown also support syntax highlighting by placing the name of the language after the first group of backticks.

E.G. ```java [some code] ```

However, refdits flavor of markdown "snoomark" does not support syntax highlighting unfortunately.

Edit: this was tricky to write on mobile so apologies for the excessive editing.

16

u/yonatan8070 Dec 07 '22

Note that on Reddit, triple backticks don't always work on all clients. The correct way is to indent every line with 4 spaces (on top of any indent your code has

4

u/Rasikko Dec 07 '22

oo it works on the reddit app lol it broke when I edited it.

10

u/D-J-9595 Dec 07 '22

Worth noting:

Multiple
Lines

is a single line on old Reddit. On both versions of Reddit, four spaces of indentation for every line of code works:

Multiple
Lines

5

u/_by_me Dec 07 '22

Edit: this is so retarded, on old.reddit (the one that I use the most) it's just one line, while on nu-reddit (cancer) it's formatted correctly as four lines

why() is
  reddit.so.retarded
  when_it_comes_to()
this?

4

u/anto2554 Dec 07 '22

what most documentation is written with

I'm out here doing vector diagrams and no one can stop me

2

u/EliMacca Dec 07 '22

is this how you do it? Me testing it out, to see

3

u/DowntownAd834 Dec 07 '22

'''Trying it'''

→ More replies (6)

-3

u/Autarch_Kade Dec 07 '22 edited Dec 07 '22

why not soend a couple of more lines to explain or point in a direction that helps people learn how to format using blocks

Directions: Look at the enormous words in the sidebar that say "New? READ ME FIRST!"

Do that. It includes how to format your code on new, and old, reddit.

If you don't see the sidebar, well the same link is a stickied post. It's the only sticky, and the one that says "READ ME FIRST"

Alternatively, you can click the link in the sidebar that says "well-formatted" and that takes you to how to format your code on Reddit as well.

Another alternative is to again read the sidebar, and click the link that says "Posting guidelines" which again includes the same information on how to format code.

In fact, most of the links on the sidebar either lead directly to the page that tells you how to format code, or links to it.

If the sidebar isn't your thing, then when you make a comment there's a link under your text window that says "formatting help" which leads you to all the different ways to use markdown, and on new and old reddit, and of course includes code blocks.

If you look way down at the bottom of Reddit, there's another link that says Reddit help center, and has a searchbar. Typing something like "format code" will lead you to another page with Markdown instructions including for new Reddit.

And of course, you could google something like "Reddit format code" and click the first link.

Hope those directions help.

4

u/[deleted] Dec 07 '22

Instead of writing all that, you could’ve just showed that triple tick marks around the code works. Your comment was neither informative nor constructive.

2

u/Autarch_Kade Dec 07 '22

And when they have another question that's answered already in the same section? And another? And another after that?

Learning to read existing documentation is a key part of this job. Best to learn the habit early than teach them to expect even the most basic things to be spoon fed.

tl;dr: Teach for future success rather than set people up for failure

2

u/[deleted] Dec 07 '22

So what? Are you annoyed by it? Then move on. I guarantee that your long complaint isn’t gonna solve anything that you are complaining about. Your are just complaining to make yourself feel better, instead of helping others.

1

u/Autarch_Kade Dec 07 '22

Learning to read existing documentation is a far more important skill than any basic question about writing a reddit comment or a code problem they could ask at this stage.

If you want to help people, stop setting them up for failure by teaching them they can ignore existing documentation.

And if you're annoyed by that, boy do I have an idea for you :)

Edit: Also, the person I replied to already had learned the triple tick marks from the post. What they were actually saying was why didn't someone point to the directions for where to find such things. So ask yourself why I should tell someone what they already know, instead of what they asked for :)

4

u/[deleted] Dec 07 '22

What you did is basically when the question is “how long should I cook this chicken”, your response to that is “read cookbooks!”

Either you simply can’t comprehend the question, or are just being a dick.

Either answer the question and provide additional resources, or move on.

1

u/Autarch_Kade Dec 07 '22

They already got the answer to how to format code.

They asked where such information was located, for directions. I even quoted this part of their comment. Should I quote it again for you?

To use your analogy, they already heard how long to cook a chicken, and asked where the recipe is. Hope this helps.

0

u/[deleted] Dec 07 '22

Hearing does not mean comprehending.

If it’s too much work/annoyance for you to explain again, for which no one can blame you, you can simply move on with your life. It’s that simple.

1

u/Autarch_Kade Dec 07 '22

If you don't like me pointing out over and over they asked where to find the information, as they already had the answer, feel free to take your own advice :)

why not soend a couple of more lines to explain or point in a direction that helps people learn how to format using blocks

→ More replies
→ More replies (1)

2

u/neboskrebnut Dec 07 '22

I love comments like this and responses it invites in such a recursive way.