|
What kind of comments do you add and when?
I'm sometimes wondering if my commenting is sufficient and other times if it's redundant.
For example when I use the same command several times, or when having a loop that only consists of like 3 short commands in total, I feel like I should add a comment, but when I do I feel like it's redundant.
I think a comment is redundant if you can easily grasp the purpose of a section of code in 5 seconds or less. If it gets large, comments should only be used for categorizing/grouping.
What do you think?
|
|
|
|
|
Hi,
ahead of a class/method: its purpose and contract, so you know what the class/method represents/does without reading its code
upfront in a method: a macroscopic how (algorithm name, reference) and why (e.g. why a particular way was chosen when there are obvious alternatives, and/or why some alternative was not chosen).
inside a method: hardly any, it should be obvious, mainly by choosing all identifiers carefully, and by keeping method length limited to 50 lines.
|
|
|
|
|
I usually clump things into logical groups, then I toss some comment above that group of code. If it's a simple bit of code, I use a small comment... if it's more complex, I might use a bigger comment. All my methods have comments on them (though some disagree with this practice and only comment public methods). I comment so others will be able to understand the code easily if they come across it later. I also have various practices for how I comment that I've just adopted over time. For example, I typically shove most of the variables at the top of the method and comment that section with:
int x;
Person person;
It's standard for me, so I know when I see that that I can pretty much skip over that section of code. That illustrates another point -- that comments aren't always to describe the code, but have other purposes. In this case, the other purpose is so I know what code to skip over, which speeds up reading the code. For a similar reason, I comment every single property I have. I do this because it avoids an interruption in reading the code... the consistency between each property means there is one less thing my brain has to process when scanning code.
Also, most of the time, you should avoid commenting what the code does and instead comment what the code was made to do. For example, this would be a bad comment:
foreach(Person p in this.GetPeople())
{
if(p.Height > Person.AverageHeight)
{
tallPeople.Add(p);
}
else if(p.Height == Person.AverageHeight)
{
averagePeople.Add(p);
}
else
{
shortPeople.Add(p);
}
}
A better comment might be:
foreach(Person p in this.GetPeople())
{
if(p.Height > Person.AverageHeight)
{
tallPeople.Add(p);
}
else if(p.Height == Person.AverageHeight)
{
averagePeople.Add(p);
}
else
{
shortPeople.Add(p);
}
}
There are other nuances when it comes to commenting code, but you'll develop an intuition for these types of things as time goes on. Whatever system you develop, just follow it consistently.
|
|
|
|
|
I think a few developers assume that someone with no knowledge of the language is going to be reading the code. My thought on that is that you should assume that the person reading your code is capable of doing your job and therefore can be expected to understand non business specific rules. As for anything that is really specific to the business, I totally agree with Luc that you should comment a methods purpose and if a specific method has been chosen, why that method was chosen. Future developers may after all think they know better, tear out your code and break any tests, or worse, live environments.
As for the code itself, well written code using appropriate variable naming and sparing comments on only the most complex of routines should see you through.
|
|
|
|
|
The purpose of comments is to document your code, both for your own memory and to guide anyone who might inherit your work. Sometimes this takes a few words, sometimes it takes a novelette. And just because something is obvious to you now doesn't mean it will be obvious when you come back to it three years latter (experience speaks. )
For ad hoc apps that will be used once or twice and discarded, I don't usually bother with comments unless I'm doing something complicated. Otherwise, I will use XML comments on classes, properties and methods, then break up sections of code and place comments at the header to explain what I am doing in that section.
|
|
|
|
|
IMO, a comment should be such that when another programmer picks up your code, he should be able to know what that code does by going through these comments.
|
|
|
|
|
Thanks for the feedback.
I was a bit lost because I used quite similar commenting as aspdotnetdev, but recently I finished a C++ class and the teacher complained all the time about me not commenting enough.
When I say obvious I don't mean obvious in the context, I mean something like
for (int i = 0; i < stringArray.Length; i++)
{
bool test = int.TryParse(stringArray[i], out testint)
} I.e. using blatantly obvious methods and basically re-stating their names (said teacher complained about me not doing that).
I tend to group larger sections together with regions, like button events, private methods, etc.
|
|
|
|
|
In addition to what the other posters said, it's also helpful to have comments explain dependencies/interactions between the code you're looking at, and other code in distant parts of your application.
If someone else is going to be maintaining your code, it's these non-obvious interactions that are going to cause them the most problems.
|
|
|
|
|
I'm developing an application to talk to a rfid card reader using C#. The application works fine on Windows XP, but when it comes to Windows 7, i just cant seem to connect to the device. I can ping to the device, meaning there's connectivity with the reader.
I have tried with both firewalls off (public and private) and have turned off my anti-virus software too, but couldnt.
Any ideas?
aHa
|
|
|
|
|
More information is need but, accessing a com port on Windows 7 needs to have specific security privilegde.
Check your C# application to see if you do not catch a UnauthorizedAccessException
My two cents
|
|
|
|
|
I am using TCP/IP, not COM port. I can ping to the device, cant connect to the device.
|
|
|
|
|
Sorry, I thought it was a local device.
Try to use the :
[^] or Wireshark in order to see if the traffic comes in on both side.
|
|
|
|
|
Hi All,
I have a DataList that is populated from a database table. The data shows in the dataList with no issues. The problem is when I link the dit button for a particular row of data the whole dataList disapears from the screen. It returns when I click on another button with the row in edit mode.
Here is the code behind the edit button:
protected void DataList_EditCommand(Object sender, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataSource = DataView1;
DataList1.DataBind();
}
Does anyone know of any reason this would be occuring?
Thanks,
Mel
|
|
|
|
|
Hi,
I develop a csharp smart device project using Visual Studio2008.I wanna load my data from my database .sdf in a DataGrid View.I add the tool from the Toolbox and in the its proprities,I add the datatable to the DataRessource field.When running,an sqlceexception appereas telling me that:
The column name is not valid. [ Node name (if any) = ,Column name = Effectué/Non ]
and it goes to this ligne of my code
int returnValue = this.Adapter.Fill(dataTable);
What is the problem?
Thank u for u suggestions
|
|
|
|
|
Based on the error message I would say that the slah "/" in the column name is responsable for the error.
Without any further details this is the best I can do.
|
|
|
|
|
Hi,
My issue stills the same.My form's code is:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ModeDifféré
{
public partial class DataGridT : Form
{
public DataGridT()
{
InitializeComponent();
}
private void DataGridT_Load(object sender, EventArgs e)
{
this.travauxTableAdapter.Fill(this.baseGmaoLocaleDataSet2.Travaux);
}
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
}
}
}
What can I add to let the system shows the data stocked in the table Travaux??
Regards
|
|
|
|
|
a,c are streams. b is an array. I am performing this
<br />
while(some condition)<br />
{<br />
a.read(b);
c.write(b);
}<br />
In order to improve performance, I want to do the read and write simultaneously. Something like this crossed my mind
<br />
a.read(b1)<br />
a.read(b2) Parallel c.write(b1)<br />
a.read(b1) Parallel c.write(b2)<br />
a.read(b2) Parallel c.write(b1)<br />
and so on...
I cannot implement this in a simple loop because of
Some obvious coditions
1) a cannot write into bx till bx has been read by c
2) c cannot read from bx till bx has been written to by a.
Can I get some idea how this can be implemented ?
|
|
|
|
|
Hi,
you could create two threads, one looping the reads, the other the writes; then the arrays need to be handed over from one thread to the other, an easy way to do so is with a Queue. If you need lots of arrays and they all are same size, same type, you could skip creating and garbage collecting them by creating a pool of arrays, holding them in an "empty queue", so the reader would get one from there, fill it, pass it thru the other queue; and the writer would get a filled array, write it out, and stuff the array in the "empty queue" again.
[ADDED]
Forgot to mention the obvious: you need some synchronization mechanism for this to work properly; have a look at the lock statement.
[/ADDED]
|
|
|
|
|
Good Morning,
I work with Visual Studio2008.I create a listview.When running,the items appear in the same ligne and very near to each other.How can arrange their disposition to see them everyone in a ligne? I do that but change nothing in my disposition using the link below:http://msdn.microsoft.com/en-us/library/ms229643.aspx[^]
I wanna informe u that I work with Csharp smart device
Thanks Smile Cry
modified on Thursday, April 22, 2010 11:28 AM
|
|
|
|
|
|
Hmm cant find my error here.
I have the following code
private static Image ImagesToTiff(Image[] pages)
{
try
{
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.ColorDepth, (long)ColorDepth.Depth32Bit);
MemoryStream ms = new MemoryStream();
Bitmap Tiff = new Bitmap(pages[0]);
Tiff.Save(ms, GetEncoder(ImageFormat.Tiff), ep);
ep.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
for (int i = 1; i < pages.Length; i++)
{
Tiff.SaveAdd(pages[i], ep);
if (i == pages.Length - 1)
{
EncoderParameters epx = new EncoderParameters(2);
epx.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.Flush);
epx.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.LastFrame);
Tiff.SaveAdd(epx);
}
}
return Image.FromStream(ms);
}
catch (Exception)
{
throw;
}
}
public static void DemoTheCode()
{
Bitmap b = new Bitmap(100,100);
Bitmap[] imgs = new Bitmap[2];
Graphics g = Graphics.FromImage(b);
g.FillRectangle(Brushes.Red,0,0,100,100);
g.Save();
imgs[0] = b;
g.FillRectangle(Brushes.Blue,0,0,100,100);
g.Save();
imgs[1] = b;
Image MyTiffOfTwoPages = ImagesToTiff(imgs);
int Pages = MyTiffOfTwoPages.GetFrameCount(FrameDimension.Page);
MyTiffOfTwoPages.Save(@"C:\Demo.Tiff", ImageFormat.Tiff);
}
Please look at the 2 last lines of code to see what i mean...
Does anyone know why ? am i missing somwthing here ?
With great code, comes great complexity, so keep it simple stupid...
|
|
|
|
|
I cannot identify the specific problem but you might try looking at Save images into a multi-page TIFF file or add images to an existing TIFF file[^] here on CodeProject. There are several other articles here, this is just the first that I found.
Good luck!
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Thanks for the reply Henry.
Before posting this, I did find all Tiff related article on CP (I hate it to, when people dont do the research themselfs ), but none, did what I did.
I have no problem with the code, if I write the images directly to a file, and dont use the MemoryStream, but in my case i have to make the image in memory, as it will be put into an SQL server as blob data. and later on it will have to be saved to another filesystem.
But ill be searching on. again thx for the reply
With great code, comes great complexity, so keep it simple stupid...
|
|
|
|
|
Hello,
I have window application in windows7. It updates datagrids from sql DB.
In windows XP it works fine .
In windows7- there are computers it works fine and there are computers the application itself loads but it can't connect to the sql server (and update the datagrids). I have .net framework installed on both computer.
What can be the problem?
|
|
|
|
|
Where is the SQL server you are trying to connect to? In same network or any other?
|
|
|
|
|