The Artima Developer Community
Sponsored Link

Java Buzz Forum
A new example of the evil of global variables, and state assumptions

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
A new example of the evil of global variables, and state assumptions Posted: Mar 24, 2006 9:33 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: A new example of the evil of global variables, and state assumptions
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement

I added a simple voting mechanism to ajaxian.com a few months ago.

Since Ajaxian runs on Wordpress the pattern for this work was:

  • Search for voting plugins
  • Weed through the million plugins
  • Find the best one for our needs
  • Go through the plugin code and optimize it

This time was no different. We based our work on the Votio plugin, and tweaked things so the plugin wasn't doing unnecessary calculations all the time.

The biggest sinner was the functions that take no variables.

As the pages are generated, they go through The Loop, where you get all of the blog entries given to you. We need to generate the ajax ratings form for each item, and to do that we just needed to add:

votio_ballot_box()

That function would access a global (gotta love PHP and "global $foo" :/) to get the current post object. Sounds fine right? We are in a loop of posts. We want to get the post.

It works at first, but what about if things change. For example, we want to do caching, and it is nice to dump out HTML that we can read in, instead of calculating these forms all of the time. In this case, the cache loader does NOT know about the loop of posts, as it is dealing with just HTML that it reads from cache.

I changed the API to be: votio_ballot_box($postId), and in The Loop I spit that out, so now when the cache is loaded it loads "votio_ballot_box(23)" and all is well.

A simple change. A stateless function. Now it is a lot more flexible.

Read: A new example of the evil of global variables, and state assumptions

Topic: Scalability: It's a matter of perspective Previous Topic   Next Topic Topic: [Mar 13, 2006 06:15 PST] 17 Links

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use