April 19, 2012

Scope precedence (from function perspective)

Scoping. Very common topic among CF developers.
Which scope has precedence? When and why I should scope variable?
No matter how many times I read or heard answer on these question, no matter how many I answered to myself, I always forget it after few months.
Thus I decided to find a way how to remember it once and for all :)
Magic formula (for me) is in answer: "Scope precedence is same as it is in Java"
Here I talk only about precedence from function perspective.
Inside method (function) body we deal with 3 major scopes (fake this I'll ignore)

Argument scope - hold references to function arguments
Local scope - hold references to local variables, variables declared (vared) inside function body
Variables scope - it contains references to protected instance variables

In same order scopes are listed here, Adobe ColdFusion 9.x (and I beleive Railo 3.x) will go through when looking for variable referenced inside function body. This means:
Referenced variable, which is not explicitally scoped inside function body, ACF first look for in argument scope, then if it could not be found there, in local and, at the end, in variable scope.

At the same time, we should have 2 things in mind :
1) Local variable cannot be named same as argument (same as in Java)
2) CF, as dynamic language with weak typing, allows dynamic argument list which means, that argument does not need to be defined, unless it is explicitly set (required = true)

Example 1: Argument value not set

//ScopingTest.cfc
component{
 myVar = "instance variable value";
 
 //no default value set for argument myVar
 function myMethod(myVar){
  myVar = myVar;
  writeOutput(myVar);
 }
}

//call
new ScoptinTest().myMthod();

//output
"instance variable value"

Example 2: Argument value set

//call
new ScoptinTest().myMethod("argument value");

//output
"argument value"

From these examples, we clearly see, if we don't explicitly scope variable:
a) If argument is not defined, instance variable will take presedence
b) If argument is defined, it is going to take presedence

To summarize:
1. Argument and Local variables shares the throne (there can be only one)
2. Variable comes right after

Knowing this, much less characters we'll write in the future and much less noise in the code.

July 18, 2011

The Last Programming Language (my 5 cents)

This started as comment on Sean's review of Uncle Bob's preso "The Last Programming Language" and outcome had too many characters and convert it to blog. Anyway, I blog quite rarely, it's been about timeto finally make one in year 2011 :)  So, to understand what I am talking about you should go and see Bob's preso first.

My 5 cents

I don't dare to predict which or what kind of language will take the throne.
No matter I am familiar with most mentioned languages, and have working expirience with some, I really it is need much more then that to be able to elaborate on that. However, I see numerous other reasons why we, humans, will settle on one (uber, last, ultimate, call it however suits you the best) programm language. Because, it's in our nature. To illustrate it, I'll use popular analogies.

Safe side
Software development is too important discipline in today's society that this level of freedom is "unacceptable" (sounds hursh I know). These days is very popular to compare software with civil engineering, so I'm going to do it too. CE students are teached to do things in certain way. These ways guarantee acceptable level of quality of their products. Most civil engineers will blindly apply what they've learned and stay on safe side.
Doing better then that means balancing project expense and construction quality, in context of reality (moment).  Simplified, in most cases, it means better quality for less money. Also, knowing when to increase expense in favor of better outcome is quality for itself.
OK. You can always say "This is so rigid because human lives are at stake". True, but is it really that different with software engineering? What controls medical equipment and modern building machines? What civil engineer use for calculations and designs? What will descrease (or increase in rare cases :)) saldo on your banking account?

Common ground
Proponent of freedom and diversity use variety of speaking languages as an example. World "live" and communicate with so many different languages, so why it can't use more than one programming language.
English language is perfect contra argument. No matter do you like it or not, but this is de facto communication standard among professionals (of any kind). See? I am just doing it :) No matter french is more "singy", spanish sexier, italian simpler, chinese more descriptive, we settle (spontaneous or not, nevermind) on english as common ground.

Software development is not just coding
People are afraid of becoming bored, by time (through decades), working with one language.
First of all, language is just a tool. As such, it just helps you, assist you, to reach your goal.
And our goal is well crafted software. Standardizing laguage, gives me more time to focus on architecture and design. From perspective of tools, Eclipse seems as solid example. Common platform that suits the needs of most programmers.

To conclude.
I am quite sure it will happen by time. Is it going to be some mutant of functional PL or not, to me, is not important at all. Our profession is young and mature by time. Why it should mature in much different way then all other (centuries old) professions?

June 20, 2010

Learning Scala

As a man who try to follow goals of pragmatic programmer, learning Scala is one of investments in my portfolio and new language this year, as well.

I am a man of practice. Therefore, I like to learn is from examples. Brute force exercise of this principal is "Learning Scala in small bites" blog, as well I place I would recommend for start. To be able to remember or comprehend things, I always try to pass through fingers all I read. This case was no exception. To be honest, I didn't follow this rule all the time :)
Now when I poisoned my brain with a bunch of raw information, next move was to spice it with some explanations and more usable examples. Next address was IBM and "The busy Java developer's guide to Scala" series of articles by Ted Neward.

So far, I like Scala a lot. One of those languages that caught me on the first site. I have no experience with functional languages, but how Martin Odersky has incorporated its principals into object-oriented world is something I grasped(?) very easy.

UPDATE: Upffff. This is THE challenge! Just when I thought I'm moving/learning fast, an totally non-understandable example hit me in the face:)

March 22, 2010

What if I don't care for Flex Builder?

"Everyone" point out how $299 for CFB is a good deal because you get Flex Builder bundled. But what if I don't care for Flex? Is it still a good deal?

Flex Builder Standard costs $249 and CFB+FB $299. What is a real idea behind this? To promote CFB among Flex developers? Honestly, I see this as a better deal for Flex developers, who really needs an IDE for rapid development, then it is for CF developers (only).

Me, as CF developer, don't see this as a good opportunity at all. Opposite. 83% of money I'll pay, actually goes to something I won't use.

On the other hand, by supporting this project, we keep doors open for version 2 one day. And when that day come, I hope that CFB will be sufficient argument for itself to have decent sale.

Update:

Simple math:
Flash Builder 4 Standard (stand alone) - $249
CF Builder 1.0 w/ Flash Builder 4 Standard included - $299
--------------------------------------------------------------
-> CF Builder 1.0 costs $50

Related content:

March 16, 2010

BlazeDS: Creating endpoint at runtime

Why? Because I do not want to bother end users with xml configuration files. Simple as that.
Biggest enemy of your application is user itself. Therefore, reducing possibility to make a mistake is most important goal if you wish to left good impression with your app. This was one of steps that I take on the road to make Collyba installation procedure as easy as possible.
As I said in previous blog, while I was working on this, insight  to BlazeDS source files was of great help. Actually that is where I found a solution.

What are channels and endpoints?
Channels are client-side objects that encapsulate the connection behavior between Flex components and the BlazeDS server. Channels communicate with corresponding endpoints on the BlazeDS server. You configure the properties of a channel and its corresponding endpoint in the services-config.xml file.
But in our case, we won't configure it in XML file but programmatically.

BlazeDS: Creating destinations at runtime

Because it is already said a lot on this subject, I won't waste much words, but go straight to the point/code.
/**
* Create blazeds destination at runtime. Follow SVN link to java source files for more details.
* @name hint="Destination name" 
*/
function createDestination(destname,channelId){
 var local = structNew();
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/core/src/flex/messaging/MessageBroker.java
 local.brokerClass = createObject('java','flex.messaging.MessageBroker');
 local.broker = local.brokerClass.getMessageBroker( javacast('null','') );
 local.service = local.broker.getService('message-service');
 local.destinations = local.service.getDestinations();
 
 //check if destination already exists. If it does just return it
 if (structKeyExists(local.destinations,arguments.destname)){ //check if destination already exist
  return local.destinations[arguments.destname]; 
 }
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/core/src/flex/messaging/Destination.java
 local.destination = local.service.createDestination(arguments.destname);
 // Creates a ServiceAdapter instance and sets its id, sets if destination  is manageable
 local.destination.createAdapter('cfgateway');
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/common/src/flex/messaging/config/ConfigMap.java
 local.configMap = createObject('java','flex.messaging.config.ConfigMap').init();
 local.configMap.addProperty('gatewayid',application.CFEventGatewayId);
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/core/src/flex/messaging/config/ServerSettings.java
 local.ss = createObject("java","flex.messaging.config.ServerSettings");
 local.ss.setAllowSubtopics(true); 
 local.ss.setSubtopicSeparator('.');
 local.ss.setDurable(false);
 
 local.destination.setServerSettings(ss);
 
 local.adapter = local.destination.getAdapter();
 
 //Initializes the adapter with the properties.
 local.adapter.initialize('cfgateway',configMap);

 local.destination.addChannel(arguments.channelId);
 
 //Starts the destination if its associated Service is started and if the destination is not already running. 
 local.destination.start();
 
 return local.destination;
}

