Category Archives: Languages

Redux’s Connect Method Explained.

Sorry but all the explanations this far have been bad so I thought I’d simplify it.

In plain Redux without connect you need to manually bind up your store.dispatch to your components as well as subscribe to updates so you can call store.getState(). But your components are suppose to be dumb.

const store = createStore(reducer);

const render = () => ReactDOM.render( // turns ReactDOM.render into executable function
  <Counter
    value={store.getState()}
    onIncrement={() => store.dispatch({ type: 'INCREMENT' })}
    onDecrement={() => store.dispatch({ type: 'DECREMENT' })}
  />,
  document.getElementById('root')
);

render();
store.subscribe(render);

So rather than manually linking dispatch and subscribing to updates we create two mapping functions, both of which bind the dispatch and the state to props of the ‘connected component’. So in this case all updates to this.store.getState().value are mapped to this.props.value and rather than mapping dispatch directly, we just wrap it and pass it in as a prop.

// create component
class ContainerComponent extends React.Component {
  render() {
    <PresentationComponent 
      value={this.props.value}
      onIncrement={this.props.onIncrement} 
      onDecrement={this.props.onDecrement} />
  }
}
// create state mapping
function mapStateToProps(state) {
  return { 
    value: state.value
  };
}
// add dispatch mapper 
function mapDispatchToProps(dispatch) { 
  return { 
    onIncrement: function() { 
      dispatch({type:'INCREMENT'}); 
    }, 
    onDecrement : function() { 
      dispatch({type:'DECREMENT'}); 
    } 
  }; 
} 
export default connect(mapStateToProps,mapDispatchToProps)(ContainerComponent);

Now when you call your components you can simply call the value passed in like so.

export default class PresentationComponent extends React.Component {
render() {
    return (      
        <div>
<span>{this.props.value}</span>
<button onClick={this.props.onIncrement}>Increment</button>
<button onClick={this.props.onDecrement}>Decrement</button>
        </div>
    );
  }
}

For more information you can checkout both of these github projects for reference.

Use Multiple Languages

Using a language exclusively for all your programming task is not productive. While knowing the all the secrets of one language makes you proficient in that language, writing a 100 line Java class to do something a 15 line Ruby script or 20 line PHP script is a waste of time.

Here is my current language use:

  • Java – Robotics, Vision Processing, Anything Performance Intensive
  • Groovy – Launching anything written in Java, this saves me the time of compiling and writing complex shell scripts to launch the Java program.
  • Ruby – General Automation Scripts or Scripts doing emailing or sending data over the network
  • Batch/Shell – I use these as launchers for backup/tar/gz or running CRON or other scripts on a set schedule.
  • PHP – Everything web, while I could use Rails, for some of this, I’ve not had the need to create a full website using all the ins and outs of this framework. See article on “Don’t Use Frameworks” for justification.
  • C or C++ – I use these for interfacing with device drivers or writing JNI (Java Native Interface) wrappers.
  • C# – I use for writing windows only programs.

A good idea if you only know one or two languages, is to find the right domain for your language. You may never have the need to use more than two, but if you find yourself struggling with something in a language, or are sick of how unproductive you feel, get a book and start experimenting. Look at WikiPedia for a list of comparison of programming languages.

This year I am planning on doing more with with Scala or Clojure.

Use Multiple Editors

Contrary to Dave Thomas’s maxim, pick a text editor and know it well, I think sometimes multiple text editors are more productive than just using one for everything.

Some of the reasons you may need to use different text editors:

  • Different Programming Languages
  • On different systems (remote SSH vs. Local)
  • Multiple Computers (Home, Work/Client Site)
  • Something fast and easy vs. something large and complicated

Personally I use the following editors:

  • Eclipse – PHP, Java, Groovy, Some Ruby
  • Notepad++ – ColdFusion, HTML, CSS, JavaScript, Ruby, C
  • VIM – Bash, on the fly edits for PHP, Java, Groovy, Ruby
  • Visual Studio – C++, C#, VB

Do I know everything about all the editors, no, but for the amount of time I spend in each of these different languages, I know enough about each editor to be the most productive in the time I have.

Whatever combination you pick follow this rule: Pick the editor with the 20% of features that are easiest  to use that allows you to do 80% of your work. Here is a list of text editors in Wikipedia to help you decide.