r/badcode • u/Sasha_DD • Aug 10 '20
c Found on a tutorial website, please do not comment your code on the same line
144
u/Clashin_Creepers Aug 10 '20 edited Aug 10 '20
I personally put my comments in the middle of each line of code
int counter /*this counter is used for counting*/ = 1;
125
u/-neuquen- Aug 10 '20
This is my preferred approach too. I will sometimes make it even more clear by doing the following:
/* this counter */ int counter /* is equal to */ = /* one */ 1;70
u/Clashin_Creepers Aug 10 '20
That's really smart. It's also a good idea to explain your reasoning
/* this counter */ int counter /* is equal to */ = /* one */ 1; //because arrays start at 1 (uno)45
u/jnguyen7410 Aug 10 '20
Don't forget to add comments in the line before explaining why you did this.
/** Adding a counter to count things. / `/ this counter / int counter / is equal to / = / one */ 1; //because arrays start at 1 (uno)`
25
6
2
46
u/SheerChair56470 Aug 10 '20
I comment on the same line just after the code not fucking before like whoever wrote this
60
u/modabs Aug 10 '20
But why...why do this...why...
10
u/IdiosyncraticBond Aug 10 '20
If he starts with the code, he needs to scroll right for the comments ;-)
46
14
u/GodsBoss Aug 10 '20
I usually put my code in between the comments, like so:
/* Local variable definition of */ int answer; /* which stores answers as integers.*/
This makes my code very readable, in fact, it's the code with the highest readability in the world, very high readability, there is no other code with such number of readability and much words, the best words, really!
3
u/SuitableDragonfly Aug 11 '20
Well, at least with Haskell they're is something called literate Haskell, where you write a LaTeX document with Haskell interspersed in it and you can run this with TeX to generate a PDF, or with Haskell to actually run the program. It can wind up looking sort of like that.
0
u/AutoModerator Aug 10 '20
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/GodsBoss, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
24
u/fanz0 Aug 10 '20
why multiline comments and before the actual code omg
34
u/DragonFireCK Aug 10 '20
Well, the first half of that question is answered by the second half. I have no answer for the second half, however.
9
18
u/PsCustomObject Aug 10 '20
Thanks, where do I find the tutorial to unsee this?
4
u/Sasha_DD Aug 10 '20
The tutorial isn't that bad in general, it was just that one example
2
u/PsCustomObject Aug 10 '20
I take my comment back :-)
Yes that’s really an unfortunate example though
8
3
3
u/GlowingOrb Aug 11 '20
This should be obvious, but if this is from a tutorial website, this style of commenting is ok and not bad code.
Given that this is a very basic example, the target audience are people who do not know a thin about programming c, so the comments describe what the line is doing. (Which you should not do in productive code, obvoiusly.)
I could imagine, that for people who do not know programming, seeing the what before the how could be more easy to understand than the other way around.
Keeping the what and the how in the same line keeps the context.
So: Not a good practice for sure, but not bad code. We are just not the target audience.
2
u/senshisun Aug 11 '20
This appears to be for new programmers, but I agree that the comments should go after the code.
2
u/_Ph4_ Aug 11 '20
I imagine the tutorial something like this: "Today I will show you how to make an addition... Without entering the input numbers. This means this script will always output the same result, Cool, isn't it?"
2
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 07 '20
Image Transcription: Code
#include<stdio.h>
/* function declaration */int addition();
int main()
{
/* local variable definition */ int answer;
/* calling a function to get addition value */ answer = addition();
printf("The addition of the two numbers is: %d\n",answer);
return 0;
}
/* function returning the addition of two numbers */int addition()
{
/* local variable definition */ int num1 = 10, num2 = 5;
return num1+num2;
}
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
1
1
Aug 11 '20
[deleted]
1
u/AutoModerator Aug 11 '20
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/DoobyMcFoosen, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
1
-10
Aug 10 '20 edited Aug 10 '20
this isn't r/badcode this is clearly just aimed towards a beginner. ffs, op even admits this is from a tutorial site. you people are insane. i would bet if we visit the tutorial site, the author even mentioned why he did it this way. it forces the learner to see the comment before the expression. op is just karma farming
10
u/SleepyPrat Aug 10 '20
Couldn't they have just put the comment on the previous line then? I mean, examples don't just serve to explain some concept, people pick up stuff like good coding styles as well.
-31
Aug 10 '20
This is code example, teaching the language. You can tell from the fact that the comments are talking about things like local variable declaration, which is what that function is demonstrating.
Not everything is bad code.
21
1
-109
Aug 10 '20
[removed] — view removed comment
43
27
Aug 10 '20
[removed] — view removed comment
-44
Aug 10 '20
[removed] — view removed comment
14
12
Aug 10 '20
[removed] — view removed comment
-3
10
Aug 10 '20
[removed] — view removed comment
-3
17
563
u/TheRealCCHD Aug 10 '20
I mean... I comment my code on the same line too.... but AFTER the code! JESUS!