-12

I asked about a best-practice strategy for field initialization. To keep it tight and clear, I specified a limited set of possible approaches and explained why they all may make sense in certain scenarios. I also explained why such a inconsistent design may be less optimal. I intentionally removed any opinion based aspects.

I'd like to get some help altering the question to eliminate the issue that the community voted on.

5
  • 28
    "best practice" is by definition an opinion. What might be best for one case might not be the best for another case. Listing down all possible cases isn't really feasible and would lack focus. In general the question you want to ask there doesn't seem suitable for Stack Overflow's Q&A format. It might be suitable for discussions though. Commented Apr 9 at 7:09
  • 2
  • 2
    meta.stackoverflow.com/questions/337031/…
    – yivi
    Commented Apr 9 at 7:19
  • 4
    How can there be a best practice at this high a level? Some people will favour readability of code and will thus probably want fields initialized with default values, others will favour that the code does not do unnecessary work, which default values certainly are when the values are going to guaranteed be overwritten. Depending on where you stand, you will and should make different choices.
    – Gimby
    Commented Apr 9 at 9:33
  • 1

1 Answer 1

13

I intentionally removed any opinion based aspects.

All three of your interrogative statements in the question are opinion-based, so that's not true:

Here's the root of my uncertainty - I can't decide and I see good arguments either way.

  • Does it still make sense setting the default values (i.e. trivial)?
  • If it doesn't, should I prefer declared or empty?
  • Should the setting be done in constructor, ngOnInit or elsewhere?

Stack Overflow is not the place for opinion-based questions. Perhaps Software Engineering.SE would be a better place to ask.

If you want to salvage your question, you'll need to pick one particular option, attempt to implement it, and ask about any actual, real problems you encounter in doing so.

Do you get an error with your code? Do you not know how to implement some technical detail? Are there objective metrics you are failing to achieve with your implementation? These are things you can ask about on Stack Overflow, not whether something makes sense.

8
  • 4
    I believe Software Engineering SE will have the same issues with opinion-based questions.
    – Craig
    Commented Apr 9 at 15:35
  • 1
    I didn't realize it was opinion based. My impression was that initializing the variables this or that way, actually provided different limitations in the longer run. It's the case in C#, for instance, so I expected to be of relevance here as well. In fact, setting values from constructor conveys a different logic than from ngOnInit() so I wonder if your claim is perhaps too categorical. Commented Apr 9 at 18:54
  • 4
    @KonradViltersten If you are concerned about specific capabilities or implications, ask about those specific capabilities or implications, not something vague like "should I do X or Y". If the limitations you mention are irrelevant to your needs, then it doesn't matter. If it they are relevant, then ask about that instead of making us guess why you may or may not prefer one vs the other.
    – TylerH
    Commented Apr 9 at 19:27
  • I genuinely do not understand how it is possible to write the words "make sense" or "should", make no effort to describe a concretely measurable goal for the proposed action, and also sincerely believe that one is speaking objectively. "My impression was that initializing the variables this or that way, actually provided different limitations in the longer run." - then ask that: "if I initialize the variable in X way, what implications will it have for assignments I can make later on?" Commented Apr 9 at 21:22
  • (That way, people can immediately recognize that you really have a duplicate of "is this language statically or dynamically typed?" and point you at an actual answer to an actually answerable question.) Similarly, if you have a question about the difference in semantics between assigning something in a constructor vs. in ngOnInit, you can explicitly and directly ask about that. You might find it useful in this case to start by constructing an MRE where it makes an observable difference. Commented Apr 9 at 21:23
  • @KarlKnechtel Thank you for the comment. I feel it's not useful in the context of my curiosity. In my experience, the specific details you refer to, I can check myself in the docs. What's more difficult, is to understand the compounded effect of many such details. Usually, there's a good-enough standard approach that will work sufficiently well so I don't have to "reinvent the wheel". Also, I notice something surprising in the comments. It seems that the sensation is to resolve problems once they emerge. Commented Apr 10 at 6:41
  • @KarlKnechtel It seems rather ad-hoc and junior approach. Wouldn't it be wiser to address the issue before it emerges and resolve it by not having it at all applying smoother strategy? Perhaps I'm misinterpreting your point. Please help me understand. I start to fear that my original question perhaps is beyond the appropriate level of SO. It's not "how do I create X", namely... Commented Apr 10 at 6:44
  • 1
    "Wouldn't it be wiser to address the issue before it emerges and resolve it by not having it at all applying smoother strategy?" - No, because in software development, there is a high probability that there is no issue and that effort is wasted worrying. Commented Apr 10 at 10:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .