Matt Gornick

Code Refactoring, Optimization, and Pareto’s Principle

Posted in Uncategorized by mgornick on July 11, 2008

Any successful programmer will say that they would love to write the cleanest and most optimized code around.  Unfortunately, bugs arise, code is ambiguous, or you don’t have the time to rethink an algorithm to optimize its performance.  This leads me to the three points in the title:

Code Refactoring, simply put, is rewriting already working code “to make it amenable to change, improve its readability, or simplify its structure, while preserving its existing functionality.” [Wikipedia]  In a lot of cases this is a good idea.  Especially if others need to review your code, maintain functionality, or add new features in the future.  This starts to become a problem when you try to do too much at one time.  I’ve noticed that a lot of people group refactoring and optimization into one large category of chaos.  In my experience, it is best to optimize then refactor or at least take your time optimizing so that refactoring isn’t needed.  This will allow you to create more efficient working code then make it readable to others.  Doing them both at the same time will confuse you, reviewers, and the compiler (error, seg fault, …ah).

Example [Wrong way]:

Coder: Wow, I could really use a HashSet instead of an ArrayList to optimize this code. [Start rewriting…] Function xyz is really messy, I’ll clean it up then come back to the HashSet structure.  Function 123 is messy too …

Example [Right way]:

Coder: Wow, I could really use a HashSet instead of an ArrayList to optimize this code. [Start rewriting … Finished with change in structure … unit tests were successful …] Function xyz is really messy, I’ll look to see if its still needed and then try to clean it up.

Non successful coders will forgo inefficient working code for efficient non-working code.  This is where optimization and Pareto’s Principle play a role.  Pareto’s or the 80-20 rule states that 20% of your code will be responsible for 80% of the running time.  Naturally, one should focus on optimizing the 20% of code to get the most speedup.  Optimization should be the result of taking working code and making it more efficient.  Planning and thought have to go into this process.  Some going blindly into the project and hope to rewrite whole classes to gain a speedup only to find that their project no longer compiles/works/completes all the unit tests.  Focus on simply refining small parts at a time and continue to test as you progress through.  Optimized code should be your way of putting icing on the cake; you take already existing code and you give it that extra touch of efficiency.

As an example, a project that one may be working on has a runtime of ~1 hour to complete the task.  It would seem completely worthless to spend hours optimizing/refactoring a piece of code that only takes 1-2 seconds to execute.  Ideally; yes, you should make the code as best you can, but one must focus on the largest piece to tackle.  Successful coders notice this and will attack the code with the largest runtime. 

What I have found that is extremely helpful when optimizing is take enough time to think, design, and implement.  This will allow you to write cleaner optimized code that may not have to go through the refactoring process (or as much).  Also start with basic improvements in speed like changing a structure or protocol.  Often these carry a lot of different pros and cons which you can judge specifically to your data.  Overall, take time optimizing so that its done correctly so that people that maintain your code in the future will thank you!


80+ Hour Week and lack there of…

Posted in Personal, Work by mgornick on July 6, 2008

I recently read a post [linked below] that discussed “How to work 80+ Hour Week.” Not only would I highly recommend read the post but also brush up on the “Cliffs Notes” version of the “4 Hour Work Week.” Essentially, the book describes ways to optimize your time, produce a business with reoccurring revenues, and outsourcing various parts of your life.  Some parts I agree with, but most are too fanciful and idealistic.  As alluded to in the linked post, you need to do what you love.  If you love sleeping late and not working, then maybe starting up the next YouTube or Facebook isn’t for you.  I guess it all depends on one’s definition of success.

I am from the breed that believes that the quality of work outweighs the quantity of work (speaking specifically to hours).  I would rather spend 1 hour coding, designing, etc. then 5 hours producing something subpar.  This belief comes with a catch and that is to put quality work into those 5 hours, 40 hours, 80+ hour weeks.  Yes, it seems impossible at times and immensely difficult.  Physically there is no way to maintain “optimal quality of work” for prolonged periods of time just as a runner cannot run a 2 mile at a speed of a 100m sprinter.  The goal is to get the very most out of yourself when you are working.  When you begin to slip, one need to be cognizant enough to refocus their efforts and continue on.

The emphasis on quality of work is important.  Through my experience in New York, people put in ridiculous amounts of hours into there work (100+ hour work weeks aren’t all that rare).  Some do it out of necessity; others out of the shear lack of putting in quality time to work and running through the motions of the day.  I easily put in 80 hours of work into my internship (and I don’t get paid for working past 40 hours per week).  When I am slowing down or realize that I’m not producing quality work, I know its time for me to take a small break.  I’ve made a list of things I do in order to help refocus my attention and get back to performing quality work:

