Generalizing the conditions after splitting of a string
Consider I have a cron expression as "* * */2 * " I have to extract the recurrence on days ,hours and minutes if it is not "" .
I have the below function which works as well.
public void CronDivision(string cron)
{
string days, value;
string cronDiv = cron.split(' ');
if(cronDiv[2] != "*")
{
days = "Days"
value = cronDiv[2].split('/')[1];
}
else if(cron[1] != "*)
{
//Similar logic
}
else if(cron[0] != "*)
{
//Similar logic
}
else if(cron[3] != "*)
{
//different logic for extracting weeks
}
else if(cron[4] != "*)
{
//different logic for extracting years
}
}
How do I generalize the above function so that I dont want to use many if else/switch statements with hardcoded numbers like1,2,3 and so on after the division of cron expression.
Can it be made more readable without the obvious solution of splitting the actual cron expression and extracting it?
c# asp.net .net c#-4.0
add a comment |
Consider I have a cron expression as "* * */2 * " I have to extract the recurrence on days ,hours and minutes if it is not "" .
I have the below function which works as well.
public void CronDivision(string cron)
{
string days, value;
string cronDiv = cron.split(' ');
if(cronDiv[2] != "*")
{
days = "Days"
value = cronDiv[2].split('/')[1];
}
else if(cron[1] != "*)
{
//Similar logic
}
else if(cron[0] != "*)
{
//Similar logic
}
else if(cron[3] != "*)
{
//different logic for extracting weeks
}
else if(cron[4] != "*)
{
//different logic for extracting years
}
}
How do I generalize the above function so that I dont want to use many if else/switch statements with hardcoded numbers like1,2,3 and so on after the division of cron expression.
Can it be made more readable without the obvious solution of splitting the actual cron expression and extracting it?
c# asp.net .net c#-4.0
Out of curiosity, why is this if-elseif construct, rather than if-if. You could have run into situations where expression could have values for more than one part. say "40 9 * * 1"
– Anu Viswan
Jan 20 at 3:59
@anu : considering the cron is right( I have customized where only either hours or minutes or days exist), I am interested in refactoring after division and extraction .I am concerned about the readability to improvise the code so as to remove hardcoded values as 0 th part,1st part etc.
– user1907849
Jan 20 at 4:27
add a comment |
Consider I have a cron expression as "* * */2 * " I have to extract the recurrence on days ,hours and minutes if it is not "" .
I have the below function which works as well.
public void CronDivision(string cron)
{
string days, value;
string cronDiv = cron.split(' ');
if(cronDiv[2] != "*")
{
days = "Days"
value = cronDiv[2].split('/')[1];
}
else if(cron[1] != "*)
{
//Similar logic
}
else if(cron[0] != "*)
{
//Similar logic
}
else if(cron[3] != "*)
{
//different logic for extracting weeks
}
else if(cron[4] != "*)
{
//different logic for extracting years
}
}
How do I generalize the above function so that I dont want to use many if else/switch statements with hardcoded numbers like1,2,3 and so on after the division of cron expression.
Can it be made more readable without the obvious solution of splitting the actual cron expression and extracting it?
c# asp.net .net c#-4.0
Consider I have a cron expression as "* * */2 * " I have to extract the recurrence on days ,hours and minutes if it is not "" .
I have the below function which works as well.
public void CronDivision(string cron)
{
string days, value;
string cronDiv = cron.split(' ');
if(cronDiv[2] != "*")
{
days = "Days"
value = cronDiv[2].split('/')[1];
}
else if(cron[1] != "*)
{
//Similar logic
}
else if(cron[0] != "*)
{
//Similar logic
}
else if(cron[3] != "*)
{
//different logic for extracting weeks
}
else if(cron[4] != "*)
{
//different logic for extracting years
}
}
How do I generalize the above function so that I dont want to use many if else/switch statements with hardcoded numbers like1,2,3 and so on after the division of cron expression.
Can it be made more readable without the obvious solution of splitting the actual cron expression and extracting it?
c# asp.net .net c#-4.0
c# asp.net .net c#-4.0
edited Jan 20 at 3:35
user1907849
asked Jan 20 at 3:17
user1907849user1907849
41221132
41221132
Out of curiosity, why is this if-elseif construct, rather than if-if. You could have run into situations where expression could have values for more than one part. say "40 9 * * 1"
– Anu Viswan
Jan 20 at 3:59
@anu : considering the cron is right( I have customized where only either hours or minutes or days exist), I am interested in refactoring after division and extraction .I am concerned about the readability to improvise the code so as to remove hardcoded values as 0 th part,1st part etc.
– user1907849
Jan 20 at 4:27
add a comment |
Out of curiosity, why is this if-elseif construct, rather than if-if. You could have run into situations where expression could have values for more than one part. say "40 9 * * 1"
– Anu Viswan
Jan 20 at 3:59
@anu : considering the cron is right( I have customized where only either hours or minutes or days exist), I am interested in refactoring after division and extraction .I am concerned about the readability to improvise the code so as to remove hardcoded values as 0 th part,1st part etc.
– user1907849
Jan 20 at 4:27
Out of curiosity, why is this if-elseif construct, rather than if-if. You could have run into situations where expression could have values for more than one part. say "40 9 * * 1"
– Anu Viswan
Jan 20 at 3:59
Out of curiosity, why is this if-elseif construct, rather than if-if. You could have run into situations where expression could have values for more than one part. say "40 9 * * 1"
– Anu Viswan
Jan 20 at 3:59
@anu : considering the cron is right( I have customized where only either hours or minutes or days exist), I am interested in refactoring after division and extraction .I am concerned about the readability to improvise the code so as to remove hardcoded values as 0 th part,1st part etc.
– user1907849
Jan 20 at 4:27
@anu : considering the cron is right( I have customized where only either hours or minutes or days exist), I am interested in refactoring after division and extraction .I am concerned about the readability to improvise the code so as to remove hardcoded values as 0 th part,1st part etc.
– user1907849
Jan 20 at 4:27
add a comment |
1 Answer
1
active
oldest
votes
Based on your comments that you do not expect more than one condition to happen, you can avoid explicit indices by using Enum. The If/Switch cases can be replaced with a dictionary, which maps indices(or better Enum) to Actions(that needs to be performed for each case).
For example,
var cron = "* * */2 * ";
var arr = cron.Split(new{" "},StringSplitOptions.RemoveEmptyEntries);
var value = arr.Select((x,index)=>new {Value=x, Index=index}).First(x=>!x.Value.Equals("*"));
var dictionaryOfActions = new Dictionary<Position,Action>
{
[Position.Minute] = ()=> ProcessMinute(),
[Position.Hour] = ()=> ProcessHour(),
[Position.Day] = ()=> ProcessDay(),
};
dictionaryOfActions[(Position)value.Index]();
Where Position is defined as
public enum Position
{
Minute,
Hour,
Day
}
and ProcessMinute,ProcessHour etc represents actions that needs to be performed on each conditions.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54273301%2fgeneralizing-the-conditions-after-splitting-of-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Based on your comments that you do not expect more than one condition to happen, you can avoid explicit indices by using Enum. The If/Switch cases can be replaced with a dictionary, which maps indices(or better Enum) to Actions(that needs to be performed for each case).
For example,
var cron = "* * */2 * ";
var arr = cron.Split(new{" "},StringSplitOptions.RemoveEmptyEntries);
var value = arr.Select((x,index)=>new {Value=x, Index=index}).First(x=>!x.Value.Equals("*"));
var dictionaryOfActions = new Dictionary<Position,Action>
{
[Position.Minute] = ()=> ProcessMinute(),
[Position.Hour] = ()=> ProcessHour(),
[Position.Day] = ()=> ProcessDay(),
};
dictionaryOfActions[(Position)value.Index]();
Where Position is defined as
public enum Position
{
Minute,
Hour,
Day
}
and ProcessMinute,ProcessHour etc represents actions that needs to be performed on each conditions.
add a comment |
Based on your comments that you do not expect more than one condition to happen, you can avoid explicit indices by using Enum. The If/Switch cases can be replaced with a dictionary, which maps indices(or better Enum) to Actions(that needs to be performed for each case).
For example,
var cron = "* * */2 * ";
var arr = cron.Split(new{" "},StringSplitOptions.RemoveEmptyEntries);
var value = arr.Select((x,index)=>new {Value=x, Index=index}).First(x=>!x.Value.Equals("*"));
var dictionaryOfActions = new Dictionary<Position,Action>
{
[Position.Minute] = ()=> ProcessMinute(),
[Position.Hour] = ()=> ProcessHour(),
[Position.Day] = ()=> ProcessDay(),
};
dictionaryOfActions[(Position)value.Index]();
Where Position is defined as
public enum Position
{
Minute,
Hour,
Day
}
and ProcessMinute,ProcessHour etc represents actions that needs to be performed on each conditions.
add a comment |
Based on your comments that you do not expect more than one condition to happen, you can avoid explicit indices by using Enum. The If/Switch cases can be replaced with a dictionary, which maps indices(or better Enum) to Actions(that needs to be performed for each case).
For example,
var cron = "* * */2 * ";
var arr = cron.Split(new{" "},StringSplitOptions.RemoveEmptyEntries);
var value = arr.Select((x,index)=>new {Value=x, Index=index}).First(x=>!x.Value.Equals("*"));
var dictionaryOfActions = new Dictionary<Position,Action>
{
[Position.Minute] = ()=> ProcessMinute(),
[Position.Hour] = ()=> ProcessHour(),
[Position.Day] = ()=> ProcessDay(),
};
dictionaryOfActions[(Position)value.Index]();
Where Position is defined as
public enum Position
{
Minute,
Hour,
Day
}
and ProcessMinute,ProcessHour etc represents actions that needs to be performed on each conditions.
Based on your comments that you do not expect more than one condition to happen, you can avoid explicit indices by using Enum. The If/Switch cases can be replaced with a dictionary, which maps indices(or better Enum) to Actions(that needs to be performed for each case).
For example,
var cron = "* * */2 * ";
var arr = cron.Split(new{" "},StringSplitOptions.RemoveEmptyEntries);
var value = arr.Select((x,index)=>new {Value=x, Index=index}).First(x=>!x.Value.Equals("*"));
var dictionaryOfActions = new Dictionary<Position,Action>
{
[Position.Minute] = ()=> ProcessMinute(),
[Position.Hour] = ()=> ProcessHour(),
[Position.Day] = ()=> ProcessDay(),
};
dictionaryOfActions[(Position)value.Index]();
Where Position is defined as
public enum Position
{
Minute,
Hour,
Day
}
and ProcessMinute,ProcessHour etc represents actions that needs to be performed on each conditions.
edited Jan 20 at 9:32
answered Jan 20 at 5:31
Anu ViswanAnu Viswan
5,0482525
5,0482525
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54273301%2fgeneralizing-the-conditions-after-splitting-of-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Out of curiosity, why is this if-elseif construct, rather than if-if. You could have run into situations where expression could have values for more than one part. say "40 9 * * 1"
– Anu Viswan
Jan 20 at 3:59
@anu : considering the cron is right( I have customized where only either hours or minutes or days exist), I am interested in refactoring after division and extraction .I am concerned about the readability to improvise the code so as to remove hardcoded values as 0 th part,1st part etc.
– user1907849
Jan 20 at 4:27