Shaun Mccran

My digital playground

11
J
U
N
2009

Rounding up to the nearest timed value - Ex every 15 minutes

A watercooler moment threw up an interesting dilemma yesterday. I was talking to a colleague about a scheduled application that would run through a timed loop over the course of a day. It would chop off the top N records from a record set and perform an operation on them. Rather than having to manually run the task every 15 minutes, it would be much better to have it run automatically, at 00,15,30 and 45min markers.

So how to do this?

Initial thoughts wandered through various looping-over type conversations, and creating structures of the timed triggers etc.

First stab coding function looks a little something like this:

view plain print about
1<cf script>
2function RoundUp(input){
3var roundval = 15;
4var result = 0;
5
6if(ArrayLen(arguments) GTE 2)
7roundval = arguments[2];
8if(roundval EQ 0)
9roundval = 1;
10
11if((input MOD roundval) NEQ 0)
12{
13result = input + (roundval - (input MOD roundval));
14}
15else
16{
17result = input;
18}
19return result;
20}
21</cfscript>
22
23<cfset variables.time = RoundUp(#timeformat(NOW(),"mm")#)>
24<cfoutput>variables.time = #variables.time#</cfoutput>

Which successfully returns a rounded up value to the nearest 15 minutes.

It is a bit long winded though and after some more thought and consultation with more team members another solution was reached.

view plain print about
1dateadd("n", 15-(minute(now()) mod 15), now())

Which does exactly the same thing! Maybe not as extensible, you can't exactly build it into a dynamic function, but considerably smaller.

08
J
U
N
2009

Redirecting a URL using 301 or 302

If I want to redirect a URL I will usually try and do it on the server side, as this seems like the best fit solution to a redirect. It can be done server side, or in code, examples of both are below.

Whether it is an entire site redirect or a page move necessitating a URL redirect there are some small, but key differences in how to do it.

Differences in 301 or 302 redirects

The 302 redirect is a temporary redirect that indicates to browsers and search engines that it is not a permanent change. This is all well and good, but some engines will overlook this due to knowing that it is a temporary change. Google for instance will not index redirects of this type, effectively leaving your users pointed at the wrong URL.

The 301 redirect is a permanent redirect. It is much more search engine friendly, as they will not skip over it. They read it and cache the destination template as the end point, rather than using the redirect url, so saving your users that extra jump.

Implementing the redirect

In IIS:

  • In internet services manager, right click on the file or folder you wish to redirect
  • Select the radio titled "a redirection to a URL".
  • Enter the redirection page
  • Check "The exact url entered above" and the "A permanent redirection for this resource" (This makes it a 301 redirect)
  • Click on 'Apply'

In code:

view plain print about
1<cfheader statuscode="301" statustext="Permanent redirect">
2<cfheader name="Location" value="http://www.redirect-url.com">

12
M
A
Y
2009

Harnessing the built in functionality of your development language

Too often these days I am amazed at the lack of forethought that goes into harnessing the built in functionality of a platform. I am talking specifically about the code base here. I'm thinking from high level objects, down to single small functions.

Take a moment to step back and examine your code use. Was there a reason that you picked that language? I'm guessing that there would have been some sort of analysis of the technologies on offer, and you picked the one you currently have.

All too often people shoot off in random directions of development without really examining the functionality that a platform already provides.

My main field of development experience is coldFusion, so I'll use this as my example. An all too common example of this is the 'application.cfm', or 'application.cfc' file. This file is a directory level extender. IE any files in the same directory, or indeed sub directories will inherit properties set in it. This makes it ideal for things like user access, and session management, and general data persistence. In almost every custom built framework I have come across someone has decided that they would rather handle this is a different fashion. Now I'm not against writing custom code, far from it, but I think it is very important to know what your code base can already do - here's why:

  • Why write functionality again, when it is already there?
  • Its a standard function of the code base. Its generally going to be more efficient than anything your writing 'custom'
  • Depending on wether your open source or not, it will have generally been tested by a huge variety of developers, and software companies. Has your custom function?
  • Bringing in new staff, and contractors and introducing them to custom functionality is time consuming. Chances are they already know, or have at least heard of most of the in built platform functionality.

There are obviously exceptions to the rule, sometimes the intrinsic code simply does not do exactly what you want it to. So before you race off and write a huge error handler, or string replacement method, take a look around, you might find that it is in fact already under the hood.

27
A
P
R
2009

Securing your release code with the compile command

In this article I examine the compilation feature of ColdFusion, and explore why you would want to do this to your release code.

A project that I worked on in the recent past was being hosted on a remote server, which was being supervised by a third party company. We were still responsible for the code base and suspected that some 'hot fixes' had gone in as live code changes implemented on the server.

Obviously this is not ideal, the sanctity of your code base is paramount, and if it is your responsibility you have to be absolutely sure you know what it contains, especially if there are tinkerers.

Another good use for this functionality is where you are selling the software as a service (SAAS). The normal occurrence here is that the client is renting or leasing the functionality of your application, not the actual code base itself. So a good way to deter them from peeking under the hood is to compile it.

view plain print about
1d:\cfusionmx7\bin\cfcompile.bat deploy
2
3d:\cfusionmx7\wwwroot d:\cfusionmx7\wwwroot\appsRoot d:\cfusionmx7\wwwroot\AppsRootcompiled

In the code above the first line executes the cfcompile command, using the –deploy switch. This tells the compilation engine to take the source directory (the second line) and compile it, and move it into the destination directory (the third line). Be careful not to specify the same directories here, as it will flip out. Also there is a chance it will overwrite your source dir, which is bad as you cannot undo this.

The destination directory should now contain an exact copy of your code base, but compiled. Open a file it looks like garbage, but works exactly like the original.

Note that Compiling is different from Encrypting. With encryption you can decrypt the code base, as long as you know the seed, or hash. Compilation is irreversible, you cannot un-compile it, so be careful with your original un-compiled code base!

_UNKNOWNTRANSLATION_ /