March 7, 2010

Deploy ColdFusion 8/9 on Glassfish v3

Deploying ColdFusion 8/9 on Sun Glassfish 3 is not much different than it is on IBM WebSphere 7 AS. Except, imho, it is far more optimal, because it takes much less resources (especially if it is setup for dev) and cost nothing. I did this on Win7 platform, but it looks completely the same on Linux platforms. So, let's move.

March 4, 2010

Proud to be second!

Not that I wouldn't like to be a first, but last 2 second places were awesome!
On 29th January I won second place on "Best Of CF9" contest. Contest was  organized by Ray and other Adobe Community Experts. Thank you for that. I won't talk about much about that here, just to emphize that I have "big" plans for Collyba. "Big" actually means that I'm really going to continue development on it, which is, for itself, enormous accomplishment:)

February 17, 2010

What is MVC in few simple words

Today I read an excellent post on FW/1 mailing list. My impression came from ideal I try to follow in my life: "perfection lies in simplicity". I was, several times in situation to try to explain people what MVC (framework) is, and discovered that unexperienced developers have a problem to chew up that subject. One more thing I am sure about is that usually it is a problem with how things are presented more then to whom they are presented. However, this man, Ryan Cogswell, did it in 5 simple sentences. So, for everyone who are still not clear what MVC framework is and how it works, and for me who will use his words to explain others, I'll quote:

January 9, 2010

Everything is dead these days


Sounds morbid, but, really, in last few weeks everything died :)

As a ColdFusion developer most noticeable to me was spree of articles & blogs which tried to explain how CF is dead (or is about to die). But that's something I used to. Each time new CF Server comes out, "CF-is-dead wave" hit the coast. What really surprised me was that everything else died.

Few days ago, I read an "old" (for some reason it became popular again) article how UML died and 13 reasons why is so. According to author, this technology is already dead. After reading it, my only question was: What is an alternative? Read a blog, read dozens of comments and no one suggested what can be an alternative to this.

June 5, 2009

ColdFusion Regular Expression Optimization

Last night I bumped on article talking about regular expression performance tuning. After reading it and since we extensivly use regex to parse article & community content, I decided to see can we do something to bit boost performance on that side. So, here we go.

Startup facts and implications:

a) „ColdFusion is built on top of Java. ColdFusion data types (String, Query, Struct, Array, etc.) are really just custom Java classes that are built on top of things like strings, record sets, hash tables, collections, etc. „
=>
We can use java.lang.String methods on ColdFusion strings. This is more a fact then something we are going to use and you’ll see later why ;)

April 25, 2009

ColdFusion on Rails

First of all I am not talking here about porting CF in Rails framework, but porting of ideas.

After I heard for millionth time "Ruby on Rails is cool", I've decided to try it myself. Well, that was 6+ months ago: read Ruby language tutorial, few tuts on Rails framework, and that's where I stopped. Don't get me wrong, it is not because I didn't like it, but because of lack of time. Anyway, I think I got point, and point where I sincerely agree: "freedom yes, but not too much" or "too much freedom can kill you".
How is this related with ColdFusion.
Plus/minus some time since I start learning RoR, I also heard (at that time rumor) that Adobe plans to integrate Hibernate in new version of CF Server (code name Centaur).
I'll stop here (for now) and conclude: CF needs to make a choice on MVC framework and CF on Rails is there.

to be continued...

CF Wheels, framework inspired by Ruby on Rails, hit its first final version.

April 23, 2009

Pingback

Wikipedia:
A Pingback is one of three types of Linkbacks, methods for Web authors to request notification when somebody links to one of their documents. This enables authors to keep track of who is linking to, or referring to their articles.


Pingback is an XML-RPC request sent from Site A to Site B. This functionality usually comes as part of blogging softwares since it's most commonly used there. Almost all open source blogging applications are written in PHP and only a few in CF and none of them have support for pingback. Therefore I decided to write an CF implementation by myself.

Download source code

pingback.cfc - Pingback implementation
tools.cfc - Various tools used by pingback.cfc
xmlrpc.cfc - Translates XML-RPC packets to and from CFML data structures
xmlrpc.cfm - XML-RPC Server
test.cfm - Test template. Start from this file.

