Saturday, July 31, 2010

Clean Code: Why it matters and how it's done

Heading in late after dealing with failover of db to B..

Variables

What's in a name?
  • Intention
  • No cryptic names
  • No abbreviations
  • Rather have something longer, but clearer/obvious
  • Be serious
  • Funny comments etc

//Not clear
for(i=1; i<=ArrayLen(myArr); i++)
{
temp=myArr[i];
temp.update();
}

//More clear
for ( thisForm=1; thisForm <= ArrayLen( forms ); thisForm++ )
{
forms[thisForm].setDefaultValues();
}

Much more readable.
Doesn't like the use of i or j, k as it can get confusing in nested loops and isn't clear

Another side-by-side

v1 = new RCVal();
isDumb = v1.idiotCheck();

validator = new RemoteConnectionValidator();
isConnectionActive = validator.checkConnection();

Logic and Algorithms If / Else
  • Minimize use
  • If possible, just don't use them
  • Simple conditions
  • Break it up
  • Avoid nesting
  • Don't assume a branch will run

If you can't look at a piece of code and tell what it can do in under 10 seconds, think about re-writing.
The following just gets too confusing. Try not to have so many conditions in one if statement, break it up.

if(currentUser.isActive()
&& Len(form.content)
&& isValid(form.email)
&& currentUser.isHappy()
)
{
//do something
}

Seperate out the code so you don't get lost in the if/else statments...


if (isMessageReady( form ) )
{
//20 lines of
// send email
//code
}
else if ( !isMessageReady ( form ) )
{
//20 more
//lines
//of code
}

Change to this..

if (isMessageReady( form ) )
{
sendMessage( form )
}
else if ( !isMessageReady ( form ) )
{
handleMessageError( form )
}


Switch / Case
  • Minimize Use
  • Breaking it up
  • Avoid Nesting
  • Don't assume a case will match

Looping
  • Break up logic
  • Avoid nesting

for ( thisPreference in preferences )
{
if ( !thisPreference.isDefaultPreference() )
{
updatePreference.preferences( preferences[this] );
}
else
{
updatePreference.preferences( preferences[this] );
}
}

for (thisPrefernce in preferences )
{
updatePreference.preferences( preferences[this] );
updatePreference.preferences( preferences[this] );
}

Basically, make the code you're writing more clear to read and handle extra stuff in their own methods..


Methods and Functions

Verb-oriented Names
  • updateRSSFeed()
  • validate()
  • trackShipment()
  • authenticate()
  • findRelatedProducts()

Methods - Smaller = better
  • More cohesive
  • Easier to test
  • Easier to override

Focus!
  • Sergeants and privates
  • One public function that gets called from the outside (to a CFC for example), but within that its a bunch of private methods that get called.
  • One level of abstraction

Try not to argue
  • Avoid many arguments
  • Best number is 0, then 1, then 2... After that it gets too many / too complicated
  • Avoid optional arguments
  • Add a class to encapsulate arguments

// 1+3^2 = 10 possible invocations
function loadContent ( url, isSSL=false, useCache=false, method='get' )

function loadContent ( LoaderSettings settings )

Where you're passing a cfc as an argument, if it makes things more clear of simple

Side Effects:
What just happened?

  • Avoid side effects
  • Return a value vs changing things
  • Throwing things into a method and having it updating things, return things back to make it more clear

Error Codes
  • Codes are evil
  • Codes can change
  • Exceptions are there for a reason!

Classes (and CFCs)

Noun-Oriented Names

  • Shipment
  • Attendee
  • Permission
  • UserGateway
  • RemoteContentService
  • InvoiceGenerator
Again, smaller wins
Rather have more smaller, cohesive cfcs vs really long ones
3-4 screens tall is really big..

Cohesive
  • Does one thing
  • Simpler
  • Easier to test

Guard that Wall!
  • Encapsulate by convention, reveal by need
  • Be paranoid up front, make all private and expose them as you need them..
  • Limit external dependencies
  • The more things something depends on, the more change a chance will occur and negatively affect things.

Comments

Don't Comment

  • All comments are failures
  • Code is not simple enough or the language is not useful enough to allow for you to be clear.
  • Ask yourself WHY you think you need a comment
  • Try to make the code simpler and more readable before using comments.

If you HAVE to, make it count
  • Don't restate HOW the code works
  • /* Loop over users query, then set firstName from db record */
  • Explain WAHT the code is supposed to do

Don't Comment Out Code
  • Comments should not be version control
  • Nobody ever deletes it and will forget why its there for still..
  • All it offers is confusion