1. Simply get up, walk around, talk to people/teammates/fellow interns.  As simple as this sounds, the small change in environment helps clear your mind.  

2. Get physical!  Go to the gym for an hour, run, swim, play a game of basketball.  I’ve been going to the gym during lunch to get a good workout in and once I return to my desk I’m clear headed and ready to start.  Sometimes getting your body moving and exercising is the best cure to focusing on work.

3. Take 10 minutes to grab some food/drink.  Many people will swear by the craze of “Energy Drinks,” but if you care about your health and possibly dying of a heart attack at 25 (lol), I would recommend eating fresh fruits or having a smoothie/tea.  Natural food/drinks are not only much healthier but also contain natural sugar/caffeine to get you through the day.  If you’re used to drinking energy drinks, the slight amount of a boost you’ll receive from eating healthy will be pretty insignificant.  Try to ween yourself off of them and make healthier food choices.  It will pay off in the end when you are eating healthy, feeling better, and are more productive throughout your day.

Alluding to the second part of the title: “… and lack there of.”  All too often do I see people that work a lot less, produce less meaningful work, and are perfectly fine going through the motions of their current job.  I don’t think that working 80+ hours a week is for everyone.  Like I’ve said before, “It depends on your personal definition of success.”  To be perfectly honest, it is okay to work 40 hours per week (or even less) if you can survive and do what you truly love.  That style of living is more inline with the 4 Hour Work Week.  I can’t imagine my life without something on my list of “Things To Do”.  It keeps me occupied and always awaiting the next project.  

Take away point: Focus on quality rather than quantity of work.  Once you have quality, you can find ways to keep your endurance up and maintain better work habits.


How to be Successful: Timeliness

Posted in Personal, School, Uncategorized, Work by mgornick on July 6, 2008

“If you are on time, you are late!”

This seemingly contradictory yet true quote still resonates with me to this day.  I first heard it during my freshman year of high school and only until recently do I see its impact on pursuing success.  

First, what is being “on time.” If you are in high school/college, its the simple act of arriving 1 microsecond prior to the lecturer uttering their first words of the class.  As much as people in this age group (myself included) will tend to disagree with this definition, we all know it to be true and live by it as we prioritize our lives.  This leads me back to the point that you are either one of two things: early or late.  I’ve found that being “on time” doesn’t cut it anymore.

I’ve listed the consequences of being “on time” to a business meeting:

1. I am not prepared.  If I sit down as people are talking, I’ve missed any handouts, preliminary conversation, and the chance to organize my notes and thoughts.  This is detrimental to myself as I will not get the most out of the meeting or be able to contribute my ideas.

2. I will delay the meeting. Because most people will wait until everyone is situated, I will have essentially wasted other people’s time.  Others will be waiting for me to “catch up” to what everyone was doing earlier.  In general, people don’t like their time to be wasted!

3. I will not be in control. This is probably the most detrimental to one’s success.  If I show up “on time”/late, I obviously didn’t schedule my day, prioritize, or manage my tasks well enough.  Although this is sometimes unavoidable, it hurts to lack control over a situation.

This leads me to a story of a friend of the family that was interviewing for a job.  She’s worked in various fields throughout her life, has a presentable resumé, and landed an onsite interview with a nearby company.  She *assumed* she knew where the location was and how to get there.  When the day came for her interview, she arrived “on time”; literally walked in the door at 8:30 am for an 8:30 am interview.  Do you think she was ecstatic that she made it in time?  Of course not.  The secretary told her that if she couldn’t make the effort to arrive early for an interview then she obviously didn’t care enough about the job.  The secretary was right! If you show up late for an interview, you might not be dependable with a project or task that needs to get done.  Being “on time” is being “late”.

Lets take my story from this summer’s internship.  We [the intern class] were told to meet at a particular location at 8 am.  What time do you think most people got there?  If you guessed between 6:30 am and 7:00 am, you’d be correct.  Especially if you are not familiar with the area, you should give yourself enough time to find the location and travel there if you’re lost.  If time permits, you should try to visit the site a day in advance to judge the time it will take to get there.  A point I will leave you with: what if you were the only person to show up at exactly 8:00 am?  How would you feel?  I suspect that it will coincide with the 3 points I listed for being “on time”.

I’ve found that being punctual and organizing my time has helped me take proper steps to success.  Refer back to my post on JP Morgan to help prioritize your schedule and maintain timeliness.