patterncsharpMinor
Divide List into groups
Viewed 0 times
dividelistgroupsinto
Problem
I have a set of Service, and I want to partition them into ServiceClass, and have a reference from a Service to the Service class its belong.
I am storing the reference in
In the code below, I want to split into five service classes. I feel the code below is not well written, such as the naming of variable, but I am not sure how to improve it, can anyone help?
I am storing the reference in
Dictionary.In the code below, I want to split into five service classes. I feel the code below is not well written, such as the naming of variable, but I am not sure how to improve it, can anyone help?
int group = 5;
List service = FileIO.ReadFeatureFile(@"D:\C Drive\Desktop\input.txt");
int numPerClass = service.Count / group;
Dictionary ServiceToServiceClass = new Dictionary();
for (int i = 0; i serviceGrp = service.GetRange(0, numPerClass);
ServiceClass sc=new ServiceClass(serviceGrp);
foreach(Service service1 in serviceGrp)
{
ServiceToServiceClass.Add(service1, sc);
}
service.RemoveRange(0, numPerClass);
}
ServiceClass sc1 = new ServiceClass(service);
foreach (Service service1 in service)
{
ServiceToServiceClass.Add(service1, sc1);
}Solution
First off, as dreza said, work on your variable names. Secondly, you should really think about using more than one method for solving these types of problems.
All of my suggestions are assuming this is inside a class meant to hold this information.
Now to your code.
The line int group = 5; should be a constant. I would also rename it to something like numberOfServiceGroups. So the code line looks like:
I would move the dictionary to a class variable and instantiate it in the constructor. I would also rename it to something like _serviceLookup. The _ signifying it is a class variable
Instead of using GetRange and RemoveRange, I would change it to use the linq statements Skip([int items]).Take([int items]) and because you are using a for loop, the Skip count can be calculated:
The nice thing about Take() is that it will not blow-up if you go over bounds, it will just return what is left. Doing this will eliminate the need to have the catch loop at the end of your method.
I would move the processing of the services into the group to another method. This reduces nesting.
So your method now looks like:
And your class:
This should be a good start. I see a few more things I'd like to do, but they are beyond this scope.
Also note, I have not tested this due to missing classes / information.
All of my suggestions are assuming this is inside a class meant to hold this information.
Now to your code.
The line int group = 5; should be a constant. I would also rename it to something like numberOfServiceGroups. So the code line looks like:
const int numberOfServiceGroups = 5;I would move the dictionary to a class variable and instantiate it in the constructor. I would also rename it to something like _serviceLookup. The _ signifying it is a class variable
Instead of using GetRange and RemoveRange, I would change it to use the linq statements Skip([int items]).Take([int items]) and because you are using a for loop, the Skip count can be calculated:
for (var i = 0; i < numberOfServiceGroups; i++)
{
var servicesToProcess = service.Skip(i * servicesPerGroup).Take(servicesPerGroup);
// More on this in a minute
}The nice thing about Take() is that it will not blow-up if you go over bounds, it will just return what is left. Doing this will eliminate the need to have the catch loop at the end of your method.
I would move the processing of the services into the group to another method. This reduces nesting.
private void ProcessGroup(IList services)
{
var serviceClass = new ServiceClass(services);
foreach (var currentService in services)
{
_serviceLookup.Add(currentService, serviceClass);
}
}So your method now looks like:
private void Group()
{
const int numberOfServiceGroups = 5;
List services = FileIO.ReadFeatureFile(@"D:\C Drive\Desktop\input.txt");
var servicesPerGroup = service.Count/numberOfServiceGroups;
var serviceToServiceClass = new Dictionary();
for (var i = 0; i < numberOfServiceGroups; i++)
{
var servicesToProcess = services.Skip(i * servicesPerGroup).Take(servicesPerGroup);
ProcessGroup(servicesToProcess.ToList());
}
}And your class:
public class ClassName
{
public IDictionary _serviceLookup;
public ClassName()
{
_serviceLookup = new Dictionary();
}
private void Group()
{
const int numberOfServiceGroups = 5;
List services = FileIO.ReadFeatureFile(@"D:\C Drive\Desktop\input.txt");
var servicesPerGroup = service.Count/numberOfServiceGroups;
for (var i = 0; i services)
{
var serviceClass = new ServiceClass(services);
foreach (var currentService in services)
{
_serviceLookup.Add(currentService, serviceClass);
}
}
}This should be a good start. I see a few more things I'd like to do, but they are beyond this scope.
Also note, I have not tested this due to missing classes / information.
Code Snippets
const int numberOfServiceGroups = 5;for (var i = 0; i < numberOfServiceGroups; i++)
{
var servicesToProcess = service.Skip(i * servicesPerGroup).Take(servicesPerGroup);
// More on this in a minute
}private void ProcessGroup(IList<Service> services)
{
var serviceClass = new ServiceClass(services);
foreach (var currentService in services)
{
_serviceLookup.Add(currentService, serviceClass);
}
}private void Group()
{
const int numberOfServiceGroups = 5;
List<Service> services = FileIO.ReadFeatureFile(@"D:\C Drive\Desktop\input.txt");
var servicesPerGroup = service.Count/numberOfServiceGroups;
var serviceToServiceClass = new Dictionary<Service, ServiceClass>();
for (var i = 0; i < numberOfServiceGroups; i++)
{
var servicesToProcess = services.Skip(i * servicesPerGroup).Take(servicesPerGroup);
ProcessGroup(servicesToProcess.ToList());
}
}public class ClassName
{
public IDictionary<Service, ServiceClass> _serviceLookup;
public ClassName()
{
_serviceLookup = new Dictionary<Service, ServiceClass>();
}
private void Group()
{
const int numberOfServiceGroups = 5;
List<Service> services = FileIO.ReadFeatureFile(@"D:\C Drive\Desktop\input.txt");
var servicesPerGroup = service.Count/numberOfServiceGroups;
for (var i = 0; i < numberOfServiceGroups; i++)
{
var servicesToProcess = services.Skip(i * servicesPerGroup).Take(servicesPerGroup);
ProcessGroup(servicesToProcess.ToList());
}
}
private void ProcessGroup(IList<Service> services)
{
var serviceClass = new ServiceClass(services);
foreach (var currentService in services)
{
_serviceLookup.Add(currentService, serviceClass);
}
}
}Context
StackExchange Code Review Q#15672, answer score: 5
Revisions (0)
No revisions yet.