C# Coding Best Practice [closed]











up vote
-2
down vote

favorite












I'm currently doing work which requires me to convert VB.Net code to C#.
I've been using the "Builder Pattern" primarily and this has me converting many functions that are one single call of a function ie. SomeFunction(var1,var2,var3) into:



Dim Director As New SomeDirector
With Director
.SomeProperty = SomeValue
.SomeProperty2 = SomeValue2
End With


My concern is that this creates 5-6 lines of code rather than one single line. Is there a way for me to do this in a more concise way or is it better to have the 5-6 lines of code?



Thanks!










share|improve this question













closed as unclear what you're asking by SeM, Gert Arnold, Tim Lewis, Mark Tomlin, omajid Nov 22 at 22:44


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 2




    I don't get it. That code is not a function call and it is not c#. Regardless, best practice questions are better suited for codereview.stackexchange.com
    – Crowcoder
    Nov 22 at 16:40






  • 1




    I would actually recommend softwareengineering over codereview in this circumstance.
    – A Friend
    Nov 22 at 16:46















up vote
-2
down vote

favorite












I'm currently doing work which requires me to convert VB.Net code to C#.
I've been using the "Builder Pattern" primarily and this has me converting many functions that are one single call of a function ie. SomeFunction(var1,var2,var3) into:



Dim Director As New SomeDirector
With Director
.SomeProperty = SomeValue
.SomeProperty2 = SomeValue2
End With


My concern is that this creates 5-6 lines of code rather than one single line. Is there a way for me to do this in a more concise way or is it better to have the 5-6 lines of code?



Thanks!










share|improve this question













closed as unclear what you're asking by SeM, Gert Arnold, Tim Lewis, Mark Tomlin, omajid Nov 22 at 22:44


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 2




    I don't get it. That code is not a function call and it is not c#. Regardless, best practice questions are better suited for codereview.stackexchange.com
    – Crowcoder
    Nov 22 at 16:40






  • 1




    I would actually recommend softwareengineering over codereview in this circumstance.
    – A Friend
    Nov 22 at 16:46













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I'm currently doing work which requires me to convert VB.Net code to C#.
I've been using the "Builder Pattern" primarily and this has me converting many functions that are one single call of a function ie. SomeFunction(var1,var2,var3) into:



Dim Director As New SomeDirector
With Director
.SomeProperty = SomeValue
.SomeProperty2 = SomeValue2
End With


My concern is that this creates 5-6 lines of code rather than one single line. Is there a way for me to do this in a more concise way or is it better to have the 5-6 lines of code?



Thanks!










share|improve this question













I'm currently doing work which requires me to convert VB.Net code to C#.
I've been using the "Builder Pattern" primarily and this has me converting many functions that are one single call of a function ie. SomeFunction(var1,var2,var3) into:



Dim Director As New SomeDirector
With Director
.SomeProperty = SomeValue
.SomeProperty2 = SomeValue2
End With


My concern is that this creates 5-6 lines of code rather than one single line. Is there a way for me to do this in a more concise way or is it better to have the 5-6 lines of code?



Thanks!







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 16:36









Wonka Bear

12




12




closed as unclear what you're asking by SeM, Gert Arnold, Tim Lewis, Mark Tomlin, omajid Nov 22 at 22:44


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by SeM, Gert Arnold, Tim Lewis, Mark Tomlin, omajid Nov 22 at 22:44


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    I don't get it. That code is not a function call and it is not c#. Regardless, best practice questions are better suited for codereview.stackexchange.com
    – Crowcoder
    Nov 22 at 16:40






  • 1




    I would actually recommend softwareengineering over codereview in this circumstance.
    – A Friend
    Nov 22 at 16:46














  • 2




    I don't get it. That code is not a function call and it is not c#. Regardless, best practice questions are better suited for codereview.stackexchange.com
    – Crowcoder
    Nov 22 at 16:40






  • 1




    I would actually recommend softwareengineering over codereview in this circumstance.
    – A Friend
    Nov 22 at 16:46








2




2




