|
There's nothing necessarily wrong with eight parameters for a constructor.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
..though I would be recommending to encapsulate them in a specific parameter-class and pass a single object
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Like a Tuple<,,,,,,,> ?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I'm no longer allowed to pass a Tuple-parameter after my last review
So I now create a specific object:
class MyList: List<Tuple<int, string, string>> { }
--
Joking aside, the <a href="https://msdn.microsoft.com/nl-nl/library/system.diagnostics.processstartinfo%28v=vs.110%29.aspx">ProcessStartInfo</a>[<a href="https://msdn.microsoft.com/nl-nl/library/system.diagnostics.processstartinfo%28v=vs.110%29.aspx" target="_blank" title="New Window">^</a>] class would be a nice example of limiting the amount of parameters required, while still keeping it readable.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I really think it depends on the requirements of the object and how it fits into the existing code's style.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
John Simmons / outlaw programmer wrote: I really think it depends on the requirements of the object and how it fits into the existing code's style. I think it depends on the amount of switches.
Easy example in the ProcessStartInfo[^] class. It does not depend on how it fits into the "style".
Show me a more readable version of the same using a constructor and a param for each switch, then I'll accept it may depend on requirements.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Matching the current coding style goes a long way towards "readability". Besides, I'm not here to argue semantics. I merely stated that there's nothing necessarily wrong with a constructor with eight parameters. There are often many more considerations than "I think this code sucks". I'm not really interested enough to list all the ones I can think of right off the top of my head.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
John Simmons / outlaw programmer wrote: Matching the current coding style goes a long way towards "readability". So you'd prefer to implement that same class as a constructor with 16 switches? Show me a "current coding style" where that is more readable than a single class as described on MSDN
John Simmons / outlaw programmer wrote: I'm not really interested enough to list all the ones I can think of right off the top of my head. I did not ask you to.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes! And you can try to provide alternative constructors with fewer parameters. Do you really always need 8 parameters?
Get me coffee and no one gets hurt!
|
|
|
|
|
Taking the same ProcessStartInfo class as an example. Yes, I need those 16 switches, and no, it would not become more readable if you provided an overloaded version of each possible combination.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
It's the only constructor and none of the parameters are optional...
Oh, and did I mention that the whole class has exactly 0 comments?
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
I've got a class with 12 constructor parameters but the class represents a single row of data from a database so it is a discrete object. I also did full xml comments on everything so I don't forget why I wrote it. Would that be flagged in a code review?
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
|
|
|
|
|
Foothill wrote: I've got a class with 12 constructor parameters but the class represents a single row of data from a database
I don't do that with column data anymore unless I need the object to be initialized a certain way. I just use object initializers.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
mav.northwind wrote: the whole class has exactly 0 comments That's exactly how much comments code should have!
Great code requires no comments.
Most comments are bad anyway. I'm so allergic to comments I even wrote a tip on why you shouldn't comment (and the rare case you should): Write comments that matter[^]
|
|
|
|
|
unless the OP means 8 overloaded constructors with different parameter signatures?
(in which case defaults could definitely help)
|
|
|
|
|
He never said anything about overloads, so I think it's safe to assume there is one constructor with eight parameters.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
he wrote a lot. Compacter is:
Users()
As master Yoda says: "Make it or not. Dont: try"
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
Not to make fun of your signature:
Quote: Black holes are the places where God divided by 0 But:
Yes! And the minds of some of the sunshines in Q&A are where He multiplied by zero!
Get me coffee and no one gets hurt!
modified 2-Apr-16 20:39pm.
|
|
|
|
|
public int val2
{
get
{
return val2;
}
set
{
val2 = 10;
}
}
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Maybe there was a requirement to not use const ?
|
|
|
|
|
No, the requirement was to create a public get/set property named Value2 for a private field named value2 and to use that property to set value2 to a value of 10 - but from outside the containing class..
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I'm surprised this code compiles, as there is a circular reference in the getter.
You always obtain more by being rather polite and armed than polite only.
|
|
|
|
|
Yep, the compiler will let you run into a SO-exception without turning a hair
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Alas it does, I've lost many an hour to it 
|
|
|
|