Click here to Skip to main content
15,906,296 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi guys,

Can any one tell which code is better:

1.
C#
public void Ftpfunction(List<string> quoteNos, DataTable dt, string Templatename)
        {
            var row = GlobalVariables.dtglobal.Select("templatename='" + Templatename + "'");
            string path = row[0]["UPSLabelsPathuid"].ToString();            string username = row[0]["UPSLabelsPathuid"].ToString();
            string password = row[0]["UPSLabelsPathpwd"].ToString();

            var ftpfiles = FtpDownload.GetFiles(path, username, password);
            FtpDownload ftpClient = new FtpDownload(path, username, password);
            string desImage = string.Empty;
            foreach (var quote in quoteNos.Where(x => ftpfiles.Contains(x)))
            {
                desImage = System.AppDomain.CurrentDomain.BaseDirectory + @"\image\" + quote + ".jpg";
                if (!File.Exists(desImage))
                    ftpClient.download(quote + ".jpg", desImage);
            }
        }



2:
C#
public void Ftpfunction(List<string> quoteNos, DataTable dt, string Templatename)
        {
            string _sqlWhere = "templatename='" + Templatename + "'";
            DataTable _newDataTable = GlobalVariables.dtglobal.Select(_sqlWhere).CopyToDataTable();

            FtpDownload ftpClient = new FtpDownload(_newDataTable.Rows[0]["UPSLabelsPath"].ToString(), _newDataTable.Rows[0]["UPSLabelsPathuid"].ToString(), _newDataTable.Rows[0]["UPSLabelsPathpwd"].ToString());
            string desImage = string.Empty;

            var ftpfiles = FtpDownload.GetFiles(_newDataTable.Rows[0]["UPSLabelsPath"].ToString(), _newDataTable.Rows[0]["UPSLabelsPathuid"].ToString(), _newDataTable.Rows[0]["UPSLabelsPathpwd"].ToString());

            foreach (var quote in quoteNos.Where(x => ftpfiles.Contains(x)))
            {
                desImage = System.AppDomain.CurrentDomain.BaseDirectory + @"\image\" + quote + ".jpg";
                if (!File.Exists(desImage))
                    ftpClient.download(quote + ".jpg", desImage);
            }
        } 
Posted
Comments
TheRealSteveJudge 20-Feb-15 2:40am    
The real question is: Is any of these codes good?
On the other side it is more important that the code does what
it is supposed to do and that it is readable.
Your code is not readable.
e.g. what does "desImage" mean?
You should use self-explanatory names.
If you will have a look at your code in e.g. 1 year again
you will have no clue what it really does.
tanishtaman 20-Feb-15 3:13am    
I m trying to make application centralized by removing repetitive code. But some one in our team spoils everything. In eg. 1 it is only a thing which i m trying to do but making a class and using that class every where. hope u understand.
Wendelius 20-Feb-15 2:41am    
Better in which way?
tanishtaman 20-Feb-15 2:58am    
in way or understanding and doing changes quicker. According to me, using code like "string _sqlWhere = "templatename='" + Templatename + "'";
DataTable _newDataTable = GlobalVariables.dtglobal.Select(_sqlWhere).CopyToDataTable();" and _newDataTable.Rows[0]["string"] in every part of the application is bad habit. Please do reply guys about your opinion.

As long as both do what is required "better" becomes largely subjective. However the first version is easier on the eye and easier to understand at first glance

Note though...
- the System on System.AppDomain is redundant
- declaration of variables is inconsistent var row... but string path (etc)
- desImage = string.Empty; is not actually required (the value is not used)
- desImage could be declared within the foreach loop
- As TheRealSteveJudge pointed out some of your variable naming could be improved - it is not clear what desImage would be for example.
- Formatting in version 2 would make it easier to read e.g. rather than one very long line introduce linefeeds
C#
var ftpfiles = FtpDownload.GetFiles(_newDataTable.Rows[0]["UPSLabelsPath"].ToString(),
                                  _newDataTable.Rows[0]["UPSLabelsPathuid"].ToString(),
                                  _newDataTable.Rows[0]["UPSLabelsPathpwd"].ToString());

The main advantage for me that version 1 has over version 2 is the ease of being able to introduce some validation on the datarow content (which I presume has happened outside this function) - I would probably be doing some null checking etc before diving in to use the data
 
Share this answer
 
The first code is looking clean and better.. It is good as for maintenance wise, as coder has to change value for one time only and not multiple times..
 
Share this answer
 
Comments
manak chand 20-Feb-15 3:07am    
Please do not down vote if you are accepting the solution and pls give some rating as well.. thanks...
tanishtaman 20-Feb-15 3:14am    
happy :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900