|
-
Mar 8th, 2010, 06:24 PM
#1
Thread Starter
Hyperactive Member
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:
switch(variable)
{
case 1:
//Do something
break;
case 2:
//Do something
break;
case 3:
//Do something
break;
case 4:
//Do something
break;
}
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:
switch(variable)
{
case 1:
Window.Background = Image1;
break;
case 2:
Window.Background = Image2;
break;
case 3:
Window.Background = Image3;
break;
case 4:
Window.Background = Image4;
break;
//Dummy Code
}
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.
-
Mar 8th, 2010, 08:05 PM
#2
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.
-
Mar 8th, 2010, 08:13 PM
#3
Thread Starter
Hyperactive Member
Re: C# Code Quality
 Originally Posted by jmcilhinney
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?
-
Mar 8th, 2010, 08:22 PM
#4
Re: C# Code Quality
 Originally Posted by Pac_741
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.
-
Mar 8th, 2010, 08:38 PM
#5
Thread Starter
Hyperactive Member
Re: C# Code Quality
csharp Code:
void Initialize()
{
LoadData();
}
void LoadData()
{
//Get Data
SortData();
}
void PresentData()
{
//Send Data to GUI
}
void SortData()
{
//Sort Data
PresentData();
}
This piece of code applies the single responsibility principle, right ?
In few words the above piece of code performs better than this:
csharp Code:
void Initialize()
{
LoadData();
}
void LoadData()
{
//Get Data
//Sort Data
//Send Data to GUI
}
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 ?
-
Mar 8th, 2010, 08:54 PM
#6
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.
-
Mar 8th, 2010, 09:02 PM
#7
Thread Starter
Hyperactive Member
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.
-
Mar 8th, 2010, 09:16 PM
#8
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.
-
Mar 8th, 2010, 10:33 PM
#9
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|