Skip to main content
Tweeted twitter.com/StackSoftEng/status/1268467563203264514
Question Protected by gnat
Became Hot Network Question
edited body
Source Link
john c. j.
  • 501
  • 1
  • 3
  • 10

Is it OK to have a function that returns true/nothing instead of true/false?

My examples use JavaScript, but I'm wondering about the general case, not attached to one specific language.

The subjects of my interest:

  • It isIs it OK from the practical side?
  • Is it OK from the logical side?

I mean, some languages provide too much freedom and personally I don't like it. I think it is better to follow the way how it works in serious languages like C++ and Java, but I never worked with them.

var a = 1;
var b = 2;

function foo() {
    if (a === b)
        return true;
}

if (!foo())
    alert('error');

versus

function foo() {
    if (a === b)
        return true;
    else
        return false;
}

Is it OK to have a function that returns true/nothing instead of true/false?

My examples use JavaScript, but I'm wondering about the general case, not attached to one specific language.

The subjects of my interest:

  • It is OK from the practical side?
  • Is it OK from the logical side?

I mean, some languages provide too much freedom and personally I don't like it. I think it is better to follow the way how it works in serious languages like C++ and Java, but I never worked with them.

var a = 1;
var b = 2;

function foo() {
    if (a === b)
        return true;
}

if (!foo())
    alert('error');

versus

function foo() {
    if (a === b)
        return true;
    else
        return false;
}

Is it OK to have a function that returns true/nothing instead of true/false?

My examples use JavaScript, but I'm wondering about the general case, not attached to one specific language.

The subjects of my interest:

  • Is it OK from the practical side?
  • Is it OK from the logical side?

I mean, some languages provide too much freedom and personally I don't like it. I think it is better to follow the way how it works in serious languages like C++ and Java, but I never worked with them.

var a = 1;
var b = 2;

function foo() {
    if (a === b)
        return true;
}

if (!foo())
    alert('error');

versus

function foo() {
    if (a === b)
        return true;
    else
        return false;
}
Source Link
john c. j.
  • 501
  • 1
  • 3
  • 10

Returning true/nothing instead of true/false?

Is it OK to have a function that returns true/nothing instead of true/false?

My examples use JavaScript, but I'm wondering about the general case, not attached to one specific language.

The subjects of my interest:

  • It is OK from the practical side?
  • Is it OK from the logical side?

I mean, some languages provide too much freedom and personally I don't like it. I think it is better to follow the way how it works in serious languages like C++ and Java, but I never worked with them.

var a = 1;
var b = 2;

function foo() {
    if (a === b)
        return true;
}

if (!foo())
    alert('error');

versus

function foo() {
    if (a === b)
        return true;
    else
        return false;
}