Results 1 to 9 of 9

Thread: C# Code Quality

  1. #1

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    C# Code Quality

    Hello folks, I'm an active C# & WPF developer. One of my main goal is to achieve high standards in software development. I'm 16 years old so I have a long way to go in this industry.

    While I'm writing C# code I feel I'm doing something wrong,perhaps sloppy.

    csharp Code:
    1. switch(variable)
    2. {
    3.   case 1:
    4.   //Do something
    5.   break;
    6.   case 2:
    7.   //Do something
    8.   break;
    9.   case 3:
    10.   //Do something
    11.   break;
    12.   case 4:
    13.   //Do something
    14.   break;
    15. }

    I've encountered myself with situations where I have to process a good amount of possible scenarios within my application. I check those using switch&case statements.

    But when those scenarios are similar to each other, I just copy and paste the code I wrote the first according to x scenario, and adjust it to y scenario.

    and example would be:

    csharp Code:
    1. switch(variable)
    2. {
    3.   case 1:
    4.   Window.Background = Image1;
    5.   break;
    6.   case 2:
    7.   Window.Background = Image2;
    8.   break;
    9.   case 3:
    10.   Window.Background = Image3;
    11.   break;
    12.   case 4:
    13.   Window.Background = Image4;
    14.   break;
    15.   //Dummy Code
    16. }

    That's seems OK to me, but what if I have to process a fair amount of code within each case. I've come out with case statements containing 20 lines of code each & 8 cases.
    I feel doing that is a sloppy way to solve the problem.

    Could anyone advise me on this issue I've encountered myself. Also could anyone provide me with some c# best practices books or articles.

    Thanks in advance.
    C# and WPF developer
    My Website:
    http://singlebits.com/

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: C# Code Quality

    As you say, in the simple case you've shown there's not really any other way that is significantly. If there's a fair bit of work to do in each case though, I would suggest one of two options:

    1. Set a bunch of variables inside each case and then use those variable values once only after the switch block.
    2. Write a method to do the work and call that inside each case block.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: C# Code Quality

    Quote Originally Posted by jmcilhinney View Post
    As you say, in the simple case you've shown there's not really any other way that is significantly. If there's a fair bit of work to do in each case though, I would suggest one of two options:

    1. Set a bunch of variables inside each case and then use those variable values once only after the switch block.
    2. Write a method to do the work and call that inside each case block.
    Thanks for your response, I think option number two would work for me.

    I read somewhere in the internet each sub routine shouldn't be longer than.....(I don't recall how many lines) Does this is considered to be a good coding standard?
    C# and WPF developer
    My Website:
    http://singlebits.com/

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: C# Code Quality

    Quote Originally Posted by Pac_741 View Post
    Thanks for your response, I think option number two would work for me.

    I read somewhere in the internet each sub routine shouldn't be longer than.....(I don't recall how many lines) Does this is considered to be a good coding standard?
    That quote normally contains the number 20 when I've seen it. It's not a bad rule of thumb. The main thing is to maintain the single responsibility principle, i.e. each method is responsible for performing a single action. Exactly what constitutes a single action is open to interpretation but, generally, if you go beyond about 20 lines, it's most likely that you've gone beyond a single action. Keep in mind that, in many cases, that single action may be to aggregate several other actions, which means that you would have one method doing nothing more than call several other methods. Getting the balance right takes a bit of practice but it's good to have something to aim for.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: C# Code Quality

    csharp Code:
    1. void Initialize()
    2. {
    3.   LoadData();
    4. }
    5. void LoadData()
    6. {
    7.   //Get Data
    8.  
    9.   SortData();
    10. }
    11. void PresentData()
    12. {
    13.   //Send Data to GUI  
    14. }
    15. void SortData()
    16. {
    17.   //Sort Data
    18.   PresentData();
    19. }

    This piece of code applies the single responsibility principle, right ?

    In few words the above piece of code performs better than this:

    csharp Code:
    1. void Initialize()
    2. {
    3.   LoadData();
    4. }
    5. void LoadData()
    6. {
    7.   //Get Data
    8.   //Sort Data
    9.   //Send Data to GUI  
    10. }

    In my opinion coding standards and best practices are matter of human reading and comprehension, because the machine will perform the same output in both pieces of code above.

    Which is a good thing I would say, If developers know in a 100% their code and it's structure, it will lead better performance for the software itself.

    I'm I wrong ?
    C# and WPF developer
    My Website:
    http://singlebits.com/

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: C# Code Quality

    Well-written code can often perform less than optimally because each method call incurs a performance penalty. Particularly on modern hardware though, that penalty is negligible. You shouldn't introduce major performance issues into your app but your main priority when writing code should be maintainability. This often requires you to spend more time writing the code in the first place but you will reap the benefits in the long run.

    In your second example above, if all the Initialize method does is call the LoadData method then having both is probably pointless. Separating the getting, sorting and presenting responsibilities is definitely a good idea though. I don't really see that it would be the responsibility of the SortData method to call PresentData though. Sorting and presentation are not related. In your first example above, you shouldn't really be chaining the methods like that because you're breaking the single responsibility principle. The LoadData, SortData and PresentData should fulfil the single responsibilities implied by their names. The Initialize method should have the single responsibility of coordinating the initialisation. That means that Initialize calls LoadData and LoadData returns the raw data. Initialize then calls SortData and SortData returns the sorted data. Initialize then calls PresentData and the data gets presented. The three worker methods should have to know anything about each other. The fewer interdependencies the better.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: C# Code Quality

    The reason I'm asking all this questions is because I'd like to start becoming a more advanced programmer in programming concepts knowledge & coding standards and performance. As software industry continues growing the last two points will be fundamental in this industry.

    Thanks for your time & kind responses.
    C# and WPF developer
    My Website:
    http://singlebits.com/

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: C# Code Quality

    I applaud your ambition. It's a far better idea to learn to code well as you learn to code because unlearning bad habits is never easy.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: C# Code Quality

    In reference to each method call incurring a performance penalty. Modern compilers are clever and might well decide to compile several methods inline as well as making other optimizations. I more familiar with the GCC and VS6 compilers than I am with Net but I'm sure it is similar in its optimization even if it does just generate bytecode.

    In other words breaking up long inline code into smaller chunks might not even have any performance penalties.
    W o t . S i g

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