Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit (default) values vs Explicit values

Tags:

c#

.net

Is it a bad practice to rely on implicit default values, like:

class Node
{
    int red;
    int green;
    int blue;

    bool grayscale;

    Node next;
}

Instead of explicitly setting them:

class Node
{
    int red = 0;
    int green = 0;
    int blue = 0;

    bool grayscale = false;

    Node next = null;
}
like image 712
Joan Venge Avatar asked May 09 '09 21:05

Joan Venge


People also ask

What is a default value example?

A DEFAULT value clause in a data type specification explicitly indicates a default value for a column. Examples: CREATE TABLE t1 ( i INT DEFAULT -1, c VARCHAR(10) DEFAULT '', price DOUBLE(16,2) DEFAULT 0.00 ); SERIAL DEFAULT VALUE is a special case.

How do you define a default value?

A default value is the value that is inserted into a column when an explicit value is not specified in an INSERT statement. A default value can be a literal character string that you define or one of the following SQL constant expressions: USER.

Which data type Cannot have default values?

The BLOB , TEXT , GEOMETRY , and JSON data types cannot be assigned a default value.

What are default values in coding?

A function default value is a value that can be used for a parameter if the calling statement does not pass an argument. If an argument is provided, the default value is ignored.


1 Answers

No, I think it's OK to rely on default values. Explicitly assigning them will just clutter up the code. Also, it has the advantage of making it easier to distinguish fields you assign non-default values to.

like image 58
mmx Avatar answered Sep 19 '22 02:09

mmx