Importance of Code Refactoring

By continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you’ll find that it is easier to extend and maintain code.
—- Joshua Kerievsky, Refactoring to Patterns

I had recently seen the above quote while sifting through some periodicals that I read and it reminded of the importance of Code Refactoring. Refacotring is a mindset that you must put yourself into. You have to decide that you do not write perfect code the first time and that there may be some room for improvement. An hour or two a day may be all you need to review your own code.

I recently spent two hours reviewing code from one of my modules that I had written. It had the same or similar code written in 5 sections in the code and had the same purpose to these sections. Each section had 11 or more lines of code in it. I took one of those sections of code and converted it to a function and then replaced the 5 sections with this new function. Ultimately I had eliminated 51 lines of code out of one module.

Reasons for Refactoring Code
1. Consolidating and eliminating “Like” or “Similar” Code
2. Breaking out a extraordinary long function into more manageable bites
3. Make error trapping easier to handle
4. Make code more readable and maintainable
5. Removing nested IF or logic Loops
6. Make it easier to document
7. Create Reusable code
8. Better class and function cohesion.

The benefits now are the following:
1. Similar code is now the same which is the way it was meant to be ( coded over three days, the code morphed a bit ).
2. Since the code had the same purpose, it looks the same now and behaves the same.
3. Code is in one spot instead of 5 – makes it easier for a base change
4. Error trapping is much more controlled.

If you cannot review your own code ( some people cannot and should not be ashamed for it ), you should get yourself someone you can trust to review the code for you. The do not have to modify the code, they can either insert comments to you, print out and scribble on the pages or meet with you. Never take it as bad thing if someone wants to change your code; take it as a learning experience. If you are using someone else, challenge them back. Make sure they know what they are talking about. A face to face meeting is always a good idea. A mentor, or team lead should be taking on the role of code reviewer. Code reviewer should be recommending ways to refactor your code. This should be done prior to release, deployment and ending a project. The review should always be done with the developer(s) responsible for writing the code. Should never be done without as that undermines the developer(s) and no one learns anything from it.

What Languages do I Use

I was recently asked what I use to develop software and websites. I mostly use DotNet Technologies as it is often interchangeable between websites, web applications and desktop applications. I can often use the same data access classes and business logic between multiple platforms. I find that DotNet allows me to easier structure objects visually and logically.  I prefer to develop using C-Sharp, but I get requests to use VB.NET and J-Sharp on a regular basis.

When I went to college, I had learned C, Pascal, COBOL and some Mainframe language that I cannot remember.  Upon graduating I taught myself VisualBasic 3, RPG.  My first programming job was Visual COBOL.  I had created a company in the Caribbean Islands programming public utility and insurance software in RPG.   I can honestly say since I sold my shares of the company, I cannot foresee myself developing in RPG again.

Why don’t I use PHP?  Well I do use PHP, but for specific purposes and upon request by a customer. A specific purpose is when I create a WordPress site or other content driven sites. I will not often create a website or web application from scratch using PHP as Microsoft made ASP.NET easy for rapid development.

Do I use JAVA? Yes; I do believe there are cases where JAVA is still relevant. JAVA is still a good tight platform for developing small single purpose applications.  I have used JAVA 3 times in the past 2 years for applications that I believe fit the bill well.

Now, I am curious;  What do you use and why?

Project Manager – Team Lead – Manager of Development

I have been asked a few times for resources and tips for a new Project Manager, Team Lead, Manager of Development type position.
My tips are a generalization as I can pull out more resources for certain situations.

Tips 
1. Have an open door
2. Don’t tell your team everything that you know. Be selective
3. Be positive in all your dealings
4. Lead by example. Do not ask your team to do anything you would not.
5. Speak clearly
6. If a developer says it will take a week, say two. If a developer says a month, say 6 weeks. Always buffer time.
7. Be real with expectations
8. Be kind, not too strict, but strict enough
9. Careful planning of project details
10. Listen to those above you and below you. You will learn stuff from everyone.
11. You are not the end all be all.
12. Fight for those who deserve it, help those who need it
13. Stay Organized
14. Meet with your team weekly with an agenda, so everyone can be prepared
15. Know what your superiors want from you
16. Know your place
17. Make sure you keep developing
18. Code reviews are a must, even yours ( can be fun for the team )
19. Earn your trust, trust your team
20. Know each team member’s strengths and use them. You cannot be everything to everyone
21. Failure is always an option – learn from each failure and success
22. Reflect on everyone project as there is always something to learn
23. Find a mentor or mentors – they can be good sounding boards

Online Resources 
http://www.fenman.co.uk/activities/training-manual/team-leaders-development.html
http://andrewtokeley.net/archive/2008/05/02/how-to-become-a-development-team-leader.aspx
http://www.atlassian.com/agile/people/teamlead.jsp
http://en.wikipedia.org/wiki/Lead_programmer 

