Showing posts with label fresher. Show all posts
Showing posts with label fresher. Show all posts

Monday, October 30, 2017

Refactoring Code

I believe refactoring is a very important trait for a programmer. Refactoring helps in reducing the repetition of code and also helps to make the code generic. Like everything else in life, code too accumulates dust if not maintained constantly. Constant refactoring helps in keeping the code clean. Learning to refactor early on in programming career helps in having a great career. In fact it is something we don't even need to learn as, as human beings we are experts at identifying patterns and  crux of refactoring is pattern matching.

Recently I was reminded of the age old programming puzzle by a friend and wanted to use that to capture my thoughts on refactoring.

The code is to generate something like this:



The initial code we would typically write would be specific to this particular output . (See the initial iteration in JSBin)




The first thing that strikes from this code is the repetition of the code block within the two for loops. The first refactor will be to extract this into a function. 

Anytime we see code being repeated, it immediately calls for refactoring. It is well known that lesser the code lesser will be the defects. 

The refactored code could look like this: (1st round of refactor)

This is still specific to the particular output. It could further be refactored to be generic, suitable for output of similar pattern, say a grid of 15x15. (2nd Round - Generalization)


Still it provides the same output as the first code, but, in a much generic way, with less repetition. 

The key rules of refactoring:

#1: If there is repetition of code, it is a no brainer for refactoring

#2: If the code could be applicable for similar problems, refactor it to be generic. Just be aware of the ROTI (Return on time invested)

#3: Don't make the code clever in the name of refactoring. The code should be clear to understand. Not clever. This code could be refactored to use a single loop, but, at the cost of clarity.   See line 24 in the code below. (This is fairly straight forward than the clever codes out in the wild. But, you get the drift.)


(Have a look at http://jsbin.com/bemitew/edit?html,js,output which has this 1 loop implementation)

Happy refactoring!




Thursday, May 27, 2010

OJT vs. Traditional Training

1.1 OJT

Definition for this purpose:

Make a just-out-of-college or almost-so new employee to work on a problem in an ongoing project and let him come through it.

Action:

The employee should be handed a problem that has enough complexity. The employee has to setup his or her environment based on the choices made for the project. The employee could solve the problem in parallel with the team. This will prevent him from being a bottleneck, but at the same time allows enough room and an actual problem to think.

Additionally he can also be made to troubleshoot some issues. These issues could be current issues or issues that have been solved but have educational value.

The employee can be presented with a list of training courses, which he can attend to accomplish the task and should be encouraged to ask for one if he is stuck.

Result:

Employee learns to solve some of the problems that may crop up, which will help in production projects. The training becomes a pull model rather than a push model. This results in better take away from training. Since the training correlates with the knowledge required to solve the problem at hand, the information gets registered better.

But, it may also turn out that the employees don’t ask for training as most of the problem might have been solved by one-off discussions.

1.2 Traditional Training

The usual training models involve pushing the information to the employee. The information contains mainly of overview of technologies, which could be easily obtained from the Net and information on standards and conventions. Most of these are not registered in the minds of the employees, as they could not match this with a real need.

1.3 Hybrid Model

A better approach will be a hybrid of the above two. Let the employee go through OJT for a month or so until enough curiosity is awakened and then put him through the training. The training should involve general, but critical information on fundamentals (e.g. fundamentals of webapps – Web server, app server, ports, request, response etc. along with show and tell with a request analyzer), specific technologies (with emphasis on practical application), unit testing. Ideally, an employee could be trained in multiple technologies and then he may align into one or many of them.

1.4 Things that have not worked

Some approaches have not worked very well in an OJT:

  • Working on an internal project

Typically, this internal project will consist only of trainees/new employees. The whole team has an air that it is an unimportant one. Most of the times the project is not utilized.

  • Involving in a live project, but without any real problem to solve i.e. left to handle routine activities
  • Giving a problem from a real project but not attaching any importance to the results or worse not expecting any result at all

Better approach will be:

  • If it is a critical internal project, create a real team for it with proper team lead and some senior developers. If not, scrap the project. If it is not important enough it is not worth developing.
  • Utilizing an employee effectively only when forced does not add any value to the team. Prepare the employee for tomorrow’s requirement. Automate routine activities and share/cycle non-automated ones amongst the team.
  • Push for the results. Use parts of the results where it could be. Expect something that could be used. Give the employee a sandbox branch in the source control for the project and let him commit his changes there.