Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 2.20 out of 5)
Loading ... Loading ... |
  • E-mail this story to a friend!
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Slashdot
  • Facebook
  • MySpace
  • Google Bookmarks
  • DotNetKicks

Coding Horror 01: Readability vs Efficiency

My First Official "Coding Horror" Encouter

My First Official "Coding Horror" Encounter

Back in College, I have a friend who’s programming prowess is so godly that it can surpass or match mine :D . He is a C++ Junkie and belongs to the Java Cult. His mental agility is so great that he can beat me in writing complex algorithms anytime of the day. In fact, I chose him to be the “Architect” for our project in Compiler Theory Class (Kompayler Studio 2008: The First Tagalog Kompayler, Programming Language and IDE). He is THAT great of a programmer… BUT

Being a Great Programmer is not the same as being a Great Software Developer

“Aren’t programming and software development the same?” Uhhmm, NO. Programming is just a single part of Software Development. Software Development, on the other hand, is an entirely different song and dance.

In one of our converstations, I told him that instead of reading books about programming he should try to read books about software development. I gave him that advice not because I want him to suck in programming (I’m not that envious). I gave him that advice because I want him to realize that programming alone won’t make a great software product and that programming prowess alone won’t give him the best programming job around… at least here in the Philippines.

One of our main differences is reflected on how we write code (which eventually shows in our software). For example, say that we have a project that requires us to deliver a Simple Number Sorting Application that will ask the user to input 50 numbers and sort it. This project is resource constrained in such a way that we need to deliver it in 2 days.

What I would do is use a pre-existing bubble sort algorithm available in the class library (in .NET, I suppose), then I would add additional functionality, then create a test harness (unit test) to test my functions and then I’m done with the “coding” part. Then I would use the rest of my time to design an intuitive user-interface, provide a user manual and work on the packaging.

My Simple Sorting Application

My Simple Sorting Application

On the other hand, my friend would create a complex multi-threaded sorting algorithm that will have a runtime efficiency of  O(Log N). Not only that, it will be written in Haskel (or any other hardcore programming language) and it will also be invoked via command-line! So hackerish. :D

His Complex Sorting "Tool"

His Complex Sorting "Tool"

My point is simple… In Software Development, there is a thing called Tradeoffs. What do I mean by Tradeoff? Take the above example. I traded-off the uber-cool complex multi-threaded sorting algorithm to USABILITY AND FUNCTIONALITY which, by the way, has more practical and positive impact to my users. You could argue that I suck for using bubble sort and not the complex multi-threaded sorting algorithm and you could also argue that my bubble sort has a runtime complexity of O(N^2). But being a Software Developer, I know that using that “complex multi-threaded sorting algorithm” would only be faster by 0.000000000000001312 seconds (remember, we are only sorting 50 elements) and that “improvement” will not be of any significant use to my end-users and I might as well use my time in creating extra  features, such as Print and Copy to Clipboard, that will surely make my product more functional and easy to use.

At work, I am working on a project to “migrate” our products to Vista. To Migrate meaning “to make it compatible with Vista”. One of our product has an Autorun executable which will decide which “components” (SQL Server, VC++ 8.0 Redistributable, .NET Framework) to install. During a routine test run at the Test Deparment, they found out that this certain product is not working on Vista and filled a Track against it. The said Track was assigned to me and I’m expected to fix it. So I pulled the Visual Basic 6.0 source code of the Autorun executable from our TFS and performed an investigation. While investigating, I saw a very weird block of code…

Private dim VCREDIST as long
Private dim NETFRAMEWORK as long
Private dim SQLSERVER as long
Private dim COMPONENT1 as long
Private dim COMPONENT2 as long
Private dim COMPONENT3 as long
Private dim COMPONENT16 as long
 
VCREDIST = 1
NETFRAMEWORK = 2
SQLSERVER = 4
COMPONENT1 = 8
COMPONENT2 = 16
COMPONENT3 = 32
COMPONENT16 = 65536

If you see a 1 or a 0 in the code, its pretty normal because you know that it is a flag for something . But if you see a 7 or 16 or 32, you know something is wrong. So I ignored that and went straight to the actual function that does the shell execution to install the component and I saw something like this…

While ComponentInstalled != 13
 
	installNum = GetNextComponent	
 
	if installNum AND 1 then
		InstallComponent(installNum)
	else
		installNum = installNum * 2
	end if
 
End While

I was dumbfounded and stupified, why is there an “AND 1″ in that code? and what is the purpose of the * 2? So I asked my manager for his input. And after staring at the code for 30 minutes, my brilliant manager shouted “That code is using BIT PATTERNS!“. Instantly, I understood what the code does.

