Thursday, March 20, 2014

Is it fast enough - the lost art of building fast programs

When I was writing programs for the stock market speed and memory usage were huge issues. You dare not leak any memory because 100,000 headlines coming your way in a 24 hour period with a memory leak was a sure way to crash a program.

Speed was also very important. In the news business being first, and maybe only, source to get a headline out was critical. It needed to be on the screen right away. Turns out accuracy from the source was of less importance, they could always correct things in the story body later.

After the stock market company outsourced everything to India I work on tamer programs that did not use real-time data. Of course you still don't want to leak memory and you want things to be fast but the thrill and stress of that waned considerably.

Writing mobile apps brought a lot of that back. Not only do you want to avoid memory leaks you want to have as small a footprint as possible. The CPU / GPU combination on a mobile device is much slower than the desktop you are developing on. Speed is again a big factor. No one wants taps and swipes to feel like molasses. You have much less screen space to deal with and you need to use it wisely. Everything was about speed, space and time again. The thrill was in the air. I never stopped coding like size and speed mattered but the importance bubbled to the top again and I loved it.

Right now I am helping out with a web application using JavaScript and AngularJS. I have used JavaScript with JQuery in the past and I find AngularJS to be big step in the right direction. The app really just needs to be fast enough to be usable. Simple request / reply / show it on the screen operations. Speed is in no way critical and really is just an afterthought if considered at all. We are also using a number of JavaScript libraries including Underscore.js.

As I use various methods in these libraries I wonder about their speed and efficiency. You do seem some mention of that when you query about Underscore array methods on Stack Overflow. I don't feel enough attention is focused here and I try to find and use the most efficient method for my code. Sure, it is not bad to make a new copy of an array to add or remove a few elements but if that array grows tenfold what happens?

It is so easy to look at code and think each line of code takes the exact same amount of time to execute. Here is this wonderful AngularJS controller and it is only 20 lines of code! Dang it must be fast because when we did something similar in JQuery it was 100 lines. Sadly reality does not work that way. Each of those 20 lines can be running all sorts of framework code. That one call to an Underscore method might be 50 lines in itself. Dynamic injection is happening. DOM changes are occurring. Watch methods are being called. Other controllers are receiving messages and running lines and lines of code.

It is wonderful when the framework takes care of the busy work. I want to focus on the business logic at hand. I love it when a few lines of code updates four separate areas of the screen. I have written multithreaded framework code to perform those types of actions. Set a variable to false and have everything that cares disable - the menu item, the toolbar button the right button menu option, etc. If the user of my framework put the call to toggle the setting between true and false in a loop it looked innocent but man a lot of stuff was happening in the background.

I still want to know what is going on behind the scenes. When I called that line what was the ripple effect? If I add an item to that collection does it create a whole new collection and return it to me or just add it? Did it walk the entire collection to add it to the end or is the push operation fairly native? Does it scale well or do I need to use a different object or collection or framework to do this job? Will the client have 10 of those or 1,000 of them?

Speed is hard to add but easy to lose. Optimizing early can be a waste of time. Not thinking about optimizing at all can kill a project. Right now I am trying to learn so many new things in the JavaScript arena it is hard to know if I am doing it correctly. As I write the code I try to look for the optimal way to do it. Luckily code reviews help point out when I missed a better way. Taking time out to find web references is also needed. I try to scan over all the available methods in a library even if I don't need them now just in case I need them later. I may not remember the exact name but if I remember there was a method I can go back and find it and give in the in depth evaluation pass to decide if it is usable.

Tuesday, March 11, 2014

Let's have some IDE fun

During any given day I might be in 6 different IDEs. Eclipse, IntelliJ, Android Studio, Xcode, AppCode, and Aptana. Generally I have an instance of Sublime text editor running too. I use them for the following things:

Eclipse - Java server side work (our REST services)
IntelliJ - Java one off utility work
Android Studio - Java Android mobile development
Xcode and AppCode - Objective C iOS mobile development (some Mac work too)
Aptana - JavaScript with AngularJS, bootstrap and other libs
Sublime - taking notes, small file edits including .gitignore and other system type files