Joined at the Hip
  • Stale comments are far worse than no comments
  • Obligated to freshness
  • If you put a comment, you're obligated to keep that block of comment fresh and relevant.
  • Again, try to make the code more useful first.

Format and Layout

How do you lay the code out physically?

Adopt a Standard

Vertical Spacing
  • Make it easy to scan through
  • Seperate methods
  • Separate appropriate chunks of logic
  • Litmus test for complexity

Put some spaces here and there to visually break things up..

Indentation
  • Again, easy to read
  • Easy to identify conditionals, loops etc
  • Litmus test for complexity
  • If it goes too far over, you'll start to know it may be getting too complex

You Are Your Code

Conclusion

Clean Code Matters


  • Clean code is easier to read
  • Clean code is easier to maintain
  • Clean code encourages thoughtful design

Some reading:

Clean Code: A Handbook of Agile Software Craftsmanship
The Pragmatic Programmer

Friday, July 30, 2010

Adding real-time data visualization to your application or website

Using a Google map to detect users who hit his blog site..

Load last hour feature has a time-based load in. That twinkles all the logins over like 30 seconds.. oooh neeto!
http://userglobe.com/heatmap/

Sounds pretty cool, but not too sure if we'll use this much. Maybe Graeme got more out of this one.

http://gregsramblings.com/cfunited2010/

Lunch and after

Lunch was pretty uneventful. Jay and I talked about the media player a bit and about the algorithm talk I went to (optimizing code etc... See my last post)

I just installed Railo, which took all of 5 seconds and now I'm grabbing EHCACHE and going to attempt to install it and test it under Railo..

Mac Customizations and Dev Tricks

Terminal Document Reference List:
http://ss64.com/osx/

Remove the giant header in VMWare:
Terminal Command defaults write com.vmware.fusion fluxCapacitor -boolean

Spell Check in Every App:

Terminal Command defaults write -g WebContinuousSpellCheckingEnabled -boolean


Macs Developer Bundle:
http://developer.apple.com/mac/


http://www.iotashan.com/


Sercert Server Like App for MacOSX
http://agilewebsolutions.com/products/1Password


Allocate more ram to VMare to increase fusion unity

Designing creative & scalable algorithms

New ways to looking at and solving problems.

Categorizing algorithms
•Theoretical
Big-O notation
•Practical

Disk Accesses
•disk is extremely slow
Slowest part of app is probably the disk.

Be practical
How much work does it do.

Structkeyexists fast
CreateUUID super slow
Randrange not too bad with smaller ranges

Structs typically better than arrays
Queries great for many reasons

Sorting
Handled by SQL
Trust the API, it's faster

Arraysort not too useful
Structsort very fast
Querysort very powerful, slightly slower (sort on multiple fields)


Lists are not your friend
Never use lists use arrays or structs

Query of queries super fast

Merging

SQL handles

Other objects?
Array
Query
Struct

CF9 much faster than CF8

Look into read-only locks

Look at Transfer ORM

Hibernate XML
xmlSearch use it

Don't bother transforming XML

Caching
Trades memory for speed
Try to make better algorithms before leaning on caching

Cache hot spots
Cache frequented paths
Cache most expensive things

Development on a Mac

This is a whole series of things that we all probably know about, but just in case:


http://www.webapper.com/blog

Don't forget about Unix apps.

Use the GO menu (note "go folder" - kinda like terminal autocomplete)
Explore Keyboard Shortcuts (in prefs)
Secrets: (mmmm) http://secrets.blacktree.com/
More: http://www.macosxhints.com/
Use Spotlight (Or Quicksilver)
Use quickview (spacebar)

Terminal Commands:
http://ss64.com/osx/

Unix Apps:
Use macports.org
More stuff:

http://www.iotashan.com/index.php/wwsu/


Web stuff:

XAMPP
MAMP (combines Apache, mysql, PHP)
NAVICAT


Local stuff;
Menumeters







Developing Web Applications for Mobile Devices

Dan Vega
http://www.danvega.org

The presentation is here on his blog:
http://www.danvega.org/blog/



Basically Apple vs Android..

Anytime/Anywhere
Competitive advantage

click!=tap
Larger tap radius on mobile devices
Need to accomodate that.

Define your audience
Google Analytics

AdMob - one of largest ad people for mobile.

Native vs Web

Making Money
Advertising
Freemium
Niche Markets
Developing application for others

HTML / Javascript / CSS

WebKit
SQL Light
CSS transitions/Animations

Sencha touch

Enhanced touch events
Built with standards
Data integration

Battery is low.. WIll follow up with more info after talk..