Question

In the case that each method and variable will only be used once, which is the more correct convention?

Creating as many variables as necessary:

var x = GmailApp.getInboxUnreadCount();
var email = GmailApp.getInboxThreads (0, x);

Composing the code in one line:

var email = GmailApp.getInboxThreads (0, GmailApp.getInboxUnreadCount());
Was it helpful?

Solution

The latter, within reason. But this is largely a matter of style in the simple cases.

Meaning that if you have a function call that takes 10 arguments, and each of those arguments comes from a large function call itself, dont do this. Think of who gets your code base afterward.

"Is this line of code readable using less local variables? Or do I need to break it up to better illustrate what the line is doing?" is the question you should ask yourself. And in this case, the latter is totally readable.


In fact, I would argue the first example is less readable due to a useless local variable name x. If instead that were named better, it might be a more viable option.

var unreadCount = GmailApp.getInboxUnreadCount();
var email = GmailApp.getInboxThreads (0, unreadCount);

This is better, but still pretty unnecessary in this very simple case.

OTHER TIPS

It's a trade off.

The first example is easier to read and debug. It has 2 lines where errors can occur. For debugging this is good because there is only one function being called per line.

The second example is called a "one liner" and can be tricky to debug because there are multiple things that could cause errors on that line, but uses less memory. Probably an insignificant amount though.

In this example,

1. var unread = GmailApp.getInboxUnreadCount();
2. var email = GmailApp.getInboxThreads (0, unread);

Say you get an error on line 2. you know that getInboxThreads is throwing the error.

1. var email = GmailApp.getInboxThreads (0, GmailApp.getInboxUnreadCount());

Now say you get an error on line 1. You'll have to check both methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top