At times it can drive you crazy. I use a Mac at work due to my iOS needs and depending on the IDE I might use CMD+S to save or Ctrl+S. Since I have a PC background and I switch to Ctrl+S at home in any IDE I am in when there. I screw up the keystrokes at times but generally have a feel for which IDE I am in at any given moment.

Eclipse is the old standby I have use for years. There are a ton of plugins for it to do nearly anything but you can't always get them to work as expected or even together. We just switched from SVN to GIT at work. Probably need to throw out the SVN plugins as I will not be using them and no real need for them to load up each time. Also the menu system gets a bit cluttered. Heck it might be a good time to start with a fresh Eclipse install as I also have the Android plug-ins running and I have switched to Android Studio for that work. Code completion works ok but is not spectacular. Refactoring works and has not crashed on me. The editor is fast and has a ton of features.

IntelliJ has a great Dracula dark theme built right in. I run a dark theme in Eclipse too but it does not theme everything perfectly. I handle one off programming in IntelliJ. Testing code bits, working on small stand alone utilities. I could do some of that in Eclipse too but I tend to have the server stuff open there and switching workspaces in Eclipse is slow as it is a full reload of the IDE. I could use workspaces in Eclipse and I do at times but our server stuff is rather large so I prefer to leave it sit by itself.

Android Studio is an IDE I like a lot as it is IntelliJ based. There have been some growing pains with the constant updating to the newest version of gradle but in general the IDE is superior to Eclipse. It has better code completion, a better preview window for your XML based layouts, shows the colors and icons in the gutter, makes it easier to convert string into the string table, shows the replacement string in code instead of the string ID and a bunch of other smaller bits of "thanks for doing that" areas. I would say that Eclipse is faster at building and getting the code on a device but I am willing to forgo that speed for all the other features Android Studio offers. It works well with Genymotion, the device emulator I use.

Xcode is my least favorite IDE of the bunch. I have to use it for iOS development but it is behind the times in areas like refactoring and its code completion tends to not be as helpful as other IDEs. Sure, you get a full list of what is possible but it does not move the most commonly used ones to the top of the list. It also crashes on me more than any of the other IDEs especially when it comes to refactoring. I have had it crash when refactoring a variable that only appears twice in a 20 line method. It makes you scared to use it. It was also terrible when it comes to using SVN. GIT is much better. I wish it would offer to automatically include needed H files instead of my manually moving to the top of the file to type them in.

AppCode is also from IntelliJ. I use it when I am doing heavy Objective C coding when I already have the UI in place. So far its refactoring has worked every time crash free. It has more intelligent code suggestions, if you add a reference to an object it will prompt to add the proper H file, it handles CocoaPods nicely. There are so so many areas where it is coder friendly. Sadly it does not have an Interface Builder replacement so you end up back over in Xcode to handle those chores. They sync together without a hitch and both use the same project files making moving between them relatively painless.

Aptana has some nice code completion aspects for CSS and HTML along with a solid JSHint tie in. Since our server build does not allow JSHint errors to pass it makes it so much easier to catch them before you push to master. It is Eclipse based but focused on JavaScript which means I could end up cluttering it with plugins but so far I have kept it down to the minimum I need for JS programming. I have used this IDE flavor for the least amount of time of those listed but with my heavier involvement in JavaScript I have been very happy with it.

Sublime is my go to editor. I leave multiple tabs open with "To Do" lists for each of the projects I have going at any time. This allows me to type in quick notes so I don't forget something because people tend to drift in and out of my office with suggestions the mobile app, to share some tidbit of useful information about the server, a GIT command or something else helpful. I have a file for general work info, one for Android and one for iOS info open all the time and then I open and close other files as needed. The color coding support in Sublime for a multitude of file formats is awesome. I also use it to pretty print XML and JSON when I copy / paste server responses out of the Chrome web tools window. 

It can fry your mind to shift in and out of so many programming languages and IDEs in any given day. Of course I try to keep it to a minimum but that is not always in my control. My preference would be to stick in the world of mobile doing Android and iOS development. Right now that is on hold so I am helping out on the JavaScript team. Lots of time on Stack Overflow and doing web searches as my brain does not have a solid cache of JavaScript programming idioms and patterns. Looks like Aptana it is for the next few months.