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?

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

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.

Recycling and Trash Ideas

I had just watched Trashopolis on the National Geographic Channel; it featured Paris France.  On it they showed the history of excess trash and how Paris overcame it.  From the principal of rag pickers, they created city sponsored enterprises.   Back in the 1800’s a rag picker would be a person or persons who who would pick through trash, fix or clean items and resell the items at flee markets.

With the city sponsored enterprises, the entrepreneurs would run a depot where citizens would drop of batteries, old appliances, clothing and other items.   The depots would then clean, fix or re-purpose the items for donation or resale.   This seems to be cutting down on garbage, increasing recycling efforts and ultimately saving the city from garbage.  As an added bonus, the people running the depots run routes that would allow them to pick up items from vendors, shop owners and households.   I can really see the benefits here.

London Ontario every year discuss what they can do to save trash costs, increase recycling efforts and work on new contracts with garbage collectors.  Not once I had heard of the idea of a city sponsored enterprise being setup to fix, clean and re-purpose items for resale or donations.  This would also help the city encourage entrepreneurship, increase arts and crafts and hit the international stage for becoming more Green.

I think for thing to work, the city would only have to find a person to run things, donate a building and help build a business plan for the person to start.  This would be a great venture for a young person with a basic knowledge of electronics and business, recruit some local artists to help re-purpose items and you could have a good little business helping the city to become more green.

May be worth while sending a small team of business and city officials to places like Paris who seemingly are doing things a little better.

The Art of Task Delegation

Delegation is a tough skill to master.  Many mangers have trouble delegating tasks to employees because they have trouble giving up control.   A good manager knows their strengths and the amount of work load they can handle without stressing.   A good manager also knows their employees strengths.  Use those strengths to your advantage.  Through the art of delegation, you can harness the skills you don’t have and reduce your stress by reducing your work load.  Delegation of tasks does not mean you are no longer responsible for the task, it only means that you are now managing.  You are still responsible for quality of the work done by the person you delegated the task to.  You still deserve credit for getting the work done, the only difference is you should be sharing the credit with the people who helped you.

Tips for Delegating Work

  • Know everyone’s strengths
  • Follow up with everyone who you delegated work to.   Ensure they are on task and the quality it to your desire.
  • Delegate tasks only to the people you trust.
  • Use a calendar.   Make sure you are following a schedule and that everyone knows the schedule.
  • Make sure everyone has the resources to get the job done. 
  • Provide your knowledge and share your experience.
  • Manage, manage, manage – this doesn’t mean you do the work. 
  • Share the glory, if you get praise, share the praise with those who helped. 

Javascript equalsIgnoreCase Function

This week’s code sample is JAVASCRIPT based. I found this while researching user validation routines. This function is a javascript version of a common Java function. We have replicated the calling syntax as a java programmer would expect to use it by effectively appending it as a method of the javascript String Object.

For more great scripts: http://www.apriori-it.co.uk

Important Note: Works with IE and FireFox

//The first line assigns the MatchIgnoreCase function as 
//an equalsIgnoreCase method of the String object 

String.prototype.equalsIgnoreCase = MatchIgnoreCase; 

function MatchIgnoreCase(strTerm, strToSearch) 
{ 
	strToSearch = strToSearch.toLowerCase(); 
	strTerm = strTerm.toLowerCase(); 
	if(strToSearch==strTerm) 
	{ 
		return true; 
	} else { 
		return false; 
	} 
}