Books to Read 
Project Management in a Week By Mark Brown
Survival is Not Enough By Seth Godin
Made in Canada Leadership By Amal; Morissette, Francoise Henein

My Blogs 
http://unlatched.com/blog_What_is_Leadership.aspx
http://unlatched.com/blog_Effective_Listening.aspx
http://unlatched.com/blog_How_a_Senior_can_Help_a_Junior_Developer.aspx
http://unlatched.com/blog_Can_Senior_Developers_Learn_From_Junior_Developers.aspx

Remember: Have fun or you will not like it. Take time off – you need breaks.

Shortcut To Creating Properties in C-Sharp – Revisited

Based on my earlier an post, a I had a question about my technique and if there was a benefit, or could you use MethodBase.GetCurrentMethod().Name. Truthfully I did not know the answer until I tried it. I found this new way works, but I also find it is a little tougher to look at style-wise. You need to use stack frames if you nest the routine like I had in my original post.

public class ErrorLogRecord
    {
        public Hashtable _hsh = new Hashtable();

        public DateTime DateOfOccurance
        {
            get { return (DateTime?)_hsh[MethodBase.GetCurrentMethod().Name.Replace("set_", "").Replace("get_", "")] ?? DateTime.Now; }
            set { _hsh[MethodBase.GetCurrentMethod().Name.Replace("set_", "").Replace("get_", "")] = value; }
        }
        public String ErrorText
        {
            get { return (String)_hsh[MethodBase.GetCurrentMethod().Name.Replace("set_", "").Replace("get_", "")]; }
            set { _hsh[MethodBase.GetCurrentMethod().Name.Replace("set_", "").Replace("get_", "")] = value; }
        }
    }

Original Referenec Shortcut To Creating Properties in C-Sharp

When a Developer Leaves

To start; I am not saying this is me, but I did talk to a few developers for research.

Employers are often left scratching their heads when a developer leaves. The unfortunately truth is when one leaves another often follows not to long after. It is very hard to understand why the first developer left let alone the second or third. I believe as a employer, team lead or VP, you should know some of the “WHY” for a better understanding of a developer’s behavior. I admit we are a strange brew of employees.

Developers like to be challenged and appreciated. When the challenge is gone, so is the developers will to continue. Developers need to know that they will not be doing the same thing day in and day out. They really need to mix things up a bit. They need responsibilities, new tasks and freedom to try new things on their own. They also need to know they have the company’s support to continue education, and not just a pat on the back. Try to hire from within the team before you look for that new manager. Sometimes you have the perfect leader within the company already.

Developers also have a keen sense of appreciation. They do not need a thank you, or a pat on the back from their employer all the time, but they definitely know when they are being exploited and taken advantage of. More and more companies are introducing foosball, video games and fun activities to show their developers that they can have fun and are appreciated. It is the little things that go a long way. A BBQ in the summer with some good laughs and keep the spirits a little brighter.

These are just brief explanations of why a developer may leave a company, but very rarely is it just for more money. There is often more to it. An exiting interview may shed more light on the “WHY”, but more likely you will never know. An observation that I have made recently; there is a real misunderstanding of junior developers. Junior developers are ones that leave their company the most. Junior developers leave because they are treated poorly by senior staff, paid unfair salaries, no or little vacations and benefits and simply they often do not feel apart of the team. Junior developers add value to any team for a couple reasons.

  • Successorship for senior developers
  • Brings new ideas and techniques to the table
  • They still absorb new information at a fast rate

Why would other developers leave shortly after?
Loyalty is often a factor. Developers are social creatures who form a tight community that is often hard to break into. They often congregate at bars and coffee shops after hours or on breaks to share and implement ideas. This form of interactions creates a tight bond. If a company hires one developer, it is not uncommon for that developer to want to take a few of his trusted colleagues with him. This is a practice that shows the tight bond and trust between the development community. Developers like to work with people they know and trust. It is also becoming a more common practice for the new employer to try to take an entire team of developers that are linked by a common employer or group. This helps create a stronger team.

What Not to Say to a Developer
I was once told that I would never be able to advance out of my position because I was too good at it. These words are like knives going into the heart. A developer likes to be challenged and advance through their career like anyone else in the workforce. Developers are often known to put their heart and sole into their work; often giving 110% of themselves. Advancement is often equal to being similar to being appreciated and/or trusted. Nothing kills a employee’s will to continue faster than words. Chose your words wisely.

Strange Creatures
Developers are strange creatures. They are creatures of habits ( generalization ) and often do not like change. Exception here is developers often embrace changes as in new technology and ideas. When I say they do not like change, they do not like to

move homes, place of work, parking spots, watering holes, etc. They like their every day habits and routines. When a developer leaves it is often a big decision and they did not take it lightly. It is often not a personal reflection on the company, but rather a personal reflection of themselves.

