Results 1 to 17 of 17

Thread: IDE0017 - Should I simplify all these?

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a new Maserati down a dead end street
    Posts
    4,206

    IDE0017 - Should I simplify all these?

    Maybe I should've paid closer attention 5 years ago when I started coding our application, LOL. But now that I have some free time, I'm looking at Messages and Warnings.
    I have 207 places in my code where it says "Object intialization can be simplified".
    Yes it can be, but should it be?
    I mean, I know I can disable these messages or simply ignore them, but is my code going to better if I do it?
    Thanks.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,239

    Re: IDE0017 - Should I simplify all these?

    It's all personal preference. Visual Studio/ReSharper are very opinionated and will throw warnings on a ton of stuff for you.

    Just tailor it to stuff you wish to have warnings about or not.. there is really no real answer since most of the warnings you'll see are probably just syntactic sugar in C# and don't actually make a difference in the interpreted code.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,067

    Re: IDE0017 - Should I simplify all these?

    One of your primary aims should be to write the most readable code that you can. Readable code is maintainable code. If those changes changes make your code more readable then you should make them. Of course, what is more readable is somewhat subjective, so it's up to you to decide.

  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a new Maserati down a dead end street
    Posts
    4,206

    Re: IDE0017 - Should I simplify all these?

    OK, thank you both!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,067

    Re: IDE0017 - Should I simplify all these?

    If you'd like our subjective opinion, show us the code.

  6. #6

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a new Maserati down a dead end street
    Posts
    4,206

    Re: IDE0017 - Should I simplify all these?

    So a lot of them have to do with form instantiation...
    Code:
    frmCustomerService frmCustomerService = new frmCustomerService(txtCustNo.Text, tab, sdc);
    
    frmCustomerService.StartPosition = FormStartPosition.Manual;
    frmCustomerService.Location = new System.Drawing.Point(this.Location.X + (this.Width - frmCustomerService.Width) / 2, this.Location.Y + (this.Height - frmCustomerService.Height) / 2);
    // 11/03/17 - Added this as the owner so we can return here when it closes.
    frmCustomerService.Show(this);
    Cursor.Current = Cursors.Default;
    This could be a big one, depending how many of these properties I can "simplify"
    Code:
    // Insert a record into xtblMessages
    BusinessObjects.PagerMessage msg = new BusinessObjects.PagerMessage();
    msg.MsgText = String.Format("Special Handling has been turned on for this case by {0}.", Globals.Variables.LoadedName);
    msg.MsgType = "Page";
    BusinessObjects.Emp emp;
    emp = new BusinessObjects.Emp(Globals.Variables.gDatabaseSelected, Globals.Variables.CurID);
    msg.SentBy = Globals.Variables.CurID;
    msg.Subject = String.Format("Special Handling - Case {0} {1}", txtSONumber.Text.Trim(), txtCustomerName.Text.Trim());
    if (emp.EmailAddress != "")
    {
    // Send one message to this user
    msg.MsgRecipient = emp.EmailAddress;
    msg.InsertMessagesRecord(Globals.Variables.gDatabaseSelected);
    }
    // Send a second messgae to ...
    msg.MsgRecipient = Globals.Variables.SpecialHandlingEmail;
    msg.InsertMessagesRecord(Globals.Variables.gDatabaseSelected);
    The reason I say "how many I can" is because previously on a form instantiation block I think I went too far and I put Show(this) inside the braces only to crash with an ineligible form owner error....so just because simplication is pointed out to me I still have to figure out how much of it I can simplify. Or I guess the refactoring suggestion would do it for me? I have not tried that; I am kind of a control freak and rather code it myself then let it generate. Also because all this code works and I am changing working code and will be deploying it at some point, I certainly don't want to break anything.

    So there you go - subjective opinions always welcome!
    Last edited by si_the_geek; Jan 13th, 2021 at 09:54 AM. Reason: removed an email address
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,574

    Re: IDE0017 - Should I simplify all these?

    Those two code blocks can be changed to these variations:
    Code:
    frmCustomerService frmCustomerService = new frmCustomerService(txtCustNo.Text, tab, sdc)
       {
        StartPosition = FormStartPosition.Manual,
        Location = new System.Drawing.Point(this.Location.X + (this.Width - frmCustomerService.Width) / 2, this.Location.Y + (this.Height - frmCustomerService.Height) / 2)
       };   
    // 11/03/17 - Added this as the owner so we can return here when it closes.
    frmCustomerService.Show(this);
    Cursor.Current = Cursors.Default;
    Code:
    // Insert a record into xtblMessages
    BusinessObjects.PagerMessage msg = new BusinessObjects.PagerMessage()
      {
       MsgText = String.Format("Special Handling has been turned on for this case by {0}.", Globals.Variables.LoadedName),
       MsgType = "Page",
       SentBy = Globals.Variables.CurID;
       Subject = String.Format("Special Handling - Case {0} {1}", txtSONumber.Text.Trim(), txtCustomerName.Text.Trim()) 
      };   
    BusinessObjects.Emp emp;
    emp = new BusinessObjects.Emp(Globals.Variables.gDatabaseSelected, Globals.Variables.CurID);
    if (emp.EmailAddress != "")
    {
    // Send one message to this user
    msg.MsgRecipient = emp.EmailAddress;
    msg.InsertMessagesRecord(Globals.Variables.gDatabaseSelected);
    }
    // Send a second messgae to ...
    msg.MsgRecipient = Globals.Variables.SpecialHandlingEmail;
    msg.InsertMessagesRecord(Globals.Variables.gDatabaseSelected);
    I feel they are more readable because you eliminate the object names on many lines, and you also group the initialisation parts together.

  8. #8

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a new Maserati down a dead end street
    Posts
    4,206

    Re: IDE0017 - Should I simplify all these?

    @si_the_geek, So if you were my successor here you would want me to take the time and address the 207 occurrences where my code *could* be simplified?
    My point in asking is while I can certainly find these and change them, am I going to go nuts doing this many?
    I guess I could tackle a few at a time, as time and my sanity permit!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  9. #9
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,239

    Re: IDE0017 - Should I simplify all these?

    Not sure if the code you posted is stripping whitespace, but it makes me feel very claustrophobic. I would rewrite it as follows (untested.. just modified it in the post).

    C# Code:
    1. // Insert a record into xtblMessages
    2. var msg = new BusinessObjects.PagerMessage {
    3.     MsgText = $"Special Handling has been turned on for this case by {Globals.Variables.LoadedName}.",
    4.     MsgType = "Page",
    5.     SentBy = Globals.Variables.CurID,
    6.     Subject = $"Special Handling - Case {txtSONumber.Text.Trim()} {txtCustomerName.Text.Trim()}")
    7. };
    8.  
    9. var emp = new BusinessObjects.Emp(Globals.Variables.gDatabaseSelected, Globals.Variables.CurID);
    10. if (emp.EmailAddress != string.Empty)
    11. {
    12.     // Send one message to this user
    13.     msg.MsgRecipient = emp.EmailAddress;
    14.     msg.InsertMessagesRecord(Globals.Variables.gDatabaseSelected);
    15. }
    16.  
    17. // Send a second message to ...
    18. msg.MsgRecipient = Globals.Variables.SpecialHandlingEmail;
    19. msg.InsertMessagesRecord(Globals.Variables.gDatabaseSelected);

    Basically I've removed the duplicate typing of the object by using an implicit type in the declaration. I've set the properties when creating the instance, and I've switched to use string interpolation instead of string.format for the strings.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,574

    Re: IDE0017 - Should I simplify all these?

    Quote Originally Posted by MMock View Post
    @si_the_geek, So if you were my successor here you would want me to take the time and address the 207 occurrences where my code *could* be simplified?
    My point in asking is while I can certainly find these and change them, am I going to go nuts doing this many?
    I guess I could tackle a few at a time, as time and my sanity permit!
    If I was the successor I would prefer it had been changed, but would understand if it wasn't.

    Doing a few at a time (and potentially giving up after a while) is certainly a valid approach

  11. #11

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a new Maserati down a dead end street
    Posts
    4,206

    Re: IDE0017 - Should I simplify all these?

    Okay, so I have a question. I am launching forms from other forms quite often. So let's say the user has form A positioned nicely on his screen where he wants it and he launches form B. The requirements have always been to create form B centered to form A. So I do this:

    Code:
                        frmPrintRepairReport frmPrintRepairReport = new frmPrintRepairReport(soNumber, drJobs.SupressPricing, txtEquipmentOwnerName.Text);
                        frmPrintRepairReport.StartPosition = FormStartPosition.Manual;
                        frmPrintRepairReport.Location = new System.Drawing.Point(this.Location.X + (this.Width - frmPrintRepairReport.Width) / 2, this.Location.Y + (this.Height - frmPrintRepairReport.Height) / 2);
                        frmPrintRepairReport.Show();
    But now to "simplify" I am diong this:
    Code:
                        frmPrintRepairReport frmPrintRepairReport = new frmPrintRepairReport(soNumber, drJobs.SupressPricing, txtEquipmentOwnerName.Text)
                        {
                            StartPosition = FormStartPosition.Manual,
                            Location = new System.Drawing.Point(this.Location.X + (this.Width - Width) / 2, this.Location.Y + (this.Height - Height) / 2)
                        };
    Notice how I have two Width variables. One is the width of the parent form, the other the child. I changed frmPrintRepairReport.Width to just "Width" since it's what I am simplifying, but the compiler is also telling me to remove "this". That's wrong because I need it to differentiate what Width I mean. I know I can define a variable parentWidth and dispense with "this" but shouldn't it be smart enough to know I'm differentiating one from the other?
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  12. #12
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,239

    Re: IDE0017 - Should I simplify all these?

    How is the width of the parent form received? I can't see where you have width declared, so I don't know exactly what is going on, but I wouldn't call both properties "Width."

    I wouldn't call it just "Width", I would call it "parentWidth" if it is the parent form's width and height.

    I also prefix all class-level properties/methods with "this." So personally I tell VS to ignore those warnings.

  13. #13

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a new Maserati down a dead end street
    Posts
    4,206

    Re: IDE0017 - Should I simplify all these?

    Quote Originally Posted by kfcSmitty View Post
    ...I wouldn't call both properties "Width."
    I am not calling both properties Width; Microsoft is. Form A has a Width property and Form B has a Width property; they are both forms.
    I like your idea of having VS remove "this" warnings. Like when I set the caption of a form and say Text = "Form Caption" I'd much rather say this.Text, to make it clear who the Text property belongs to.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,067

    Re: IDE0017 - Should I simplify all these?

    Quote Originally Posted by MMock View Post
    I am not calling both properties Width; Microsoft is. Form A has a Width property and Form B has a Width property; they are both forms.
    I like your idea of having VS remove "this" warnings. Like when I set the caption of a form and say Text = "Form Caption" I'd much rather say this.Text, to make it clear who the Text property belongs to.
    No, no, no. How could there be any confusion as to who the property belongs to? If it's not qualified at all then it can only be a property of the current type. The point of this is to differentiate members from local variables. You're trying to use it to solve a problem that doesn't exist.

  15. #15
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,239

    Re: IDE0017 - Should I simplify all these?

    Quote Originally Posted by MMock View Post
    I am not calling both properties Width; Microsoft is. Form A has a Width property and Form B has a Width property; they are both forms.
    I like your idea of having VS remove "this" warnings. Like when I set the caption of a form and say Text = "Form Caption" I'd much rather say this.Text, to make it clear who the Text property belongs to.
    Hmm.. I guess we differ in approaches. If the form has to be opened centre parent, then I would be creating a constructor for the form that receives a width and height and sets the appropriate settings in that constructor, I wouldn't be doing it in every form I instantiate the form from. I try to abstract everything away... so I would move all of your setup logic into the constructor.

  16. #16

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a new Maserati down a dead end street
    Posts
    4,206

    Re: IDE0017 - Should I simplify all these?

    Quote Originally Posted by jmcilhinney View Post
    ....How could there be any confusion as to who the property belongs to? If it's not qualified at all then it can only be a property of the current type. The point of this is to differentiate members from local variables. You're trying to use it to solve a problem that doesn't exist.
    Yes, I agree with your first three sentences but what are you saying is the problem that doesn't exist? Here is the whole routine. It runs when you click a command button on a form called frmAddUnit, and it is instantiating frmPrintRepairReport. So we have two of the same types, two windows forms, with the same properties. Without a qualifier, the appearance of keyword this that I highlighted would be Location, Width and Height of frmPrintRepairReport (as you said - if not qualified it's the property of the current type). I have to say this so it knows to use the properties of frmAddUnit, the parent or containing form.

    Name:  IDE0017.jpg
Views: 12
Size:  16.9 KB
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  17. #17

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a new Maserati down a dead end street
    Posts
    4,206

    Re: IDE0017 - Should I simplify all these?

    Quote Originally Posted by kfcSmitty View Post
    Hmm.. I guess we differ in approaches ... I would move all of your setup logic into the constructor.
    No, we don't differ in approaches at all. I just may not have been thinking about it enough but you're absolutely right.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width