Questions tagged [construction]

19 questions
32
votes
6 answers

Create new object or reset every property?

public class MyClass { public object Prop1 { get; set; } public object Prop2 { get; set; } public object Prop3 { get; set; } } Suppose I have an object myObject of MyClass and I need to reset its properties, is it…
Bells
  • 433
  • 1
  • 4
  • 6
31
votes
3 answers

Is a "start", "run" or "execute" method a good practice?

I'm currently working on a code base that has many classes that implement a Start method. This seems like two-phase construction to me, which I had always considered a bad practice. I can't tell the difference between this and a constructor. When…
Dave Hillier
  • 3,940
  • 1
  • 25
  • 37
13
votes
5 answers

How does the concept of a class change when passing data to the constructor instead of method parameters?

Let's say we're making a parser. One implementation could be: public sealed class Parser1 { public string Parse(string text) { ... } } Or we could pass the text to the constructor instead: public sealed class Parser2 { public…
ciscoheat
  • 240
  • 2
  • 7
10
votes
3 answers

Is relying on implicit argument conversion considered dangerous?

C++ has a feature (I cannot figure out the proper name of it), that automatically calls matching constructors of parameter types if the argument types are not the expected ones. A very basic example of this is calling a function that expects a…
futlib
  • 2,107
  • 3
  • 21
  • 26
7
votes
4 answers

Separation of construction and initialization

I'm confused by this post by Mark Seeman. And his comment on IInitializable below: The problem with an Initialize method is the same as with Property Injection (A.K.A. Setter Injection): it creates a temporal coupling between the Initialize…
6
votes
1 answer

Dependency injection: what belongs in the constructor?

I'm evaluating my current PHP practices in an effort to write more testable code. Generally speaking, I'm fishing for opinions on what types of actions belong in the constructor. Should I limit things to dependency injection? If I do have some data…
Annika Backstrom
  • 707
  • 4
  • 13
4
votes
2 answers

How to separate construction and runtime logic in iOS?

I've been reading Robert Martin's Clean Code book where it suggests we should separate a programs startup/construction process from its run time logic. In Java (the language the book uses) this involves moving all aspects of construction to main and…
Declan McKenna
  • 476
  • 4
  • 12
3
votes
1 answer

Testing a class with "prerequisite" methods

I'm working on a class that should manipulate files. Its interface has a Open(string filename) method, and various other methods to retrieve and manipulate the contents. Is it ok to call the open method before calling the method I'm trying to…
BgrWorker
  • 1,694
  • 2
  • 9
  • 14
3
votes
2 answers

How can I roll-back the execution of a constructor while building a complex data structure?

I'm building a complex tree of objects. There are a total of five types, A, B, C, D, and E. There is a single instance of A, which is the root node. A has one or more Bs as children, each B has one or more Cs as children, and so on. The original…
Thomas Owens
  • 79,623
  • 18
  • 192
  • 283
3
votes
2 answers

using this in a constructor, why does it work?

I got two classes. busiObj(created from ancient flat files) key(provides means to tell busiObjs apart) key has a c'tor that takes a busiObj public key(busiObj foo) { bar = foo.someThing; [...] } I want to create the key in the c'tor of…
Mark
  • 373
  • 5
  • 13
3
votes
4 answers

Should I use the factory design pattern for every class?

I've been writing a website in PHP. As the code becomes more complex, I keep finding problems that can be solved using the factory design pattern. For example: I've a got a class Page which has subclasses HTMLPage, XMLPage, etc. Depending on some…
Frog
  • 324
  • 5
  • 10
2
votes
2 answers

Preferred way of handling errors when loading an object from a file

If I want to load an object from a file, there are a number of things that can go wrong. Thus, one needs a way of handling errors when doing so. In some languages, like haskell, one can return a Maybe object that might contain the newly created…
Grieverheart
  • 183
  • 7
2
votes
1 answer

Data serialization architecture, injection on construction vs. on function call

I am creating a data serialization/deserialization mechanism for essentially a persistent storage object. Due to the variety of systems this mechanism could run on, there needs to be a a variable number of "drivers" that could be used to serialize…
2
votes
1 answer

Learning to write DSLs utilities for unit tests and am worried about extensablity

I'm trying to simplify our unit tests with hand written DSL's. So far I have DSL's that walk developers through processing a service after setting up all preconditions and the construction of an monster object that has a huge constructor and…
candied_orange
  • 102,279
  • 24
  • 197
  • 315
1
vote
1 answer

Passing 0's (literals) to a constructor

I have a function that creates a new object by passing to it's constructor integer ids primarily with values of 0. This function is called when saving a newly created record (not an edit). public class RecordController { private int _recordId; …
1
2