Should I document a "return callback()" as return value if it is not intended to be used? - jsdoc

I have a piece of code which returns nothing useful:
/**
* Close the web server
*
* #param {function} callback - Called after web server is stopped
*/
PolyApp.prototype.stop = function(callback) {
if (!this._listeningServer) {
if (callback) {
return callback();
}
return;
}
this._listeningServer.close(callback);
};
This function makes use of return to control the execution flow. Given it returns nothing useful I want to avoid documenting it. That gives me the following benefits:
The documentation is more clear as it documents the intention of use
The code is less cluttered with comments that provide no value
I avoid signing a contract of returning something that I do not want to maintain.
On the other hand:
I am returning a value which is not being documented
I think that I should not document it as I do not want people to rely on any returning behavior.
What do you think about? Am I doing right being pragmatic?

What you want to document is up to you. You have to ask yourself the question: "Will I, or other people, ever need to see this documentation to get additional knowledge?". In the case of callbacks that don't need any specific return behavior, you don't need to document anything of it. You should speficy for stop that it'll return whatever the callback returns. People might get confused otherwise.

Related

When should one use element.isPresent() over ExpectedConditions.presenceOf(element)?

While end-to-end testing our app, I am often waiting for a state transition (a modal to close, a button to appear, etc). Blind experimentation has led me to sometimes use browser.wait(ExpectedConditions.presenceOf(someElement), and other times use browser.wait(someElement.isPresent()).
To me, the names imply they're interchangeable. But it isn't so. Is there something about the situations in which I use these that will help me tell which to use when?
In my experience, I have used element.isPresent() for when I do not want to specifically verify something. For example I can write:
element.isPresent().then(function(elm){
if(elm) { //if the element is present, do something }
else { //if the element is not present, do something else }
});
But if I need some element to be present/visible/clickable in order to execute an expect or something else, then I would use a browser.wait(EC.presenceOf/visibilityOf/elementToBeClickable(element))
It also depends on the element you need to wait for. If its a button that you need to click, you'll have to use elementToBeClickable
If we take a look at the code, we will see that isPresent method looks like this
isPresent(): wdpromise.Promise<boolean> {
return this.count().then((count) => {
return count > 0;
});
}
While presenceOf is dependent on isPresent
presenceOf(elementFinder: ElementFinder): Function {
return elementFinder.isPresent.bind(elementFinder);
};
In fact they are doing the same. But note, that ExpectedConditions operators, like not, or and etc. expecting ExpectedConditions function as an argument whichpresenceOf is, while isPresent is not.
Sometimes you need more complicated conditions, than to check that one particular element is present browser.wait(someElement.isPresent()). This is where ExpectedConditions constructions are incredibly useful and this is where you would need presenceOf.

synchronous queries to MongoDB

is there a way to make synchronous queries to MongoDB?
I'd like to run some code only after I've retrieved all my data from the DB.
Here is a sample snipped.
Code Snippet A
const brandExists = Brands.find({name: trxn.name}).count();
Code Snippet B
if(brandExists == 0){
Brands.insert({
name:trxn.name,
logo:"default.png",
});
Trxs.insert({
userId,
merchant_name,
amt,
});
}
I'd like Code snippet B to run only after Code Snippet A has completed its data retrieval from the DB. How would one go about doing that?
You can use simple async function async function always returns a promise.
const brandExists;
async function brandExist() {
brandExists = Brands.find({
name: trxn.name
}).count();
}
brandExist().then(
// Your Code comes here
if (brandExists == 0) {
Brands.insert({
name: trxn.name,
logo: "default.png",
})
Trxs.insert({
userId,
merchant_name,
amt,
});
});
I don't think using an if statement like the one you have makes sense: the queries are sent after each other; it is possible someone else creates a brand with the same name as the one you are working with between your queries to the database.
MongoDB has something called unique indexes you can use to enforce values being unique. You should be able to use name as a unique index. Then when you insert a new document into the collection, it will fail if there already exists a document with that name.
https://docs.mongodb.com/manual/core/index-unique/
In Meteor, MongoDB queries are synchronous, so it already delivers what you need. No need to make any changes, snippet B code will only run after snippet A code.
When we call a function asynchronous we mean that when that function is called it is non-blocking, which means our program will call the function and keep going, or, not wait for the response we need.
If our function is synchronous, it means that our program will call that function and wait until it's received a response from that function to continue with the rest of the program.
Meteor is based in Node, which is asynchronous by nature, but coding with only asynchronous functions can origin what developers call "callback hell".
On the server side, Meteor decided to go with Fibers, which allows functions to wait for the result, resulting in synchronous-style code.
There's no Fibers in the client side, so every time your client calls a server method, that call will be asynchronous (you'll have to worry about callbacks).
Your code is server-side code, and thanks to Fibers you can be assure that snippet B code will only run after snippet A code.

Preferred way to check if query is successful

When checking if a query was successful, what is better?
This
$query= "SELECT * FROM `table`";
$mysqliResult = $mysqli->query($query);
if(mysqli_num_rows($mysqliResult)) {
}
Or this:
$query= "SELECT * FROM `table`";
$mysqliResult= $mysqli->query($query);
if(get_resource_type($mysqliResult) === 'mysqli result') {
}
Neither.
Firs one is not an option at all. It'just inapplicable, as empty result is a legitimate result as well - the query was a success anyway.
Besides, in case of an unsuccessful query, this very code will throw an error itself!
The second one could be used for the purpose, but this approach is outdated and extremely inconvenient.
Instead, set mysqli in exception throwing mode, and you will need no code to test for success at all!
First option is better because your are checking only query is success or not but second option we use that when we need to check values and data type are equal ,so first is better

Meteor - Cannot Access All Read Operations

I'm new to Meteor. I've been stuck on this problem for a while. I can successfully adds items to a collection and look at them fully in the console. However, I cannot access all of the read operations in my .js file.
That is, I can use .find() and .findOne() with empty parameters. But when I try to add .sort or an argument I get an error telling me the object is undefined.
Autopublish is turned on, so I'm not sure what the problem is. These calls are being made directly in the client.
This returns something--
Template.showcards.events({
"click .play-card": function () {
alert(Rounds.find());
}
})
And this returns nothing--
Template.showcards.events({
"click .play-card": function () {
alert(Rounds.find().sort({player1: -1}));
}
})
Sorry for the newbie question. Thanks in advance.
Meteor's collection API works a bit differently from the mongo shell's API, which is understandably confusing for new users. You'll need to do this:
Template.showcards.events({
'click .play-card': function() {
var sortedCards = Rounds.find({}, {sort: {player1: -1}}).fetch();
console.log(sortedCards);
}
});
See this for more details. Also note that logging a cursor (the result of a find) probably isn't what you want. If you want to see the contents of the documents, you need to fetch them.
Rounds.find().sort({player1: -1}) returns a cursor, so you will want to do this:
Rounds.find().sort({player1: -1}).fetch();
Note that this returns an Array of document objects. So you would do something more like this:
docs = Rounds.find().sort({player1: -1}).fetch();
alert(docs[0]);

CGI::Application and SQLite

I've been messing around with CGI::application the past couple of days and decided to create a really basic forum: the first page displays all posts (only first level, no replies or anything) and a form which can be used to create a new post.
The issue I'm running into is that the data that gets entered into the form never gets inserted into the SQLite database.
Here's the sub procedure I'm having trouble with:
sub newpost {
my $self = shift;
if ( $self->param() ){
my $dbh = DBI->connect("dbi:SQLite:dbname=$database_file","","");
my $sth = $dbh->prepare("INSERT INTO posts (author, time, text) VALUES('testuser', '2011-10-23', 'This is a test!')");
$sth->execute();
$self->header_type('redirect');
$self->header_props(-url=> '?rm=viewall');
}
else {
my $tmpl_obj = $self->load_tmpl('newpost.html');
return $tmpl_obj->output();
}
What happens correctly is that when the newpost run mode is first called, the code within the else statement is run (the template with the form is loaded). The action for the form calls this same run mode, but now that parameters are being provided, the code in the if statement is run. I've checked the SQL code itself and it works, so there must be something else I'm over looking.
Also, is it considered best practice to go about implementing the form logic in this way?
Thanks
You're confusing $self->param() with $self->query->param. The 1st is per-request application level parameters (stuff you might set in one method and use again in another method) and the 2nd are the parameters from the GET query string or the POST body of the request. If you're expecting something from the user it will be in $self->query->param.
BTW, the $self->query object is a normal CGI object, so see it's documentation for specifics.