Instance variables in methods outside the constructor (Python) — why and how?

Why is it best practice to initialize the instance variable within the constructor? Clarity. Because it makes it easy to see at a glance all of the attributes of the class. If you initialize the variables in multiple methods, it becomes difficult to understand the complete data structure without reading every line of code. Initializing … Read more

What is the rationale for not having static constructor in C++?

Using the static initialization order problem as an excuse to not introducing this feature to the language is and always has been a matter of status quo – it wasn’t introduced because it wasn’t introduced and people keep thinking that initialization order was a reason not to introduce it, even if the order problem has … Read more

Don’t understand @ConstructorProperties

@ConstructorProperties is used by some serialization frameworks to associate constructor parameters with corresponding fields and their getter and setter methods. To do this, it relies on the same common naming conventions that is used when naming getter and setter methods for fields: Getter and setter method names are usually created by capitalizing the field’s name … Read more

C++: When (and how) are C++ Global Static Constructors Called?

When talking about non-local static objects there are not many guarantees. As you already know (and it’s also been mentioned here), it should not write code that depends on that. The static initialization order fiasco… Static objects goes through a two-phase initialization: static initialization and dynamic initialization. The former happens first and performs zero-initialization or … Read more

@ViewScoped calls @PostConstruct on every postback request

In other words, your @ViewScoped bean behaves like a @RequestScoped bean. It’s been recreated from scratch on every postback request. There are many possible causes for this, most of which boils down that the associated JSF view is not available anymore in the JSF state which in turn is by default associated with the HTTP … Read more

Use Moq to mock Constructor?

var myMockBOC = new Mock<BusinessObjectContext>(null, null); This will pass nulls in for your two parameters. Another approach would be to create an internal constructor meant for test usage only, and use InternalsVisibleTo to allow your test assembly to use it. Unfortunately this has a big drawback in that if you sign your assemblies, Moq is … Read more

Can a constructor return a NULL value?

I agree with everyone else that you should use exceptions, but if you do really need to use NULL for some reason, make the constructor private and use a factory method: static CMyClass* CMyClass::create(); This means you can’t construct instances normally though, and you can’t allocate them on the stack anymore, which is a pretty … Read more