I don't get it. That code is not a function call and it is not c#. Regardless, best practice questions are better suited for codereview.stackexchange.com
– Crowcoder
Nov 22 at 16:40




I don't get it. That code is not a function call and it is not c#. Regardless, best practice questions are better suited for codereview.stackexchange.com
– Crowcoder
Nov 22 at 16:40




1




1




I would actually recommend softwareengineering over codereview in this circumstance.
– A Friend
Nov 22 at 16:46




I would actually recommend softwareengineering over codereview in this circumstance.
– A Friend
Nov 22 at 16:46












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










One neat way to write that is with a constructor in the SomeDirector class:



public SomeDirector(int value1, int value2)
{
this.SomeProperty = value1;
this.SomeProperty2 = value2;
}


then your code sample becomes:



var director = new SomeDirector(someValue, someValue2);


The idea here is that while the constructor looks a bit long-winded, it's tucked away in the class and the code that calls it is nice and concise (and you can't forget an important property).



In case you wondered, there's no C# equivalent of VB's with keyword.






share|improve this answer

















  • 1




    In the context they are using it, VB's With is equivalent to C#'s object initialisation.
    – A Friend
    Nov 22 at 16:45










  • Thanks Robin I will try this out right now and let you know how it goes!
    – Wonka Bear
    Nov 22 at 16:46


















up vote
1
down vote













As already mentioned, you can do something similar in C# with object initialisation:



var Director = new SomeDirector { SomeProperty = SomeValue, SomeProperty2 = SomeValue2 };


This does not require you to write an explicit constructor.






