Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented Jul 12, 2017

The second argument is usually never used (unless an assertion fails), so we shouldn't spend time eagerly computing its value.

@ghost ghost requested a review from rbuckton July 12, 2017 15:18
@ghost ghost force-pushed the debugAssert branch from 9ee240b to 210df75 Compare July 12, 2017 15:19
Copy link
Contributor

@rbuckton rbuckton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases the interpolation is probably cheaper than the closure. For the common cases, we can create specific assertions on Debug.


Debug.assert(start >= 0, "start must be non-negative, is " + start);
Debug.assert(length >= 0, "length must be non-negative, is " + length);
Debug.assert(start >= 0, "start must be non-negative", () => `is ${start}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding callbacks is arguably more expensive, as you have to create an environment record allocation each time.

It might make more sense to add something like Debug.assertNonNegative(start, "start") or similar.

*/
function createInlineBreak(label: Label, location?: TextRange): ReturnStatement {
Debug.assert(label > 0, `Invalid label: ${label}`);
Debug.assert(label > 0, "Invalid label", () => label.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback is probably more expensive.


Debug.assert(start >= 0, "start must be non-negative, is " + start);
Debug.assert(length >= 0, "length must be non-negative, is " + length);
Debug.assertLessThanOrEqual(0, start);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertGreaterThanOrEqual?

}

export function assert(expression: boolean, message?: string, verboseDebugInfo?: () => string, stackCrawlMark?: Function): void {
export function assert(expression: boolean, message?: string, verboseDebugInfo?: string | (() => string), stackCrawlMark?: Function): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoids needing a closure for the second argument (if it is not an interpolation).

const scriptInfo = this.projectService.getScriptInfoForPath(sourceFile.path);
if (!scriptInfo) {
Debug.assert(false, `scriptInfo for a file '${sourceFile.fileName}' is missing.`);
Debug.assert(false, "scriptInfo for a file is missing:", sourceFile.fileName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to change this, other than from an assert(false, to fail as the condition was already checked.

@ghost ghost changed the title And lint rule to check that Debug.assert calls do not eagerly interpolate strings Add lint rule to check that Debug.assert calls do not eagerly interpolate strings Jul 12, 2017
@ghost ghost merged commit ceae613 into master Aug 8, 2017
@ghost ghost deleted the debugAssert branch August 8, 2017 14:56
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants