Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark,lib,test: use braces for multiline block #13828

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions benchmark/buffers/buffer-iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ function main(conf) {
function benchFor(buffer, n) {
bench.start();

for (var k = 0; k < n; k++)
for (var i = 0; i < buffer.length; i++)
for (var k = 0; k < n; k++) {
Copy link
Contributor

@mscdex mscdex Jun 20, 2017

Choose a reason for hiding this comment

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

I get why braces would be added here, but why also on the for below if it does not contain a multi-line body? Ditto for all other instances of this in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mscdex To make the decision about things like that, I looked at surrounding code and also used my own judgment as to what was more readable.

In this case, having one for with braces wrapping another for without braces seemed less readable to me than both having braces. Additionally, there were no other examples in this file of blocks without braces.

for (var i = 0; i < buffer.length; i++) {
assert(buffer[i] === 0);
}
}

bench.end(n);
}

function benchForOf(buffer, n) {
bench.start();

for (var k = 0; k < n; k++)
for (var b of buffer)
for (var k = 0; k < n; k++) {
for (var b of buffer) {
assert(b === 0);

}
}
bench.end(n);
}

Expand Down
6 changes: 4 additions & 2 deletions benchmark/dgram/array-vs-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ function server() {
var onsend = type === 'concat' ? onsendConcat : onsendMulti;

function onsendConcat() {
if (sent++ % num === 0)
if (sent++ % num === 0) {
for (var i = 0; i < num; i++) {
socket.send(Buffer.concat(chunk), PORT, '127.0.0.1', onsend);
}
}
}

function onsendMulti() {
if (sent++ % num === 0)
if (sent++ % num === 0) {
for (var i = 0; i < num; i++) {
socket.send(chunk, PORT, '127.0.0.1', onsend);
}
}
}

socket.on('listening', function() {
Expand Down
6 changes: 4 additions & 2 deletions benchmark/dgram/multi-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ function server() {
var socket = dgram.createSocket('udp4');

function onsend() {
if (sent++ % num === 0)
for (var i = 0; i < num; i++)
if (sent++ % num === 0) {
for (var i = 0; i < num; i++) {
socket.send(chunk, PORT, '127.0.0.1', onsend);
}
}
}

socket.on('listening', function() {
Expand Down
6 changes: 4 additions & 2 deletions benchmark/dgram/offset-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ function server() {
var socket = dgram.createSocket('udp4');

function onsend() {
if (sent++ % num === 0)
for (var i = 0; i < num; i++)
if (sent++ % num === 0) {
for (var i = 0; i < num; i++) {
socket.send(chunk, 0, chunk.length, PORT, '127.0.0.1', onsend);
}
}
}

socket.on('listening', function() {
Expand Down
6 changes: 4 additions & 2 deletions benchmark/dgram/single-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ function server() {
var socket = dgram.createSocket('udp4');

function onsend() {
if (sent++ % num === 0)
for (var i = 0; i < num; i++)
if (sent++ % num === 0) {
for (var i = 0; i < num; i++) {
socket.send(chunk, PORT, '127.0.0.1', onsend);
}
}
}

socket.on('listening', function() {
Expand Down
3 changes: 2 additions & 1 deletion benchmark/url/url-searchparams-iteration.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ function iterator(n) {
const noDead = [];

bench.start();
for (var i = 0; i < n; i += 1)
for (var i = 0; i < n; i += 1) {
for (var pair of params) {
noDead[0] = pair[0];
noDead[1] = pair[1];
}
}
bench.end(n);

assert.strictEqual(noDead[0], 'three');
Expand Down
5 changes: 3 additions & 2 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,13 @@ ClientRequest.prototype.abort = function abort() {
this.aborted = Date.now();

// If we're aborting, we don't care about any more response data.
if (this.res)
if (this.res) {
this.res._dump();
else
} else {
this.once('response', function(res) {
res._dump();
});
}

// In the event that we don't have a socket, we will pop out of
// the request queue through handling in onSocket.
Expand Down
8 changes: 4 additions & 4 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,13 @@ OutgoingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
// any messages, before ever calling this. In that case, just skip
// it, since something else is destroying this connection anyway.
OutgoingMessage.prototype.destroy = function destroy(error) {
if (this.socket)
if (this.socket) {
this.socket.destroy(error);
else
} else {
this.once('socket', function(socket) {
socket.destroy(error);
});
}
};


Expand Down Expand Up @@ -505,8 +506,7 @@ function matchHeader(self, state, field, value) {

function validateHeader(msg, name, value) {
if (typeof name !== 'string' || !name || !checkIsHttpToken(name))
throw new TypeError(
'Header name must be a valid HTTP Token ["' + name + '"]');
throw new TypeError(`Header name must be a valid HTTP Token ["${name}"]`);
if (value === undefined)
throw new Error('"value" required in setHeader("' + name + '", value)');
if (msg._header)
Expand Down
6 changes: 4 additions & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ function writeHead(statusCode, reason, obj) {
var originalStatusCode = statusCode;

statusCode |= 0;
if (statusCode < 100 || statusCode > 999)
if (statusCode < 100 || statusCode > 999) {
throw new errors.RangeError('ERR_HTTP_INVALID_STATUS_CODE',
originalStatusCode);
}


if (typeof reason === 'string') {
Expand Down Expand Up @@ -224,9 +225,10 @@ function writeHead(statusCode, reason, obj) {
headers = obj;
}

if (common._checkInvalidHeaderChar(this.statusMessage))
if (common._checkInvalidHeaderChar(this.statusMessage)) {
throw new errors.Error('ERR_HTTP_INVALID_CHAR',
'Invalid character in statusMessage.');
}

var statusLine = 'HTTP/1.1 ' + statusCode + ' ' + this.statusMessage + CRLF;

Expand Down
7 changes: 4 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,14 @@ Object.setPrototypeOf(Buffer, Uint8Array);
function assertSize(size) {
let err = null;

if (typeof size !== 'number')
if (typeof size !== 'number') {
err = new TypeError('"size" argument must be a number');
else if (size < 0)
} else if (size < 0) {
err = new RangeError('"size" argument must not be negative');
else if (size > binding.kMaxLength)
} else if (size > binding.kMaxLength) {
err = new RangeError('"size" argument must not be larger ' +
'than ' + binding.kMaxLength);
}

if (err) {
Error.captureStackTrace(err, assertSize);
Expand Down
7 changes: 4 additions & 3 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,16 @@ function spawnSync(/*file, args, options*/) {
var input = options.stdio[i] && options.stdio[i].input;
if (input != null) {
var pipe = options.stdio[i] = util._extend({}, options.stdio[i]);
if (isUint8Array(input))
if (isUint8Array(input)) {
pipe.input = input;
else if (typeof input === 'string')
} else if (typeof input === 'string') {
pipe.input = Buffer.from(input, options.encoding);
else
} else {
throw new TypeError(util.format(
'stdio[%d] should be Buffer, Uint8Array or string not %s',
i,
typeof input));
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1951,10 +1951,11 @@ ReadStream.prototype.open = function() {
};

ReadStream.prototype._read = function(n) {
if (typeof this.fd !== 'number')
if (typeof this.fd !== 'number') {
return this.once('open', function() {
this._read(n);
});
}

if (this.destroyed)
return;
Expand Down Expand Up @@ -2116,10 +2117,11 @@ WriteStream.prototype._write = function(data, encoding, cb) {
if (!(data instanceof Buffer))
return this.emit('error', new Error('Invalid data'));

if (typeof this.fd !== 'number')
if (typeof this.fd !== 'number') {
return this.once('open', function() {
this._write(data, encoding, cb);
});
}

var self = this;
fs.write(this.fd, data, 0, data.length, this.pos, function(er, bytes) {
Expand Down Expand Up @@ -2151,10 +2153,11 @@ function writev(fd, chunks, position, callback) {


WriteStream.prototype._writev = function(data, cb) {
if (typeof this.fd !== 'number')
if (typeof this.fd !== 'number') {
return this.once('open', function() {
this._writev(data, cb);
});
}

const self = this;
const len = data.length;
Expand Down
12 changes: 8 additions & 4 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,26 @@ class Session extends EventEmitter {
}

post(method, params, callback) {
if (typeof method !== 'string')
if (typeof method !== 'string') {
throw new TypeError(
`"method" must be a string, got ${typeof method} instead`);
}
if (!callback && util.isFunction(params)) {
callback = params;
params = null;
}
if (params && typeof params !== 'object')
if (params && typeof params !== 'object') {
throw new TypeError(
`"params" must be an object, got ${typeof params} instead`);
if (callback && typeof callback !== 'function')
}
if (callback && typeof callback !== 'function') {
throw new TypeError(
`"callback" must be a function, got ${typeof callback} instead`);
}

if (!this[connectionSymbol])
if (!this[connectionSymbol]) {
throw new Error('Session is not connected');
}
const id = this[nextIdSymbol]++;
const message = {id, method};
if (params) {
Expand Down
10 changes: 6 additions & 4 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ function setup_cpuUsage() {
}

// If a previous value was passed in, return diff of current from previous.
if (prevValue) return {
user: cpuValues[0] - prevValue.user,
system: cpuValues[1] - prevValue.system
};
if (prevValue) {
return {
user: cpuValues[0] - prevValue.user,
system: cpuValues[1] - prevValue.system
};
}

// If no previous value passed in, return current value.
return {
Expand Down
12 changes: 8 additions & 4 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,19 +1005,23 @@ function lookupAndConnect(self, options) {
var localAddress = options.localAddress;
var localPort = options.localPort;

if (localAddress && !cares.isIP(localAddress))
if (localAddress && !cares.isIP(localAddress)) {
throw new TypeError('"localAddress" option must be a valid IP: ' +
localAddress);
}

if (localPort && typeof localPort !== 'number')
if (localPort && typeof localPort !== 'number') {
throw new TypeError('"localPort" option should be a number: ' + localPort);
}

if (typeof port !== 'undefined') {
if (typeof port !== 'number' && typeof port !== 'string')
if (typeof port !== 'number' && typeof port !== 'string') {
throw new TypeError('"port" option should be a number or string: ' +
port);
if (!isLegalPort(port))
}
if (!isLegalPort(port)) {
throw new RangeError('"port" option should be >= 0 and < 65536: ' + port);
}
}
port |= 0;

Expand Down
6 changes: 4 additions & 2 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,18 @@ Interface.prototype._onLine = function(line) {
};

Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) {
if (typeof stringToWrite !== 'string')
if (typeof stringToWrite !== 'string') {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE',
'stringToWrite',
'string',
stringToWrite
);
}

if (this.output !== null && this.output !== undefined)
if (this.output !== null && this.output !== undefined) {
this.output.write(stringToWrite);
}
};

Interface.prototype._addHistory = function() {
Expand Down
3 changes: 2 additions & 1 deletion lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ function check(hostParts, pattern, wildcards) {
return false;

// Check host parts from right to left first.
for (var i = hostParts.length - 1; i > 0; i -= 1)
for (var i = hostParts.length - 1; i > 0; i -= 1) {
if (hostParts[i] !== patternParts[i])
return false;
}

const hostSubdomain = hostParts[0];
const patternSubdomain = patternParts[0];
Expand Down
3 changes: 2 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,9 +960,10 @@ exports.inherits = function(ctor, superCtor) {
if (superCtor === undefined || superCtor === null)
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor', 'function');

if (superCtor.prototype === undefined)
if (superCtor.prototype === undefined) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor.prototype',
'function');
}
ctor.super_ = superCtor;
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
};
Expand Down
Loading