Can you pls. Code review of C# code(.cs file)?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2039 Views - Last Post: 24 February 2011 - 12:28 AM Rate Topic: -----

#1 montu3377  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 11

Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 01:00 PM

Hi,

Can you pls. do code review? I am attaching .cs file (renamed to .txt).

I am writing description of each function here.

1. private void FileBrowse_Click(object sender, EventArgs e):

This function is used for selecting .txt file with validation as .txt file has particular format.

2. StartProcess():

This function starts the process after clicking button "Start process" from UI and selects particular word templates. There are five different types of templates which has bookmark in it. This function calls sub-function named "GenerateXMLDocs(,,,).

3. private bool GenerateXMLDocs(object FileName ,object InputCSVFile,string CSVFileName)
This function generates xml and associated .doc file. The main purpose of this function is to get the
values from .txt file and plug those value one by one to bookmark in the word template and generate
.xml file also for each .doc file.
There are some other small functions in the file which is used to get the values to generate xml file and set the flag in database once process is completed for particular record.

Pls. review the code. I want to know that what is the code quality level? is it best or good or avg or below avg or really bad coding.? i am attaching .txt file which is basically .cs file.

Spoiler


MOD EDIT: Moved code into post. Don't ATTACH your code, post it IN CODE TAGS, as I have done, within the message.

:code:

Where the code is so large, I have wrapped it in spoiler tags.

Is This A Good Question/Topic? 0
  • +

Replies To: Can you pls. Code review of C# code(.cs file)?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 01:04 PM

You really expect a bunch of strangers on the Internet to invest the time in reviewing 58K of code?
Was This Post Helpful? 1
  • +
  • -

#3 montu3377  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 11

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 01:13 PM

Sorry, i am new to this forum, so i taught let me attach the code. Anyway here i am using spoiler.

I think now it will be fine.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 01:15 PM

Dude the code is over 1000 lines long and has minimal comments. It would take a rather long time for anyone to even understand whats going on, let alone 'review' it

Perhaps it would be better to post smaller, more manageable sections of the code with information on what it does so we can answer you without spending a lot of time reading
Was This Post Helpful? 1
  • +
  • -

#5 montu3377  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 11

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 01:31 PM

I hope this function is fine with you to do code review.

This function is generating .xml and .doc file. It also retrives value from .txt file and then
plug each value to bookmark sitting inside of .doc file and then generate xml file and save the
modified .doc file in particular network location.

Pls. let me know if you want to know more.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#6 montu3377  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 11

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 01:43 PM

Do you have any question on the code? pls. let me know. Pls. let me know if this code is still longer
than you expected.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 01:48 PM

In general I think you've worked harder instead of smarter.
With less work on avoiding a problem and more time on after-the-fact.

One example:

