HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Plugin for pulling Tweets

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
tweetspluginforpulling

Problem

I've been working on a jQuery plugin for pulling tweets since the v1 endpoints were all closed off and you need oAuth now. This is just the jQuery part but if you want to see the php behind it I'll be happy to post. But basically, I'd love suggestions, as this is one of the first real plugins i've made, i'm really looking for critique on patterns and efficiency. Is there anything I'm doing that can be shorten'd significantly? Is there a feature you think would be cool for me to include?

And just FYI, there is an oAuth/twitter library that authenticates everything first and pulls, and the php file my ajax call references holds the logic for all that and creating / serving the json, but it's not that exciting.

I'm going to post the plugin with the documentation comments I have in there, to see if it all makes sense to people who have never seen it. All opinions and comments welcome, I want to write better code!

```
/***
Invisible Tweets
Author : Shan Robertson
Website: http://invisibled.com
Version: 1.0
Updated: August 7th, 2013
Description:
This plugin allows you to simply display Twitter timelines & lists on your website.
It caches the twitter content in a json file & changing the cache name will
allow you to have as many instances of Invisible Tweets as you want.

Dependancies:
- jQuery
- invisibletweets.php (included)
- OAuth.php (included)
- twitteroauth.php (included)
- invisibletweets/cache folder (must be writable)

Options:
user : [string] - The desired Twitter username.

slug : [string] - The slug to your Twitter list.

cache : [string] - The cache name, if dealing with multiple feeds.

path_to_core : [string] - Path to the invisibletweets folder.

expire : [string] - Cache expire time, uses default PHP time syntax eg. 15 minutes, 10 seconds.
Warning: If expire time is shorter than 15 minutes, you may hit the rate limit.

type : [string]

Solution

A few minor notes:

-
Comments like this:

// Creates an empty template with all present data, in order.
$.each(tplArray, function(i, elm){


and this:

//Populates the template with data
$.each(data, function(i, item){


could be great function names. It would also increase the abstraction level of the code which helps maintainers with a quick overview about what the code does (without the details). They still can check the details inside the functions if they're interested in. It usually requires less code to understand (so it's faster and easier), especially if they want to modify only a small part of it.

(Clean Code by Robert C. Martin, Don’t Use a Comment When You Can Use a Function or a Variable, p67.)

-

var scope     = this;
var parent    = this.element;
var tpl       = "";
var tplArray  = scope.options.template.replace(/[\s,]+/g, ',').split(',');


I've found this kind of indentation hard to maintain. If you have a new variable with a longer name you have to modify several other lines too to keep it nice. It could also cause unnecessary patch/merge conflicts.

-
Comments on the closing curly braces are unnecessary and disturbing. Modern IDEs and editors could show blocks.

}; //findLinks


“// …” comments at end of code block after } - good or bad?

Code Snippets

// Creates an empty template with all present data, in order.
$.each(tplArray, function(i, elm){
//Populates the template with data
$.each(data, function(i, item){
var scope     = this;
var parent    = this.element;
var tpl       = "";
var tplArray  = scope.options.template.replace(/[\s,]+/g, ',').split(',');
}; //findLinks

Context

StackExchange Code Review Q#29565, answer score: 2

Revisions (0)

No revisions yet.