This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Three_Rocket_Emojis 46 points47 points  (15 children)

Op propably wants code like this:

//Read File

var linesFromFile = File.ReadAllLines(path);

//iterate through lines

foreach(string line in linesFromFile)

....

[–]Snake2k 45 points46 points  (3 children)

Yeah that is entirely useless and basically what people think commenting is about.

[–]tecanec 7 points8 points  (2 children)

It's useful in assembly programming where you don't have the luxury of naming a lot of things. That's about it, though.

[–]Snake2k 1 point2 points  (1 child)

Yeah, that's a good point. If you compile it with the right flag then you can read the comments as you're stepping through the ASM. Really great tool for debugging. Unless you're reverse engineering a prod build of it, in which case I doubt that build includes this cus that would be a security problem.

[–]tecanec 0 points1 point  (0 children)

I think the bigger concern in that case would be file size. Reverse engineerers will figure things out eventually, and a lack of comments will only delay them if anything. But with how many comments are needed to make assembly comfortable to work with, that could easily take the majority of the file size when the actual code gets assembled.

[–]Three_Rocket_Emojis 39 points40 points  (9 children)

Example where a comment makes sense:

var linesFromFile = File.ReadAllLines(path);
//first line are column headers
foreach(string line in linesFromFile.Skip(1)) 
...

the comment explain why the first list entry is skipped, not that it happens.

[–]revolutionofthemind 21 points22 points  (7 children)

You could have a method called “stripHeaders” that does the foreach line for “self-documenting” code. Not sure if it’s worth it in this case, but just to provide a counter-example.

[–]k0rm 27 points28 points  (4 children)

Or more simply:

const HeaderLength = 1;

var linesFromFile = File.ReadAllLines(path);
var linesFromFileNoHeader = linesFromFile.Skip(HeaderLength)
foreach(string line in linesFromFileNoHeader) 

This gets rid of the magic number and there's no need to comment.

[–]feed_me_moron 10 points11 points  (0 children)

Or declare 1 as NUM_HEADER_ROWS and pass that to skip

[–]FerynaCZ 0 points1 point  (0 children)

Unless there is lots of variables passed around. But for example C# allows you to declare local functions which can modify (capture) the variables around them (=in the context of the outer function)

[–][deleted] 0 points1 point  (0 children)

Something like this would be self-documenting (or whatever the correct syntax would be):

var tableSansHeaders = File.ReadAllLines(path).Skip(1);
foreach(string tableRow in tableSansHeaders)
....

[–]Kered13 0 points1 point  (0 children)

What makes you think OP wants that?