DialogResult Browse = openFileDialog.ShowDialog();
            if (Browse==DialogResult.OK)
            {
                if ((Path.GetExtension(openFileDialog.FileName.ToString()) != ".txt"))
                {
                    MessageBox.Show("Pls. Select Proper File!!");
                    txtFileName.Focus();
                    return;
                }

You can specifiy file type filters in an openFileDialog. So if you had used that you could have forced the user to only be able to open .txt files.

Or this

//Create Copy of New file with exact same data as Original file has some invalid characters in name.
                fin = new FileStream(txtFileName.Text.ToString(), FileMode.Open);
                fout = new FileStream(DestinationFilename, FileMode.Create);


How would a file have been created with invalid characters in the name to begin with? So what are you really filtering out? Characters that are actually valid to the filesystem but not valid to your application?
Why are you doing the whole filestream read/write thing instead of just renaming the file?

All of your button.Click handlers are filled with functioning code rather than activating methods.
for example:
public void BtnStop_Click(object sender, EventArgs e)
        {
            BtnStop.Enabled = false;
            BtnGenerateDocsFromTemplate.Enabled = true;
            StopClicked = true;
            BtnExit.Enabled = true;

        }



instead of
private void btnStop_click(object sender, EventArgs e)
{
   Stop();
}
public void Stop()
{
            BtnStop.Enabled = false;
            BtnGenerateDocsFromTemplate.Enabled = true;
            StopClicked = true;
            BtnExit.Enabled = true;
}

In this example you have a public method for Stop that can be called from anywhere in your class, or from an outside class. If you put it all in the buttonhandler like you did its just ugly to use from outside: Someone has to know it is the btnStop_click method. yuck. and you are forced to make up a sender and event args just to call it. If you have a Stop() method with no args its easy to call.

All in all there is a LOT of places where this could be improved upon in overall style and architecture. There are some really good books on refactoring code. I suggest you pick one up.

Don't let this discourage you by any means. It's a good program for someone early in their coding development. You can't expect to run before you learn to walk.
Was This Post Helpful? 1
  • +
  • -

#8 montu3377  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 11

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 02:10 PM

I agree with your comment on code review. Can you pls. suggest some good/best books on refactoring code?
and if you know any book to improve code architecture?

Thanks.

One more question :

What is code quality rating out of 10? 0 is lowest and 10 is highest. Pls. consider that programmer has about 9-10 yrs of experience.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 02:13 PM

View Postmontu3377, on 23 February 2011 - 03:10 PM, said:

One more question :
What is code quality rating out of 10? 0 is lowest and 10 is highest. Pls. consider that programmer has about 9-10 yrs of experience.

I was going to guess closer to this being the 9th or 10th program the author had written in C#.
Was This Post Helpful? 0
  • +
  • -

#10 montu3377  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 11

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 02:21 PM

Can you pls. explain more? I didn't understand your comment completely.
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 02:22 PM

Reference material:

http://lmgtfy.com/?q="Refactoring Csharp"

http://www.amazon.co...ng+c%23&x=0&y=0
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 02:30 PM

View Postmontu3377, on 23 February 2011 - 03:21 PM, said:

View PosttlhIn`toq, on 23 February 2011 - 03:13 PM, said:

View Postmontu3377, on 23 February 2011 - 03:10 PM, said:

One more question :
What is code quality rating out of 10? 0 is lowest and 10 is highest. Pls. consider that programmer has about 9-10 yrs of experience.

I was going to guess closer to this being the 9th or 10th program the author had written in C#.


Can you pls. explain more? I didn't understand your comment completely.


It doesn't look like code written by someone with a decade of experience, in C#.
It looks like code written by a university student still in class. Like it was maybe their 9th or 10th program they have ever written in C#.

Maybe the author has 10 years experience; but in another programming language. That would translate to good logical thinking and methodical approach to work, without actually making their C# code look all fancy and polished.

Someone can have experience at programming, but not have experience in the .NET framework and therefore not know their way around a lot of the controls, framework layout etc.

If someone tried to pass this off as an example of 10 years of experience in C#, I wouldn't hire them.
Was This Post Helpful? 0
  • +
  • -

#13 montu3377  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 11

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 02:56 PM

So as per you ,this programmer needs to improve in which areas? like in refactoring code and what else?
can you pls. specify specifically? so i will try to improve it in those specific area. The whole purpose of doing these exercise is that to improve code quality. do you think "Code complete" which is available on amazon.com is also another best book?

Thanks
Was This Post Helpful? 0
  • +
  • -

#14 montu3377  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 11

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 03:02 PM

One more question which i forget to ask you in last post.

What are the ways to improve lacking skills? likewise reading books, write more code etc.

Pls. let me know your taught.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Can you pls. Code review of C# code(.cs file)?

Posted 23 February 2011 - 03:25 PM

Honestly, I'm starting to regret having given input here.
I should have followed my collegues here and just said "Are you nuts, you want us to critique your entire 1000 lines of code?"

I took the time to give some review in an effort to help and so as to not always come off as the designated hard ass. Now no matter what I say I'm sure to just perpetuate that image.

Quote

So as per you ,this programmer needs to improve in which areas? like in refactoring code and what else? can you pls. specify specifically?


First you wanted a critique. Then a scoring. Then book recommendations. Then an appraisal of the coder. Then a study plan on how and where to improve. Enough.

No. I'm not going to take another 2-3 hours to write out a course plan for someone I've never met. There are universities and professors who get paid for that. You asked for a basic critique of the code. You got that.

If the coder who wrote this has a decade of experience in C# as you said, then tell them to stop now. Or at least resign themselves to coding for fun but not for profit. Anyone with 10 years experience shouldn't be asking "How do I get better? Do I read books or do more coding exercises?"

I'm going to drop in the same standard resources I give to all the newbies. I hope it helps. Beyond that, I'm withdrawing from this thread.


I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard - or you can learn by doing the tutorials and following a good "How to learn C#" book.

May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your local book seller or even public library.

C# resources. Start here

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables

Intro to C# online tutorial

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.
Was This Post Helpful? 3
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2