share|improve this answer




























    up vote
    0
    down vote













    More of a general answer to your question:
    In theory it doesnt matter how many lines of code you have, only how much resources it costs to execute. But best practices are also about human readability so it really depends on the situation. Do you need to optimize for performance or further development. This is something that in most cases you as a developer will know better than anyone else who dont have the full insight.






    share|improve this answer




























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      One neat way to write that is with a constructor in the SomeDirector class:



      public SomeDirector(int value1, int value2)
      {
      this.SomeProperty = value1;
      this.SomeProperty2 = value2;
      }


      then your code sample becomes:



      var director = new SomeDirector(someValue, someValue2);


      The idea here is that while the constructor looks a bit long-winded, it's tucked away in the class and the code that calls it is nice and concise (and you can't forget an important property).



      In case you wondered, there's no C# equivalent of VB's with keyword.






      share|improve this answer

















      • 1




        In the context they are using it, VB's With is equivalent to C#'s object initialisation.
        – A Friend
        Nov 22 at 16:45










      • Thanks Robin I will try this out right now and let you know how it goes!
        – Wonka Bear
        Nov 22 at 16:46















      up vote
      0
      down vote



      accepted










      One neat way to write that is with a constructor in the SomeDirector class:



      public SomeDirector(int value1, int value2)
      {
      this.SomeProperty = value1;
      this.SomeProperty2 = value2;
      }


      then your code sample becomes:



      var director = new SomeDirector(someValue, someValue2);


      The idea here is that while the constructor looks a bit long-winded, it's tucked away in the class and the code that calls it is nice and concise (and you can't forget an important property).



      In case you wondered, there's no C# equivalent of VB's with keyword.






      share|improve this answer

















      • 1




        In the context they are using it, VB's With is equivalent to C#'s object initialisation.
        – A Friend
        Nov 22 at 16:45










      • Thanks Robin I will try this out right now and let you know how it goes!
        – Wonka Bear
        Nov 22 at 16:46













      up vote
      0
      down vote



      accepted







      up vote
      0
      down vote



      accepted






      One neat way to write that is with a constructor in the SomeDirector class:



      public SomeDirector(int value1, int value2)
      {
      this.SomeProperty = value1;
      this.SomeProperty2 = value2;
      }


      then your code sample becomes:



      var director = new SomeDirector(someValue, someValue2);


      The idea here is that while the constructor looks a bit long-winded, it's tucked away in the class and the code that calls it is nice and concise (and you can't forget an important property).



      In case you wondered, there's no C# equivalent of VB's with keyword.






      share|improve this answer












      One neat way to write that is with a constructor in the SomeDirector class:



      public SomeDirector(int value1, int value2)
      {
      this.SomeProperty = value1;
      this.SomeProperty2 = value2;
      }


      then your code sample becomes:



      var director = new SomeDirector(someValue, someValue2);


      The idea here is that while the constructor looks a bit long-winded, it's tucked away in the class and the code that calls it is nice and concise (and you can't forget an important property).



      In case you wondered, there's no C# equivalent of VB's with keyword.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 22 at 16:43









      Robin Bennett

      1,397112




      1,397112








      • 1




        In the context they are using it, VB's With is equivalent to C#'s object initialisation.
        – A Friend
        Nov 22 at 16:45










      • Thanks Robin I will try this out right now and let you know how it goes!
        – Wonka Bear
        Nov 22 at 16:46














      • 1




        In the context they are using it, VB's With is equivalent to C#'s object initialisation.
        – A Friend
        Nov 22 at 16:45










      • Thanks Robin I will try this out right now and let you know how it goes!
        – Wonka Bear
        Nov 22 at 16:46








      1




      1




      In the context they are using it, VB's With is equivalent to C#'s object initialisation.
      – A Friend
      Nov 22 at 16:45




      In the context they are using it, VB's With is equivalent to C#'s object initialisation.
      – A Friend
      Nov 22 at 16:45












      Thanks Robin I will try this out right now and let you know how it goes!
      – Wonka Bear
      Nov 22 at 16:46




      Thanks Robin I will try this out right now and let you know how it goes!
      – Wonka Bear
      Nov 22 at 16:46












      up vote
      1
      down vote













      As already mentioned, you can do something similar in C# with object initialisation:



      var Director = new SomeDirector { SomeProperty = SomeValue, SomeProperty2 = SomeValue2 };


      This does not require you to write an explicit constructor.






      share|improve this answer

























        up vote
        1
        down vote













        As already mentioned, you can do something similar in C# with object initialisation:



        var Director = new SomeDirector { SomeProperty = SomeValue, SomeProperty2 = SomeValue2 };


        This does not require you to write an explicit constructor.






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          As already mentioned, you can do something similar in C# with object initialisation:



          var Director = new SomeDirector { SomeProperty = SomeValue, SomeProperty2 = SomeValue2 };


          This does not require you to write an explicit constructor.






          share|improve this answer












          As already mentioned, you can do something similar in C# with object initialisation:



          var Director = new SomeDirector { SomeProperty = SomeValue, SomeProperty2 = SomeValue2 };


          This does not require you to write an explicit constructor.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 16:54









          Polyfun

          7,51842736




          7,51842736






















              up vote
              0
              down vote













              More of a general answer to your question:
              In theory it doesnt matter how many lines of code you have, only how much resources it costs to execute. But best practices are also about human readability so it really depends on the situation. Do you need to optimize for performance or further development. This is something that in most cases you as a developer will know better than anyone else who dont have the full insight.






              share|improve this answer

























                up vote
                0
                down vote













                More of a general answer to your question:
                In theory it doesnt matter how many lines of code you have, only how much resources it costs to execute. But best practices are also about human readability so it really depends on the situation. Do you need to optimize for performance or further development. This is something that in most cases you as a developer will know better than anyone else who dont have the full insight.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  More of a general answer to your question:
                  In theory it doesnt matter how many lines of code you have, only how much resources it costs to execute. But best practices are also about human readability so it really depends on the situation. Do you need to optimize for performance or further development. This is something that in most cases you as a developer will know better than anyone else who dont have the full insight.






                  share|improve this answer












                  More of a general answer to your question:
                  In theory it doesnt matter how many lines of code you have, only how much resources it costs to execute. But best practices are also about human readability so it really depends on the situation. Do you need to optimize for performance or further development. This is something that in most cases you as a developer will know better than anyone else who dont have the full insight.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 16:49









                  heap1

                  598




                  598















                      Popular posts from this blog

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      Alexandru Averescu

                      Trompette piccolo