Archives

Articles filled under ‘Coding’

Better error handling

Baseball Catcher
Error handling and reporting was always a thing that I knew I wasn’t doing it right. Leaded by PHP’s on-the-fly type casting I used to make functions just return false or null but that was not right because I was hurting the return-type integrity of them.
Read the full article

Smartly resolving your WordPress pages and posts JavaScript and CSS dependences

Sometimes you want to include page-specific JavaScriptw or CSSw files and ended up in cluttered ifs and else ifs or even creating a creepy new page just for that.

Wordpressw‘s excellent plugin infrastructure with their hooks and actions provides us a beautiful and clean form of including our JavaScript in specific pages.

In this tutorial, we will be using three WordPress functions: get_post_meta() and wp_enqueue_script() and wp_enqueue_style().
Read the full article

5 best practices on developing WordPress plugins

Wordpress Plugins
Wordpress wonders me how it born as a Blogging platform and is growing a powerful CMS platform. Its plugin framework is full of hooks and actions and almost everything can be trapped and modified by plugins.

As WordPress is a Procedural Programmingw oriented platform the function wrapped to the hooks and actions have no scope when called except global this can be hell when developing plugins. We must take extra-care to not compromise our plugin security and compatibility.

In our road developing plugins we learned some lessons, here is our share. Read the full article

Alternative syntax for PHP control structures

While it’s not a great secret, many people still not aware of the alternative syntax for control structures under PHP.

The C-like syntax for control structures are very intuitive but can deal lots of trouble when your code has lot of pages and lots of ifs, fors, while and therefore lots of opening and closing braces.

NOTE: As remembered by jakyra on DZone, if you keep your code tidy you shouldn’t have functions that long and though not having this kind of problem, but this alternative syntax still have good benefits.

The alternative syntax

The alternative syntax consists in changing the opening brace ({) to a colon (:) and each closing brace to its respective closing function: endif;, endfor;, endforeach;, endwhile; and endswitch;.
Read the full article

Sustainability application under Software Development

We are constantly trying to improve our web applications performance to gain speed, lower the load on the clusters and thus being able to attend to even more users but have you ever thought about how these improvements can aid Global Warming?

For years optimization techniques such as Caching in many levels (Disk/Memory), Compression, Clustering and other were developed to acheive greater performance.

Global Warming is there for years and is getting worse every year but it seems that movements under IT has begun in order to create greener solutions. Many hosts such as Dreamhost (which we’re proud of being our hosting company) have already started their effort in reducing or even neutralizing their Carbon Footprint and now developers can help on their side too.
Read the full article

Writing less: Using scientific notation for very large or very small numbers

When dealing with very large or very small numbers we can opt by writing them under scientific notation.

Scientific notation, also known as standard form or as exponential notation, is a way of writing numbers that accommodates values too large or small to be conveniently written in standard decimal notation. Scientific notation has a number of useful properties and is often favored by scientists, mathematicians and engineers, who work with such numbers.

Scientific Notation @ Wikipedia

So as we – developers – are part-scientists,part-mathematicians and part-engineers, so I guess this is perfect for us!

We have all seen that on high school and it seemed quite strange but it is quite simple indeed:

Equation

The equation is as it follows in a simplified language:
Simplified number x10number of decimal units

The “Simplified number” is the number in a simplified manner.
Examples:

  • 1000000 = 1
  • 4230000 = 423

And then we just add in the other hand the number of decimal units that were simpled-out.

  • 1000000 becomes 1×106
  • 4230000 becomes 423×104

Read the full article

WP-Minify’s new version vanished WordPress’ HTTP requests issue

While working with WordPress we always stuck with the HTTP requests issue caused by plugins that appends external JavaScript or CSS files to our page body thus causing more http requests and downgrading our performance benchmark tests and company job standards.

Content minification is a well-known best-practice for bandwidth reduction (saves client’s money) thus giving better page load time (gives client a good smile). Since it removes all unnecessary code (like comments, extra spaces, tabs etc) and join files together, the total of KBs saved even in bandwidth and browser rendering.

Here we always implemented the Minify PHP5 tool from Steve Clay and Ryan Grove because it always worked like a charm and the results are quite amazing.

In wordpress we got problems implementing this because plugins added their own js and css files at will. WP-Minify from Thaya Kareeson came to save our souls but we got some misbehaving plugins that still adding link and script tags by echoing to the page.

Fortunately, this new version preprocesses the output thus gathering any inline style/script reference and adding it to a temporary file that is included in the minification process.

I got tricked there because voting badges and other stuff uses inline script tags to generate the badge. WP-Minify’s preprocessing fufreaked me up because this script tag depended on the_permalink() function from WordPress thus not generating the correct output on the temporary file created. Before I started freaking out I realized that the plugin’s configuration page had an script (and style) blacklist. Just added the src url (without the querystring) and everything worked like a charm!

Nice job Thaya!

So your PHP headers are already sent, but you sent nothing?

So you are coding and your transfer your files to your production server via SVN or FTP and then you start getting some “Warning: Cannot modify header information – headers already sent” errors. Well, this is not good indeed.

This errors happens when you send a header command and has already outputted text to your output buffer (generally, the browser). Then you grep your code for misleft debugging prints, echos, print_rs and var_dumps but they are gone or commented.

Oh yeah, somehow some data were added during the transfer somewhere in your source files. Great! Now some cool action!
Read the full article

Quick Tip: Do you miss the target?

Opening links in a new window was once quick and easy but since the target="_blank" options on a links have died (if you didn’t knew, I’m very sorry to be the one telling you this) the only way left was the javascript way.
Read the full article

Quick Tip: JavaScript integers parsing

I’ve came across a strange problem when trying to parse date parts (2009 01 21) as integers in a form validation script. Somehow my month being parsed as 0 (zero) and not as 1 (one) as supposed to be. Strange. I’ve googled around and discovered that JavaScript’s parseInt support different (not so many) number bases and by default it “guesses” the base you are trying to work.

Sure, he found the “01″ and thought “Oh yeah babe, it’s an octal number!” — wrong my friend, it’s just a zero-padded number, sorry disappointing.

So, I had to supply the second argument called radix as 10 (decimal) that is indeed the base you are going to use. Setting this makes parseInt to respect you instead of having his free-will.

So I ‘ve got:

var _orderDateMonth = parseInt($('#survey_order_date_month').val(),10); // decimal base!

NOTE: The dollar sign ($) and .val() are specific JQuery functions.

NOTE: I personally hate when people break up the date parts into different inputs. If you are already going to use JavaScript, implement a damn date-picker!