VCREDIST = 1                 '0000 0000 0001
NETFRAMEWORK = 2        '0000 0000 0011
SQLSERVER = 4              '0000 0000 0111
COMPONENT1 = 8           '0000 0000 1111
COMPONENT2 = 16          '0000 0001 1111
COMPONENT3 = 32          '0000 0011 1111
COMPONENT16 = 65536    '1111 1111 1111

That block of code is actually a binary pattern used for flagging the components. Each placeholder in that binary pattern is a FLAG (1 or 0) which actually tells the function GetNextComponent if that component should be installed or not. The *2 is used for shifthing the binary pattern so that the active placeholder for a certain flag will be at the rightmost (last bit) of the pattern! Brilliant!

To solve the issue, I just needed to force the Autorun to Install VCREDIST in Vista by adding this line of code in the Vista Condition of the GetNextComponent function:

VCREDIST = VCREDIST OR 1

So it will always install the Visual C++ 8.0 Redistributable in Vista. The actual problem is that the Autorun Executable thinks that Vista does not need the redistributable and will not install it.  That’s why our product that needs the redistributable will not work.

I have great admiration for the brilliance of the programmer who wrote this. The method is so efficient that instead of declaring 12 Integers for Flag, he would only need to use a single long! My manager told me that the one who wrote this application is actually a “CONTROLLER” programmer. A type of programmer in our organization that writes the firmwares for our hardware controllers.

When writing firmwares that needs to be tight and needs to run in realtime, time complexity is your biggest enemy and you need to code against that. But in an Autorun application for installation, you DONT! In this example, Readability and Maintability was traded-off to efficiency that was not even needed! In turn, a simple issue that can be fixed in a matter of seconds was fixed in half a day.

As Software Developers, we should have the right mindset to decide on what trade offs are necessary. We should not only be Great Programmers who can write great codes but we should transcend to being Great Software Developers who can “take a concept, build a team, set up state of the art development processes, design a software product, the right software product, and produce it. Not just any software product: a high quality software product that solves a problem and delights your users. With documentation. A web page. A setup program. Test cases. Norwegian versions. Bokmål and Nynorsk. Appetizers, dessert, and twenty seven eight-by-ten color glossy photographs with circles and arrows and a paragraph on the back of each one explaining what each one was.”

As for my Friend, he is now working at Oracle. My only wish is that he would be able to work on their database management system where efficiency has higher value than code-readability.

View CommentsCoding Horror 01: Readability vs Efficiency

  • eldred

    lol!,yeah right.got to find a job though to be a real "boss" :P

  • eldred

    most of the bit patterns that are used as flags and used in game state flaggings and storage of information in early 8 bit generations, the memory was so small that they need to maximize usage.

    It was really interesting when I found it in my early VB6 days. Good post for bringing it up from the vault.

  • hmmmmmm, interesting…. hahaha

    naasar nga ako dun sa teacher ko ng subj na comsci d2 sa canada…… sabi nya ang haba ng daw ng code ko.. di daw efficient sa memory…. ang sabi ko naman sa sarili ko "sa bilis ng computer ngayon, halata pa ba yung difference? at bat pa kailangang mag tipid ng memory eh ilang kb lang naman yung program, at simple lang. imposibleng magjaroon ng memory leak."

    so yun, hahahaha, kasi mas madali para sa akin i-modify yung code dun sa way na ginagawa ko, at flexible sya in case I would want to use it for a future project…… so yun….. kapilitan, kailangan kong paiklian yung code to the point na, pag kailangang i-update or modify yung process, eh kailangang mag write ng new set of code for a new program…… haaaaaay……

    so yeah, that's my share…… :D

  • Anonymous

    Just by simply looking at the values 1,2,4,8,16 and 32, you should have guessed it is in binary format. It’s easy blame the original programmer(s) throwing the argument of readability but in that situation you had, I think it’s just that you don’t have enough knowledge on that particular language. That should be easy to solve if you know what you are doing.

    Your sorting example is way exaggerated too. Even a hardcore programmer would not attempt to do multithreading stuffs just to sort 50 items.

    • Uhhh, no. Newbie programmers (Like any FRESH Graduates) doesn't have the tiniest bit of clue about bit patterns. They just don't have the experience. Don't blame us, blame the "Java Schools". Its just the way that we are taught, its all OBJECTS now.

      On the other hand, you are correct, the example is exaggerated. But you get the point.

      Still, point still stands. You can't EXPECT every programmer to understand your code on first look. IF efficiency is NOT NEEDED, why sacrifice readability?

  • Goldwyn

    What the fuck is bit patterns?

    astig…

    This is the best blog post so far…

  • Joshua

    sino yan? hahhha

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

blog comments powered by Disqus