How NOT to Code

Feb 05, 2010

I've been extremely busy this week, so sorry no coding challenges. I'll be starting up again next week. In the mean time, I would like to take a moment to show how bad coding has a hugh impact on an industry and technology. Case in point are the large number of comments written to Kevin Lynch's post earlier this week about the Flash on the iPhone issue.

http://blogs.adobe.com/conversations/2010/02/open_access_to_content_and_app.html

You can see a ton of comments from people who describe their negative experiences with Flash based applications. Their frustration is very clear and it could possibly affect many of us if this continues. ColdFusion faced similar problems back in the 90s. Many early applications written in CF were resource hogs and frankly shouldn't have seen the light of day. Partly as a result, the view of the industry to ColdFusion is pretty negative. I always try to point out that there are badly written PHP, ASP, .NET apps as well and it's easy to blame the language when it's clearly bad coding that is at fault.

Flash has had a similar history from the 'skip-intro' era to designers trying to write apps with the Flash timeline. Flex, I think, has done a lot to help, but there are plenty of examples of badly written Flash based applications to this day. This blog series 'How NOT to Code' is designed to help correct this problem.

Bad code exists. Every language/platform suffers because of it, and I know I may be preaching to the choir with this blog post, but I can't explain how important it is for all of us to help prevent these problems. Here  are some basic rules or guidelines I hope you can use.

1. The code is to blame. Bad code simply yields bad results. You can try to blame the issues on the hardware, language, anything. But in 99.9% of the cases I run across, it's simply bad code. Simply put, always accept responsibility if it's your fault.

2. Everyone writes bad code. I do it, you do it. The top gurus do it. We're all human and we all write bad code from time to time. The key is to realize when it happens and to fix it as soon as possible.

3. Test, test, test. Look into unit testing and proper usability testing. Create a procedure to test your applications under load and optimize your code. Remember, fixing a bug after the fact takes far more time and resources and can easily kill your productivity.

4. Never accept subpar performance. This is trickly but it's also vital. You may be doing your best, but all the developers on a team need to be dedicated to the process. Managers also need to understand that sending code out quickly will cost more over time. Take a small amount of additional time to ensure quality. For example, I love watching Gordon Ramsey's Hell's Kitchen and Kitchen Nightmares. You can apply the same principles to any discipline. Quality always comes first, not speed. Once quality is ensured, efficiently will develop in time with experience and under proper management.

5. Know your limits. In many cases, I see the wrong tool being used to addressed a problem or a very first attempt to solve a problem being applied. Let's face it, we all know when we have reached our limits. We copy/paste someone else's code or we struggle to work through the problem. This is the point we can cause the most damage. Make your best first attempt, but don't even think of trying to use it. If you have other team members, have them try to develop their own solutions. Once this first round is done, compare the ways everyone tackled the problem. If you don't have team members, ping a coder list, etc. After the first review, make a second attempt - this should be far better. You may be able to use the second attempt, but I would make a point to comment the code or make a note to revisit the problem in the near future. In many cases, I'll revisit the code 6 weeks later and work up a third solution which will work even better.

6. Always revisit code. Management needs to fully understand that time has to be taken to revisit and refactor code. This is trickly if you spend 80% of your time chasing bugs, but it's vital if you want to reduce the number of problems. When revisiting and refactoring code, it's very important to use unit testing. Fixing a bug or problem that produces more bugs is never  a solution.

7. Never write 'large' applications. What do I mean by this? I think most developers mess up here. They get a request to build a large app and things just go down hill from there. Every large application is simply a collection of very small apps or components. That point of view is very important. You want every component of the app to encapsulate itself and to be as autonomous as possible. For example, a shopping cart may have a component that determines the shipping cost. Think of that as a very small shipping cost app that the cart calls. When you develop in this manner, the code becomes more flexible and easier to maintain.

8. Always avoid esoteric code. If only you and god knows and can understand what you coded, you made a big mistake. In 6 weeks, you'll forget what it did as well. This is easy to catch  and you should understand it when it happens. It's usually very large procedural code, and it runs through a ton of changes or operations to handle a simple problem. This type of code either contains no comments or is heavily commented as the coder tries to explain the logic. Again, break the code down into its parts and rework it. Compare other people's solutions to the problem.

9. Comment your code, but don't over comment. Some comments are completely superfluous and silly. Well written code reads like a novel to me. Variable and method  naming should make sense by itself. It may be slightly more to type but it's well worth the effort.

10. Learn, learn and learn some more. Never become a one language developer. Always try learning new languages each year and how they tackle problems. You'll be surprised how much you get from this. A cool thing about this point is this. You actually learn more from your bad code! When you learn a new language - most of your inital code is bad and you make a ton of mistakes. It's in the process of fixing those mistakes that you learn. That's why as developers become more proficient in a single language their skills may actually drop off over time. They solve the same problems with the same tools and never grow.

 

Apple v Adobe. Who's right?

Feb 03, 2010

