A javascript design pattern for options with default values?

This uses jQuery.extend but could be interchanged with an object merger from your library of choice or Object.assign in ES6. function Module(options){ var defaults = { color: ‘red’ }; var actual = $.extend({}, defaults, options || {}); console.info( actual.color ); } var a = new Module(); // Red var b = new Module( { color: … Read more

Should you declare methods using overloads or optional parameters in C# 4.0?

I’d consider the following: Do you need your code to be used from languages which don’t support optional parameters? If so, consider including the overloads. Do you have any members on your team who violently oppose optional parameters? (Sometimes it’s easier to live with a decision you don’t like than to argue the case.) Are … Read more

Passing an empty array as default value of an optional parameter [duplicate]

You can’t create compile-time constants of object references. The only valid compile-time constant you can use is null, so change your code to this: public void DoSomething(int index, ushort[] array = null, bool thirdParam = true) And inside your method do this: array = array ?? new ushort[0]; (from comments) From C# 8 onwards you … Read more

Set default value for DateTime in optional parameter [duplicate]

There is a workaround for this, taking advantage of nullable types and the fact that null is a compile-time constant. (It’s a bit of a hack though, and I’d suggest avoiding it unless you really can’t.) public void SomeClassInit(Guid docId, DateTime? addedOn = null) { if (!addedOn.HasValue) addedOn = DateTime.Now; //Init codes here } In … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)