[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

what should comments in code entail? i feel like a lot of it

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 11
Thread images: 1

File: 1459742526188.jpg (22KB, 428x260px) Image search: [Google]
1459742526188.jpg
22KB, 428x260px
what should comments in code entail? i feel like a lot of it is silly when it's just x=2 \\x has been assigned 2 hurrrrrrrrrrrr pfftthppfthfphthflthlfhltp /n
>>
>>1074346
Don't explain what, explain why.
The general rule is if you have to explain what then your assignments are to complicated and you should split them up, but if you feel like going rogue then don't worry about it, leave them complicated fuck it.
But certainly you should explain why instead of what
Don't be afraid to comment on a block instead of line by line if it makes sense.

Basically when you write software usually you come up with an algorithm, point by point what you are going to do, then you write the code based on the algorithm right? So the comment should basically just be an explanation of the algorithm, someone who doesn't understand the language (or any code!) should be able to read the comments and understand not what you are actually doing but what you are trying to achieve and how you are going about it. Think of it like explaining it to your gran or something.
>>
>>1074346
At the header and return of a function you should document exactly what variables it's getting, from where, and what it will do to them. The return specifically should re-state what it did to spit out this particular return, especially if there are different cases.

The problem with introductory programming commenting some of it really is too dumb to bother commenting, but teachers will still want it because it's so hard to find anyone that comments code properly later in their career.
>>
I've always liked these comments

https://en.wikipedia.org/wiki/Fast_inverse_square_root
>>
>>1074346
Ideally, comments aren't necessary. Your code should tell the entire story, making it clear what everything is, just in the way the variables and methods are named and used. It's just like a perfect relationship: you never need to ask why, never need to explain why, it's just obvious, and there's never any misunderstanding.

So, for those of us in the real world, you put in whatever you were thinking, at the time, about the problem. Or,whatever you had to do to fix a problem, when you put in that bit of code. The comments are there to help you remember the state your mind about the code 10 months from now. Because then, you won't even thought about this code in the past 8 months, and it'll be 3AM, your phone going nuts with texts from your boss asking why it's down, and your SO just made it clear that if you keep them from getting sleep because of your work again this week, there will be NO nookie for the foreseeable future.
>>
>>1074346
Depends entirely on whether the code is meant to be read, reused, at all seen by anyone other than yourself and whatnot.

1. Throwaway or single-use code: Don't bother
2. Code for personal use only: Explain the overall purpose
3. Code for re-use: Explain parameters and returns
4. Code for others to tinker with: Explain everything that isn't obvious

Generally speaking, you should name classes, methods and fields so that their purpose is immediately apparent, and ensure that the code is structured to be readable.

Write-only code is only a problem if someone actually reads it.
>>
>>1074346
you describe what x is
>>
>>1074351
>>1074447
Oh, I think I get it:
int x; // x is a variable
x = 2; // assign 2 to x so that there's 2 in it
x = (x << 3) + (x << 1); // optimization, this executes faster
printf("%d", x); // print x to display 20
>>
This guy, >>1074426, explains a bit of it. I will add that it is situational as well. In college, many of your profs will insist on comments for the smallest thing. This is to get you in the habit of doing it.

Of course you don't need a comment on x=2 in the real world. What you need to comment is the interface code that is deserializing 100 columns of shit into some generic list and each of the 100 columns has a linq query against it that transforms the data specific to what it may or may not contain because hey, loops, explicit declaration and checking each column in a condition statement sucks and doesn't look fast. Naming your implicit variable semi-appropriately isn't enough if you want people to come behind you and maintain that code (i.e., you moved from the interface to the dev team on the new project but the new interface person has to call you constantly because of problems in the file dump in the field and has 20 people screaming at them and they don't have time to figure out why the hell you converted the 4th column to a string of 3 characters when the app connected to the hardware dumps it as a decimal).
>>
>>1074456
Jesus fuck no

You don't comment on every single fucking line

Antone who needs to be told that int x; is setting x as a variable has no place in code.

None of that code requires commentary.

Dont use generic variable names when the serve a real tangible function

float vStaticLoadNewtons;
int vLeverLength;

Are both infinitely more useful, memorable, and organized than:

float x; //variable for static load in newtons
int g; //length of lever in mm
>>
>>1074474
This.

Fuck programmers who abbreviate everything. It's bad enough in mathematics.
Thread posts: 11
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.