There has been a lot of talk lately due the release of the iPad without support for Adobe Flash. Steve Jobs went a step further to call Adobe lazy (http://www.wired.com/epicenter/2010/01/googles-dont-be-evil-mantra-is-bullshit-adobe-is-lazy-apples-steve-jobs/comment-page-4/).

Here's the actual quote:
"They [Adobe] are lazy, Jobs says. They have all this potential to do interesting things but they just refuse to do it. They don’t do anything with the approaches that Apple is taking, like Carbon. Apple does not support Flash because it is so buggy, he says. Whenever a Mac crashes more often than not it’s because of Flash. No one will be using Flash, he says. The world is moving to HTML5."

Now for full disclosure, I'm more of an Adobe based developer who uses Adobe's Flex, Flash and ColdFusion. I have written some Objective C, and I'm actually currently working with Scala. I'm not an Adobe or an Apple fan boy. The great thing about the Internet is we can do some simple research and cut through the company spins and see who's right.

Here's basically the breakdown of the argument so far..

Apple: Flash isn’t good enough.
Adobe: We need Apple’s help.
Apple: Adobe is lazy.

So what does Adobe say they need help in? One of my sources says that they are asking for access to the API structure for the hardware video accelerators that Apple uses (basically the ability to use the GPU for graphics processing). As a developer I did a quick search for those APIs and what Adobe is talking about. Here's what I found..

Mac uses OpenGL
http://developer.apple.com/mac/library/navigation/index.html#section=Frameworks&topic=OpenGL

iPhone and iPad use OpenGL_ES
OpenGL_ES - http://en.wikipedia.org/wiki/OpenGL_ES

Apple does have SDKs for the kernel
http://developer.apple.com/sdk/

Apple also provides Carbon as the API language for the MaxOS systems
http://en.wikipedia.org/wiki/Carbon_(API)

The Kernel itself is XNU and it's under the Apple Public Source License. An I/O Kit is available as well.
http://en.wikipedia.org/wiki/XNU

Apple actually provides a lot of resources for developers, but there is a catch. They are directing development into their Objective-C language. This is a very real problem for Adobe.

Can Adobe just port what they have into Objective-C or use Carbon. Unfortunately no, the Flash Player is written in C++ and going from C++ to Objective-C is not very practical.  Objective-C is just another superset of C. It simply adds some OOP logic and a messaging and some of the syntax is similar to Smalltalk. You can compile any C program into Objective-C but that's not currently possible to do with a C++ program. So Adobe would be left to develop two separate 'cores' for the Flash Player, one for Apple platforms and another for everyone else. It is interesting that Adobe mentions access to the GPU when that was a recent feature to the way the Flash Player has worked anyway - it use to run everything including graphics  directly off the CPU which was a performance issue.

Another concern is how any of this was a surprise for Adobe. Jobs has been promoting Objective-C since he first left Apple and started up NeXT in the 80s. It's been fairly obvious that Apple is going to push their view on this and 'encourage' people developing software for their platforms to use Objective-C. Adobe has no weight in this decision and probably shouldn't waste its energy trying to fight it.

Apple is forcing developers to go into Objective-C and Carbon to develop for their platforms. I think this is a simple business decision because they feel Objective-C is more flexible than C++. It, however, doesn't make life easy for Adobe. Keep in mind, this will affect other popular Adobe products used on the Mac platform like for example Photoshop.

I can understand why Adobe wouldn't want to use Objective-C and end up supporting two 'cores' of the Flash Player. Adobe wants Apple to help. Apple is basically saying 'no, build your own if you don't want to use the ones we provide'. From this research, I think Apple has a point. If this analysis is wrong, by all means correct me.

[Warning - stepping on my soapbox]

How did Adobe responded to this?

As normal, there wasn't an single organized response. Many in the community gave rather anemic responses like the  iwantflash.com and isflashdead.com websites, but one of the responses from an Adobe employee did completely surprise even me. Adobe evangelist, Lee Brimelow, compiled a montage illustrating his inability to view various sites including a porn site?

http://www.wired.com/gadgetlab/2010/01/adobe-porn-flash/
http://theflashblog.com/?p=1703

Another case of very bad judgement from an Adobe evangelist. To be fair, Lee did apologize, but the point I wish to make is that this has been a fairly consistent problem for Adobe, and it's very hard to take Adobe's side when they can't seem to provide an intelligent response. The leadership of Adobe needs to address this and determine a better method to make their responses. Adobe's PR is like watching a monthly train wreck.

I truly hope the Open Screen Project (http://www.openscreenproject.org/) works out, but there are many challenges for Adobe these days. The Flash Player is far from perfect. Most of us know all too well the problems it has. FP 10.1 should help, but Jobs has a point in that Flash based apps aren't known for their performance. Is HTML 5 going to completely replace Flash? No, but it could take some significant market share. The same holds true for the popular Javascript libraries or the Unity3D engine (http://unity3d.com). To illustrate my point, I used Shockwave years ago, but I can't tell you the last time I opened up Director. Currently, I enjoy using Flex, but I understand that it is very possible I'll be using something else in 10 years. The fact is that technology changes rapidly. Several of the Adobe fan boys have complained that Apple isn't playing fair. My response is simple, they really don't have to. Apple is forcing people into their supported languages and APIs. They simply have the weight to do this. Adobe is the smaller player and has to deal with it. Jobs called the folks at Adobe lazy. My advice to Adobe is very simple - make an intelligent response and prove him wrong!

How NOT to Code - Challenge 10

Jan 28, 2010

Today's challenge is in javascript. Can you find all the problems with this code? On some browsers you may see a double greater sign on the age and gpa conditionals. You can simply view that as a single greater than sign. Sorry I think this a small issue in encoding via this blog software.


<script type="text/javascript">
   var gpa = 3.92;
   var year = "4";
   var age = 22;
   var id = "123";

   if (id == "123"){
      if (age » 21){
         if (year = 4){
            if (gpa » 3.5){
               if (id === 123){
                  alert('good student');
               }else{
                  alert('bad student');
               }
            }
         }
      }
   }
</script>