Quick Points

  1. Find out what other companies are doing to keep their developers engaged and happy
  2. Talk to your developers often on a personal level
  3. Create a trust with your developers. Do not give them a reason to not trust you.( Example: monitoring internet usage and emails )
  4. Give them freedom. Allow them to occasionally work on projects of interest ( Google often does this and then makes money from the side projects )
  5. Let them use social media to keep connected and build the communities. You do not want your developer to feel isolated. Non-techie people often do not understand a developer
  6. Do your hardest to create real expectations, and not push crazy hours. Developer burnout is rampant in small companies
  7. Do your best not to call your developer all hours of the night and every day when they are on vacation. You would not want this for yourself.
  8. Treat your developer as you would want to be treated

Shortcut To Creating Properties in C-Sharp

I was looking for an easy and consistent way of creating properties for classes.  I sometimes find that it can be a long drawn out process creating 5 to 100 properties for database classes.  Here is a quick example of what I did.
You will need the following references to make this example work.

  1. System.Diagnostics;
  2. System.Reflection;
  3. System.Collections;
public class ErrorLogRecord
{
     Hashtable _hsh = new Hashtable();
     public DateTime DateOfOccurance
     {
          get { return (DateTime?)_hsh[GetName()] ?? DateTime.Now; }
          set { _hsh[GetName()] = value; }
     }        
     public String ErrorText
     {
          get { return (String)_hsh[GetName()]; }
          set { _hsh[GetName()] = value; }
     }

     public String GetName()
     {
          StackTrace stackTrace = new StackTrace();
          StackFrame stackFrame = stackTrace.GetFrame(1);
          MethodBase methodBase = stackFrame.GetMethod();
          return methodBase.Name.Replace("set_", "").Replace("get_", "");        
     }
}

While this method is probably not perfect and could be argued there is a better way, this seems to satisfy the need. I would welcome alternatives.

Alternate Reading: Shortcut To Creating Properties in C-Sharp (revisited)

What is Trending – Are you listening

Do you know what is trending today?   Do you know what is new?  Do you care?

As developers we live in a crazy world where technologies and ideas fly at the speed of light.  If you are not paying attention you will miss out on the new ideas.

Hash-tags, Twitter, blogs, LinkedIn and Facebook; these are some tools of the modern world and they can help define our chosen professions.  If you have not embraced these fore mentioned tools, you will miss out.  How are you keeping up?  Are you relying on your everyday work or are you digging into the realms of social media to see what is trending?  The printed press like magazines, journals and newspapers are not cutting any more because of the pace of change.  By the time you read it, it is history.

Social media is trending the new ideas, tools and techniques at a pace that is baffling.  If you are a developer and you have not embraced social media, you are probably missing out on a lot of great stuff.  Managers are now asking you in interviews; How you are learning new things and keeping in the know?  If you are not saying social media as a tool, you would be foolish.  It is great to ramble of magazine names and school courses, but most of what is learned there I consider as historical background knowledge; you need social media to enhance your knowledge.

Are you looking for the next great idea?

Here are some interesting trends to watch:

There are a million of trends happening every second of every day.  Pick one or two a day and watch.  If you find something interesting dig for it and dig deep.  LinkedIn also has groups that could help you define who you are, what you want to do and how to do it.    There is a lot happening and you will never know what will trigger your next big idea.   Once you have watched, participate and that is the ultimate way of learning what is new.

Can Senior Developers Learn From Junior Developers

I relate this back to when you are listening to grandparents tell story after story.  Somewhere in those stories you learn from their history.  Now when they ask about you, they learn more modern things.   Like the time I showed grandpa how to tour castles from my computer.   He learned that there was a tremendous amount of information at their fingertips, if they embraced something new.

It is important to listen to each other.   We as senior developers have lots to teach, but we also have lots to learn.   Most of us have been doing the same thing day-in and day-out.   We have learned new languages and new technologies, but not necessarily to the extent of the new developers joining your team.  They have recently exited their programs to join the work force.  During their college or university days, they had a hunger to learn.  Most of them soaked up everything their teach taught them and then explored other avenues and ideas to a greater extent.  New developers are little sponges and you never know what they have absorbed during their school.  Most after graduation have continued their learning either through their senior developer, through further schooling or simple through other means like reading or internet.

I am a believer that I cannot know everything and that everyone has something to teach me.   I have had some very good junior developers show me some cool new tricks.   Through those new tricks, I am able to show them how to refine those tricks into something truly amazing.   There are some great minds out there.

Remember, today’s junior developers are doing things that we did not even fathom at their age.   I went to school with Windows 3.1, OS2 warp and COBOL programming.   They went to school with DotNet, PHP and Windows 7 and Linux.

I currently do not have a junior developer working with me, but when I did; I used to have Monday mornings as a sharing time.   We would meet in the boardroom (breakfast sometimes) and share what we have learned or done.   We also used to discuss issues or choices.   We used this opportunity to learn from each other.

Listen to what your juniors have to say.   You may just learn something as well as teach them something.

Related Reading: Can Senior Developer Learn From Junior Developers