Successfully tested on Railo 3.1 and ColdFusion 8 Enterprise Server.
Should not be a problem to integrate it into blogCFC.

Pingback specification:
http://www.hixie.ch/specs/pingback/pingback


December 1, 2008

Eclipse: Working Sets

Very neat feature of Eclipse. It is going to help you to locate files. Why would you watch and browse through jungle of files, if you can isolate only part of code (files) that are of your interest? Interested, then Read It!

November 24, 2008

ColdFusion Frameworks

The best retrospective of ColdFusion frameworks I've found on net. Article will give you insight into most popular frameworks and categorization.

Read It!

November 17, 2008

Unit testing in ColdFusion

Why would CF community differentiate from any other community? That's why "all" Unit testing platforms are derivatives of xUnit project. My choice is CFUnit, because CFCUnit looks abandoned (not sure is it really), it has Eclipse plugin and it's better documented. But, after all, both environments do the job well.
So much about installation. Nnow, when you have everything you need, you just need to know what to do with it. Well, go and read CFUnit primer. It's the fastest way to learn how to use CFUnit.

More to read:
Test Driven Development with ColdFusion
(part I-III)

CFC variables.instance

When I saw for the first time, in cfc code, variables.instance = StructNew(), I asked myself "What, what, what?" :) Well, purpose of "subinstancing" of variables structure, is nothing more but teh convenience:
1. It's organized better. All variables of same kind can be grouped in that manner.
2. Easier for cfdump-ing. Do and you'll see what I'm talking about.
3. And it is more obvious what you do: variables.instance.myVar="A" means "I am adding (assigning value to) myVar into instance of variables structure"

Not necessity but can be handy

November 13, 2008

Developer Guidelines

I found this in "Developer Guidelines" on R-Project developer page. This is something I was always followed during my work but never had it written and digested. So, thank you r-project people for this :) Here it goes:
  • DO fix simple bugs
  • DO NOT fix bugs that require extensive modification
  • DO NOT fix exotic bugs that haven't bugged anyone
  • DO make small enhancements if they are badly needed
  • DO NOT let one user decide what is badly needed
  • DO fix configuration for broken platforms
  • DO NOT break functioning platforms in the process
  • DO NOT fix things that are not broken
  • DO NOT restructure code
  • DO NOT add experimental code
  • DO NOT modify the API (unless absolutely sure it is buggy)
  • DO NOT change defaults without a *very* good reason
  • DO clarify documentation
  • ONLY add or modify examples if needed for clarification
  • DO NOT reword messages
  • DO NOT modify regression tests (except if they were buggy)
  • DO NOT add code that cannot become reasonably complete by the next release.
Obviously none of the above rules are carved in stone, and all are subject to interpretation in actual cases

November 12, 2008

Eclipse and SVN Client

I choose Subclipse as my favorite SVN Client plugin for Eclipse. Why? Because most people use it. To install it go here

To start working open Eclipse, go to Window -> Show View -> Other, open up the SVN Folder and pick the SVN Repository, click on the "Add SVN Respository" button and enter the address of your SVN server (svn:// or svn+ssh:// or http:// ot https://). That's it.

Few basic commands (abstract from SVN online book):
The typical work cycle looks like this:

1. Update your working copy.
• svn update

2. Make changes.
• svn add
• svn delete
• svn copy
• svn move

3. Examine your changes.
• svn status
• svn diff

4. Possibly undo some changes.
• svn revert

5. Resolve conflicts (merge others' changes).
• svn update
• svn resolve

6. Commit your changes.
• svn commit

Setting up SVN Server

OK, Let's be honest. Who doesn't like to do a job in a minute and pretend to be uber smart? Software that would allow you that, in case of SVN installation, is svn1clicksetup. Without further hesitation I will tell you to go there, download it and do your "next-next-finish" work.

But, if you really,really, really feel that you need to put some effort (aka extra work), you may
go to read Chapter 6. Server Configuration of SVN book online.
This chapter will help you to decide which server configuration to choose and how to set it up.
Read It!

Ofc, I've chosen easiest option - vanilla svnserve installation. Why? Well I am not an administrator, I am not willing to become one and there is always large possibility that company you are working for already have SVN server configured or rented.

Btw, "Version Control with Subversion" is very good literature to read and place where you can look for answers on this subject.