Saturday, May 25, 2013

Finally, my computer does what I want it to do

Computers have always been so darn literal.

And that is usually a very good thing.

Today, for the first time that I can recall, my computer did what I wanted it to to, not what I told it to do.

~$ cd ~/tmmp
/Users/lex/tmp $

Enable Typo-Fixing Magic in your Shell

Put this handy command in your ~/.bashrc file:

shopt -s cdspell

Other Handy Bash Shell Features

The OSX terminal is loaded with handy features
  • Tab Completion
  • Command History
  • Directory Switching
  • Short Cut Keys

Tab Completion

Type the beginning of file or directory name, hit TAB and the rest of the name will be typed for you.

Command History

Up arrow to see previous command(s).

Down arrow to go back down the list of commands.

Directory Switching

Type cd - to switch back to previous directory.

Short Cut Keys

KeyDescription
CTL+AMove cursor to beginning of line
CTL+EMove cursor to end of line
ESC+FMove cursor to beginning of next word
ESC+BMove cursor to beginning of current word
ESC+DelErase previous word
ESC+DErase word

Friday, May 24, 2013

Useful Git Scripts

Anybody that has worked with me closely knows that I have a bash script for nearly everything I do in the terminal.

I'll put some git related commands here as time permits...

remove-files-from-git-repo-that-have-been-deleted-locally


#!/bin/bash
# Usage: remove-files-from-git-repo-that-have-been-deleted-locally
#
# Stage the removal of the deleted files and then commit
git ls-files --deleted | xargs git rm
git commit -m "remove-files-from-git-repo-that-have-been-deleted-locally"

Other options that are not so great

The following only works for a limited number of file deletions:

$ git rm $(git ls-files --deleted)
-bash: /usr/local/bin/git: Argument list too long


The following will not only stage deletions, but will also add any other file that has been changed and has been previously referenced by git.

git add -u .


The following similar to git add -u ., but it also adds new files.

git add -A

Sunday, May 12, 2013

Application Architecture Considerations


Here's a list of things I consider when evaluating an application's architecture:

Functionality

Does the application satisfy its business requirements?

Performance

Does the application run fast enough?

For example, if there are any views that take longer than 7 seconds to display, then you need to re-engineer something.

Scalability

How well does your application scale?
  • Can you easily add and remove components without affecting your application's performance or reliability?
  • How loosely (or tightly) coupled is your application code?

Messaging

  • How robust is the message bus/messaging framework?
  • How does your application handle events?

Memory Management

Does your application architecture provide memory management/garbage collection mechanisms?

Testability

How easy is it for developers to test features of your application, e.g., memory management or use case functionality?

Error Handling

How does the application handle errors and exception cases?

Logging

How does the application handle logging of normal and/or exception cases?

Consistency

Does the application code look like somebody cared or written by a disparate group of developers with their own styles and whimsical coding techniques?

Extensibility

How easy is it to extend the functionality of the application?

Reusable Components

How well does the application make use of reusable components?

Security

Does the application handle security?
  • Data Security
  • User authentication
  • Authorization
  • Accounting/logging of Security Events

Maintainability

  • How many lines of code does your application require to satisfy its functional requirements?
  • How readable is your code? (Fancy one liners that are unreadable add to technical debt.)
  • How defect-free is the application?
  • How long does it take to add functionality give new enhancement requirements?


Destructuring Assignment: Another Case for CoffeeScript

Often times, a single options parameter is passed into a JavaScript function.

In that called function, we will then split up the options argument into separate variables to used later.

Javascript

Here's how it's frequently done in Javascript:


var options;

options = {
  id: 1,
  name: "bob",
  address: "123 main street",
  city: "new york",
  state: "ny",
  zip: 10027
};

({
  myFunction: function(options) {
    var address, city, id, name, state, title, zip;

    id = options.id
    name = options.name
    title = options.title
    address = options.address
    city = options.city
    state = options.state
    zip = options.zip;

    return console.log('name', name);
  }
});


CoffeeScript

Here's how it can be done in CoffeeScript:


options =
  id: 1
  name: "bob"
  address: "123 main street"
  city: "new york"
  state: "ny"
  zip: 10027
  

myFunction: (options) ->
  { id, name, title, address, city, state, zip } = options  # << Destructing Assignment
  console.log 'name', name


The CoffeeScript implementation, using Destructing Assignment is a nice improvement.



Javascript 1.7

Granted Javascript 1.7 provides Destructing Assignment, but if you rely on that, then you'll need to worry about end user browser compatibility and the higher likely hood of developers falling into the trap of using proprietary extra non-cross-browser features, e.g., Mozilla's for each.

Conclusion

This is one small example of why you should be using CoffeeScript instead of raw Javascript.

Here are a few more reasons to use CoffeeScript:
  • Fewer lines to write and maintain
  • More expressive (easier to understand) code
  • String interpolation
  • Consistent, readable, pretty-printed generated Javascript
  • Generally runs faster than typical Javascript hard-coded "equivalent" code


References

http://jashkenas.github.io/coffee-script/
http://js2coffee.org/


Friday, May 10, 2013

Rails Generate Controller Commands

Typical Rails Generate Controller Command

Rails generate commands are great for cranking out boilerplate code that you can later go back and customize.

Here, I create a Rails Controller and associated files...


$ be rails g controller Application index static
Rails.env: (development), using rails_env: (development)
    conflict  app/controllers/application_controller.rb
Overwrite /hs/dev/find-activities/api-fa/app/controllers/application_controller.rb? (enter "h" for help) [Ynaqdh]
       force  app/controllers/application_controller.rb
       route  get "application/static"
       route  get "application/index"
      invoke  erb
      create    app/views/application
      create    app/views/application/index.html.erb
      create    app/views/application/static.html.erb
      invoke  test_unit
      create    test/functional/application_controller_test.rb
      invoke  helper
   identical    app/helpers/application_helper.rb
      invoke    test_unit
      create      test/unit/helpers/application_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/application.js.coffee
      invoke    scss
      create      app/assets/stylesheets/application.css.scss



Rails Destroy Controller Command

I decided that I did not want the test files. So, I removed all the files that were generated

$ be rails d controller Application index static
Rails.env: (development), using rails_env: (development)
      remove  app/controllers/application_controller.rb
       route  get "application/static"
       route  get "application/index"
      invoke  erb
      remove    app/views/application
      remove    app/views/application/index.html.erb
      remove    app/views/application/static.html.erb
      invoke  test_unit
      remove    test/functional/application_controller_test.rb
      invoke  helper
      remove    app/helpers/application_helper.rb
      invoke    test_unit
      remove      test/unit/helpers/application_helper_test.rb
      invoke  assets
      invoke    coffee
      remove      app/assets/javascripts/application.js.coffee
      invoke    scss
      remove      app/assets/stylesheets/application.css.scss



With No Test Files

Here, I generate the files without the test files.


$ be rails g controller Application index static  --no-test-framework
Rails.env: (development), using rails_env: (development)
      create  app/controllers/application_controller.rb
       route  get "application/static"
       route  get "application/index"
      invoke  erb
      create    app/views/application
      create    app/views/application/index.html.erb
      create    app/views/application/static.html.erb
      invoke  helper
      create    app/helpers/application_helper.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/application.js.coffee
      invoke    scss
      create      app/assets/stylesheets/application.css.scss