Refactoring the code and developing a clean code
I have a dto as follows:
public class DaysDetails
{
public bool Sun {get;set;}
public bool Mon {get;set;}
...
public bool Sat {get;set;} //All 7 days of the week
}
I have a method which checks if the days are checked and builds a comma separated string format . For eg: if sunday and monday are checked then the output is "0,1" (numbers corresponding to days)
pubic string ConstructDays(DaysDetails d)
{
StringBuilder constructDays = new StringBuilder();
if(d.Sun == true)
{
constructDays.Append("0");
}
if(d.Mon == true)
{
constructDays.Append("1");
}
..... //So on for all seven days
string day = Convert.toString(constructDays);
if(day != string.Empty && day[0] == ",")
day = day.Remove(0,1);
return day;
}
I need to convert this function to a more maintainable code and simplified version . What can be improved in this?
c# asp.net .net c#-4.0
|
show 1 more comment
I have a dto as follows:
public class DaysDetails
{
public bool Sun {get;set;}
public bool Mon {get;set;}
...
public bool Sat {get;set;} //All 7 days of the week
}
I have a method which checks if the days are checked and builds a comma separated string format . For eg: if sunday and monday are checked then the output is "0,1" (numbers corresponding to days)
pubic string ConstructDays(DaysDetails d)
{
StringBuilder constructDays = new StringBuilder();
if(d.Sun == true)
{
constructDays.Append("0");
}
if(d.Mon == true)
{
constructDays.Append("1");
}
..... //So on for all seven days
string day = Convert.toString(constructDays);
if(day != string.Empty && day[0] == ",")
day = day.Remove(0,1);
return day;
}
I need to convert this function to a more maintainable code and simplified version . What can be improved in this?
c# asp.net .net c#-4.0
To begin with, are you permitted to change DaysDetails ? I mean, why not use a Enum with Flags ?
– Anu Viswan
Jan 19 at 2:56
Please post code that compiles too, so someone can build on what you've got so far. There are several typos.
– User
Jan 19 at 2:59
you can use a List instead of a CSV field
– Josue Martinez
Jan 19 at 3:00
@anu : DayDetails is what I get from UI mapped fields . But yes , I am permitted to make use of any other variable derived from DayDetails and can be passed to ConstructDays function . Ultimately I need a comma separated string either from DayDetails or any custom variable passed to function.
– user1400915
Jan 19 at 3:05
1
I'm voting to close this question as off-topic because it is asking for a code review try Code Review
– Daniel A. White
Jan 19 at 3:38
|
show 1 more comment
I have a dto as follows:
public class DaysDetails
{
public bool Sun {get;set;}
public bool Mon {get;set;}
...
public bool Sat {get;set;} //All 7 days of the week
}
I have a method which checks if the days are checked and builds a comma separated string format . For eg: if sunday and monday are checked then the output is "0,1" (numbers corresponding to days)
pubic string ConstructDays(DaysDetails d)
{
StringBuilder constructDays = new StringBuilder();
if(d.Sun == true)
{
constructDays.Append("0");
}
if(d.Mon == true)
{
constructDays.Append("1");
}
..... //So on for all seven days
string day = Convert.toString(constructDays);
if(day != string.Empty && day[0] == ",")
day = day.Remove(0,1);
return day;
}
I need to convert this function to a more maintainable code and simplified version . What can be improved in this?
c# asp.net .net c#-4.0
I have a dto as follows:
public class DaysDetails
{
public bool Sun {get;set;}
public bool Mon {get;set;}
...
public bool Sat {get;set;} //All 7 days of the week
}
I have a method which checks if the days are checked and builds a comma separated string format . For eg: if sunday and monday are checked then the output is "0,1" (numbers corresponding to days)
pubic string ConstructDays(DaysDetails d)
{
StringBuilder constructDays = new StringBuilder();
if(d.Sun == true)
{
constructDays.Append("0");
}
if(d.Mon == true)
{
constructDays.Append("1");
}
..... //So on for all seven days
string day = Convert.toString(constructDays);
if(day != string.Empty && day[0] == ",")
day = day.Remove(0,1);
return day;
}
I need to convert this function to a more maintainable code and simplified version . What can be improved in this?
c# asp.net .net c#-4.0
c# asp.net .net c#-4.0
asked Jan 19 at 2:48
user1400915user1400915
92221544
92221544
To begin with, are you permitted to change DaysDetails ? I mean, why not use a Enum with Flags ?
– Anu Viswan
Jan 19 at 2:56
Please post code that compiles too, so someone can build on what you've got so far. There are several typos.
– User
Jan 19 at 2:59
you can use a List instead of a CSV field
– Josue Martinez
Jan 19 at 3:00
@anu : DayDetails is what I get from UI mapped fields . But yes , I am permitted to make use of any other variable derived from DayDetails and can be passed to ConstructDays function . Ultimately I need a comma separated string either from DayDetails or any custom variable passed to function.
– user1400915
Jan 19 at 3:05
1
I'm voting to close this question as off-topic because it is asking for a code review try Code Review
– Daniel A. White
Jan 19 at 3:38
|
show 1 more comment
To begin with, are you permitted to change DaysDetails ? I mean, why not use a Enum with Flags ?
– Anu Viswan
Jan 19 at 2:56
Please post code that compiles too, so someone can build on what you've got so far. There are several typos.
– User
Jan 19 at 2:59
you can use a List instead of a CSV field
– Josue Martinez
Jan 19 at 3:00
@anu : DayDetails is what I get from UI mapped fields . But yes , I am permitted to make use of any other variable derived from DayDetails and can be passed to ConstructDays function . Ultimately I need a comma separated string either from DayDetails or any custom variable passed to function.
– user1400915
Jan 19 at 3:05
1
I'm voting to close this question as off-topic because it is asking for a code review try Code Review
– Daniel A. White
Jan 19 at 3:38
To begin with, are you permitted to change DaysDetails ? I mean, why not use a Enum with Flags ?
– Anu Viswan
Jan 19 at 2:56
To begin with, are you permitted to change DaysDetails ? I mean, why not use a Enum with Flags ?
– Anu Viswan
Jan 19 at 2:56
Please post code that compiles too, so someone can build on what you've got so far. There are several typos.
– User
Jan 19 at 2:59
Please post code that compiles too, so someone can build on what you've got so far. There are several typos.
– User
Jan 19 at 2:59
you can use a List instead of a CSV field
– Josue Martinez
Jan 19 at 3:00
you can use a List instead of a CSV field
– Josue Martinez
Jan 19 at 3:00
@anu : DayDetails is what I get from UI mapped fields . But yes , I am permitted to make use of any other variable derived from DayDetails and can be passed to ConstructDays function . Ultimately I need a comma separated string either from DayDetails or any custom variable passed to function.
– user1400915
Jan 19 at 3:05
@anu : DayDetails is what I get from UI mapped fields . But yes , I am permitted to make use of any other variable derived from DayDetails and can be passed to ConstructDays function . Ultimately I need a comma separated string either from DayDetails or any custom variable passed to function.
– user1400915
Jan 19 at 3:05
1
1
I'm voting to close this question as off-topic because it is asking for a code review try Code Review
– Daniel A. White
Jan 19 at 3:38
I'm voting to close this question as off-topic because it is asking for a code review try Code Review
– Daniel A. White
Jan 19 at 3:38
|
show 1 more comment
4 Answers
4
active
oldest
votes
You can simplify your code by converting each bool to int and joining the resulting collection.
public class DaysDetails
{
public bool Sun { get; set; }
public bool Mon { get; set; }
public bool Sat { get; set; }
}
public string ConstructDays(DaysDetails d)
{
var week = new
{
Convert.ToInt32(d.Sat),
Convert.ToInt32(d.Sun),
Convert.ToInt32(d.Mon),
};
return string.Join(",", week);
}
Or if your looking for more than just 0/1:
public string ConstructDays(DaysDetails d)
{
var week = new
{
d.Sat ? 0 : -1,
d.Sun ? 1 : -1,
d.Mon ? 2 : -1,
//...//
}.Where(x => x != -1);
return string.Join(",", week);
}
add a comment |
Define a flag enum to store your values:
[Flags]
public enum Days
{
None = 0,
Sun = 1, // 0
Mon = 2, // 1
Tue = 4, // 2
Wed = 8, // 3
Thu = 16, // 4
Fri = 32, // 5
Sat = 64 // 6
}
You can set the selected days like this:
var days = Days.None;
if (some condition)
days |= Days.Mon;
if (some other condition)
days |= Days.Wed;
if (yet another condition)
days |= Days.Sat;
And generate values based on which flags are set like so:
static public string ConstructDays(Days days)
{
return string.Join(",", Enum.GetValues(typeof(Days))
.Cast<Days>()
.Where(d => days.HasFlag(d) && d != Days.None)
.Select(d => Math.Log((int)d, 2))); // 1,3,6
}
add a comment |
Iterate through the classe properties like :
pubic string ConstructDays(DaysDetails d)
{
int Idx = 0;
string days = "";
var obj = new DaysDetails ();
foreach (var p in obj .GetType().GetProperties())
{ days += (bool)p.GetValue(obj ) ? (days=="" ? Idx.ToString() : ","+Idx.ToString()) : "";
Idx++;
}
return days
}
add a comment |
I would suggest two things: make a separate method for converting a boolean to a int representation, and override the ToString method instead of generating a separate ConstructDays method.
public class DaysDetails
{
public bool Sun {get;set;}
public bool Mon {get;set;}
...
public bool Sat {get;set;} //All 7 days of the week
public override string ToString() {
//formatted string
return $"{GetNumberRepresentationOfBool(Sun)},{GetNumberRepresentationOfBool(Mon)},{GetNumberRepresentationOfBool(Sat)}"
}
}
public int GetNumberRepresentationOfBool(bool value) {
return value ? 1 : 0
}
//printing the value
Console.WriteLine(dayDetailsObject.ToString());
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%2f54263672%2frefactoring-the-code-and-developing-a-clean-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can simplify your code by converting each bool to int and joining the resulting collection.
public class DaysDetails
{
public bool Sun { get; set; }
public bool Mon { get; set; }
public bool Sat { get; set; }
}
public string ConstructDays(DaysDetails d)
{
var week = new
{
Convert.ToInt32(d.Sat),
Convert.ToInt32(d.Sun),
Convert.ToInt32(d.Mon),
};
return string.Join(",", week);
}
Or if your looking for more than just 0/1:
public string ConstructDays(DaysDetails d)
{
var week = new
{
d.Sat ? 0 : -1,
d.Sun ? 1 : -1,
d.Mon ? 2 : -1,
//...//
}.Where(x => x != -1);
return string.Join(",", week);
}
add a comment |
You can simplify your code by converting each bool to int and joining the resulting collection.
public class DaysDetails
{
public bool Sun { get; set; }
public bool Mon { get; set; }
public bool Sat { get; set; }
}
public string ConstructDays(DaysDetails d)
{
var week = new
{
Convert.ToInt32(d.Sat),
Convert.ToInt32(d.Sun),
Convert.ToInt32(d.Mon),
};
return string.Join(",", week);
}
Or if your looking for more than just 0/1:
public string ConstructDays(DaysDetails d)
{
var week = new
{
d.Sat ? 0 : -1,
d.Sun ? 1 : -1,
d.Mon ? 2 : -1,
//...//
}.Where(x => x != -1);
return string.Join(",", week);
}
add a comment |
You can simplify your code by converting each bool to int and joining the resulting collection.
public class DaysDetails
{
public bool Sun { get; set; }
public bool Mon { get; set; }
public bool Sat { get; set; }
}
public string ConstructDays(DaysDetails d)
{
var week = new
{
Convert.ToInt32(d.Sat),
Convert.ToInt32(d.Sun),
Convert.ToInt32(d.Mon),
};
return string.Join(",", week);
}
Or if your looking for more than just 0/1:
public string ConstructDays(DaysDetails d)
{
var week = new
{
d.Sat ? 0 : -1,
d.Sun ? 1 : -1,
d.Mon ? 2 : -1,
//...//
}.Where(x => x != -1);
return string.Join(",", week);
}
You can simplify your code by converting each bool to int and joining the resulting collection.
public class DaysDetails
{
public bool Sun { get; set; }
public bool Mon { get; set; }
public bool Sat { get; set; }
}
public string ConstructDays(DaysDetails d)
{
var week = new
{
Convert.ToInt32(d.Sat),
Convert.ToInt32(d.Sun),
Convert.ToInt32(d.Mon),
};
return string.Join(",", week);
}
Or if your looking for more than just 0/1:
public string ConstructDays(DaysDetails d)
{
var week = new
{
d.Sat ? 0 : -1,
d.Sun ? 1 : -1,
d.Mon ? 2 : -1,
//...//
}.Where(x => x != -1);
return string.Join(",", week);
}
edited Jan 19 at 3:34
answered Jan 19 at 3:22
JStewardJSteward
3,6982717
3,6982717
add a comment |
add a comment |
Define a flag enum to store your values:
[Flags]
public enum Days
{
None = 0,
Sun = 1, // 0
Mon = 2, // 1
Tue = 4, // 2
Wed = 8, // 3
Thu = 16, // 4
Fri = 32, // 5
Sat = 64 // 6
}
You can set the selected days like this:
var days = Days.None;
if (some condition)
days |= Days.Mon;
if (some other condition)
days |= Days.Wed;
if (yet another condition)
days |= Days.Sat;
And generate values based on which flags are set like so:
static public string ConstructDays(Days days)
{
return string.Join(",", Enum.GetValues(typeof(Days))
.Cast<Days>()
.Where(d => days.HasFlag(d) && d != Days.None)
.Select(d => Math.Log((int)d, 2))); // 1,3,6
}
add a comment |
Define a flag enum to store your values:
[Flags]
public enum Days
{
None = 0,
Sun = 1, // 0
Mon = 2, // 1
Tue = 4, // 2
Wed = 8, // 3
Thu = 16, // 4
Fri = 32, // 5
Sat = 64 // 6
}
You can set the selected days like this:
var days = Days.None;
if (some condition)
days |= Days.Mon;
if (some other condition)
days |= Days.Wed;
if (yet another condition)
days |= Days.Sat;
And generate values based on which flags are set like so:
static public string ConstructDays(Days days)
{
return string.Join(",", Enum.GetValues(typeof(Days))
.Cast<Days>()
.Where(d => days.HasFlag(d) && d != Days.None)
.Select(d => Math.Log((int)d, 2))); // 1,3,6
}
add a comment |
Define a flag enum to store your values:
[Flags]
public enum Days
{
None = 0,
Sun = 1, // 0
Mon = 2, // 1
Tue = 4, // 2
Wed = 8, // 3
Thu = 16, // 4
Fri = 32, // 5
Sat = 64 // 6
}
You can set the selected days like this:
var days = Days.None;
if (some condition)
days |= Days.Mon;
if (some other condition)
days |= Days.Wed;
if (yet another condition)
days |= Days.Sat;
And generate values based on which flags are set like so:
static public string ConstructDays(Days days)
{
return string.Join(",", Enum.GetValues(typeof(Days))
.Cast<Days>()
.Where(d => days.HasFlag(d) && d != Days.None)
.Select(d => Math.Log((int)d, 2))); // 1,3,6
}
Define a flag enum to store your values:
[Flags]
public enum Days
{
None = 0,
Sun = 1, // 0
Mon = 2, // 1
Tue = 4, // 2
Wed = 8, // 3
Thu = 16, // 4
Fri = 32, // 5
Sat = 64 // 6
}
You can set the selected days like this:
var days = Days.None;
if (some condition)
days |= Days.Mon;
if (some other condition)
days |= Days.Wed;
if (yet another condition)
days |= Days.Sat;
And generate values based on which flags are set like so:
static public string ConstructDays(Days days)
{
return string.Join(",", Enum.GetValues(typeof(Days))
.Cast<Days>()
.Where(d => days.HasFlag(d) && d != Days.None)
.Select(d => Math.Log((int)d, 2))); // 1,3,6
}
answered Jan 19 at 3:22
UserUser
54.6k676124
54.6k676124
add a comment |
add a comment |
Iterate through the classe properties like :
pubic string ConstructDays(DaysDetails d)
{
int Idx = 0;
string days = "";
var obj = new DaysDetails ();
foreach (var p in obj .GetType().GetProperties())
{ days += (bool)p.GetValue(obj ) ? (days=="" ? Idx.ToString() : ","+Idx.ToString()) : "";
Idx++;
}
return days
}
add a comment |
Iterate through the classe properties like :
pubic string ConstructDays(DaysDetails d)
{
int Idx = 0;
string days = "";
var obj = new DaysDetails ();
foreach (var p in obj .GetType().GetProperties())
{ days += (bool)p.GetValue(obj ) ? (days=="" ? Idx.ToString() : ","+Idx.ToString()) : "";
Idx++;
}
return days
}
add a comment |
Iterate through the classe properties like :
pubic string ConstructDays(DaysDetails d)
{
int Idx = 0;
string days = "";
var obj = new DaysDetails ();
foreach (var p in obj .GetType().GetProperties())
{ days += (bool)p.GetValue(obj ) ? (days=="" ? Idx.ToString() : ","+Idx.ToString()) : "";
Idx++;
}
return days
}
Iterate through the classe properties like :
pubic string ConstructDays(DaysDetails d)
{
int Idx = 0;
string days = "";
var obj = new DaysDetails ();
foreach (var p in obj .GetType().GetProperties())
{ days += (bool)p.GetValue(obj ) ? (days=="" ? Idx.ToString() : ","+Idx.ToString()) : "";
Idx++;
}
return days
}
edited Jan 19 at 3:27
answered Jan 19 at 3:22
lagripelagripe
375114
375114
add a comment |
add a comment |
I would suggest two things: make a separate method for converting a boolean to a int representation, and override the ToString method instead of generating a separate ConstructDays method.
public class DaysDetails
{
public bool Sun {get;set;}
public bool Mon {get;set;}
...
public bool Sat {get;set;} //All 7 days of the week
public override string ToString() {
//formatted string
return $"{GetNumberRepresentationOfBool(Sun)},{GetNumberRepresentationOfBool(Mon)},{GetNumberRepresentationOfBool(Sat)}"
}
}
public int GetNumberRepresentationOfBool(bool value) {
return value ? 1 : 0
}
//printing the value
Console.WriteLine(dayDetailsObject.ToString());
add a comment |
I would suggest two things: make a separate method for converting a boolean to a int representation, and override the ToString method instead of generating a separate ConstructDays method.
public class DaysDetails
{
public bool Sun {get;set;}
public bool Mon {get;set;}
...
public bool Sat {get;set;} //All 7 days of the week
public override string ToString() {
//formatted string
return $"{GetNumberRepresentationOfBool(Sun)},{GetNumberRepresentationOfBool(Mon)},{GetNumberRepresentationOfBool(Sat)}"
}
}
public int GetNumberRepresentationOfBool(bool value) {
return value ? 1 : 0
}
//printing the value
Console.WriteLine(dayDetailsObject.ToString());
add a comment |
I would suggest two things: make a separate method for converting a boolean to a int representation, and override the ToString method instead of generating a separate ConstructDays method.
public class DaysDetails
{
public bool Sun {get;set;}
public bool Mon {get;set;}
...
public bool Sat {get;set;} //All 7 days of the week
public override string ToString() {
//formatted string
return $"{GetNumberRepresentationOfBool(Sun)},{GetNumberRepresentationOfBool(Mon)},{GetNumberRepresentationOfBool(Sat)}"
}
}
public int GetNumberRepresentationOfBool(bool value) {
return value ? 1 : 0
}
//printing the value
Console.WriteLine(dayDetailsObject.ToString());
I would suggest two things: make a separate method for converting a boolean to a int representation, and override the ToString method instead of generating a separate ConstructDays method.
public class DaysDetails
{
public bool Sun {get;set;}
public bool Mon {get;set;}
...
public bool Sat {get;set;} //All 7 days of the week
public override string ToString() {
//formatted string
return $"{GetNumberRepresentationOfBool(Sun)},{GetNumberRepresentationOfBool(Mon)},{GetNumberRepresentationOfBool(Sat)}"
}
}
public int GetNumberRepresentationOfBool(bool value) {
return value ? 1 : 0
}
//printing the value
Console.WriteLine(dayDetailsObject.ToString());
answered Jan 19 at 6:21
izzy255izzy255
233
233
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%2f54263672%2frefactoring-the-code-and-developing-a-clean-code%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
To begin with, are you permitted to change DaysDetails ? I mean, why not use a Enum with Flags ?
– Anu Viswan
Jan 19 at 2:56
Please post code that compiles too, so someone can build on what you've got so far. There are several typos.
– User
Jan 19 at 2:59
you can use a List instead of a CSV field
– Josue Martinez
Jan 19 at 3:00
@anu : DayDetails is what I get from UI mapped fields . But yes , I am permitted to make use of any other variable derived from DayDetails and can be passed to ConstructDays function . Ultimately I need a comma separated string either from DayDetails or any custom variable passed to function.
– user1400915
Jan 19 at 3:05
1
I'm voting to close this question as off-topic because it is asking for a code review try Code Review
– Daniel A. White
Jan 19 at 3:38