Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Thursday, June 17, 2021

REFACTORING

 What is Refactoring?

A software is built initially to serve a purpose, or address a need. But there is always a need for enhancement, fixing problems, add a new feature, or enhancing/ modify an existing one.

Periodic changes to the code are made by a plethora of developers of varied experience and coding styles. Hence, over a period of time, the code becomes increasingly complex, adding to its performance and overall maintainability.

To combat this, it's essential to refactor the code periodically to avoid the above-mentioned problems.

Refactoring ensures the quality of the code by restructuring the code without changing the overall behavior.

When to Refactor?

Ideally, refactoring should be a part of the project plan on a periodic basis. It should also be a part of regular programming.
When you have a task of making some fix/ enhancement to the code base, look at the existing code and analyze its readability. If the developer thinks the code is not readable enough to make a straightforward change, take some time to refactor it first. Many times you would already find some code to re-use which is hidden inside some bigger method. Once you refactor them, the job looks simpler.

Then make the required change. 
 
You would notice that it would be a lot faster and less prone to errors/ bugs. 
After a couple of weeks, return to the method and see if it is still readable or needs refactoring again. Chances are, you will find scope for improvement. 
If you visit the same code again after a few weeks, there will still be scope for improvement. 
It's just how the coding works.

Benefits of Refactoring:

1. Improves Understanding and Readability

Translating someones else can be time-consuming and overwhelming. Periodic refactoring improves the design of the software and makes the bigger picture clear. It makes the code easier to understand, and eventually, the software gets cleaned up, for yourself and for other developers who would work on it in the future.

2. Improves development time

Sometimes, it takes several hours just to understand and get the gist of the functionality of a code written by someone else. A well-refactored code is easier to understand and cuts down a lot of development time and reduces regression bugs.

It's easier to write unit test cases for a well-refactored code.

3. Maintainability

Refactoring helps in finding bugs and program faster. It keeps the code clean and organized. A well-refactored code is easier to maintain in the long run.

Automated tools for refactoring

Most IDEs like eclipse/ IntelliJ already have inbuilt plugins that automate the refactoring process. However, it is not mandatory to use them. It is perfectly possible and okay to refactor manually. Just make smaller changes and test frequently to avoid errors. Unit test cases can come in handy too while refactoring. So consider writing as many unit tests to validate your changes.

Conclusion

I have often noticed teams struggling to write unit test cases just to get a good coverage percentage on various quality tools (Sonar, Jacoco, etc.). More often than not, these are mandated by stakeholders to quantify the quality of the source code. But these numbers can be highly misleading, meant only to showcase to the upper management and make pretty ppts.
Periodic refactoring is actually what will get you there. Making enhancements will take a lot less time and it will improve the code quality over a period of time. So it is important, we allocate time to this as part of your regular programming tasks.

Sunday, November 17, 2019

THINGS I HAVE LEARNED AS A TECHNICAL LEADER

1. It's always the team

Its not about you anymore. It's important to keep aside your ego, the competitive in you and jealousy. The solutions should be discussed and it's okay if a junior or a competitor proposes a better solution than you. You would need to grow a open mind to embrace the best solution, no matter from whom it comes from.
I learned to start the meetings by stating the problem, and the first line post that would be 'So anybody has a solution in mind?'. If I propose a solution myself, I learned to end the sentence with 'I am open to suggestions and feedback'.
At the same time, it's important to groom your team to be open and forthcoming with their ideas. Providing the best technical solution is not enough, it's important to find the right balance between technology and business. Hire the right people, who can groom if needed, and trust eventually.


2. Technical debt is a bitch

You can run but you cannot hide!
If we ignore the debts for too long, it will come back and bite you. Often the timelines are so tight that the technical debts are ignored and just sitting in the backlog. It's frustrating when the managers and business does not give enough importance to the technical debts and they just remain a number in your release notes, only addressing the blockers, or getting the low hanging fruits to maintain an acceptable scores in the various tools to keep the Quality team happy.
That's where our skills are put to test, convincing the business to address the technical debts. A few every sprint and you will see the difference. Easier said than done, but its worth to keep trying.


3. Find the balance between business and the technology

Instead of the attitude to 'get the story done', it's important to see how the solution will fit the business and raise flags if you think it would not work as per the customer's need, or if you think it breaks or contradicts any existing feature.

With years of grooming as a developer, its easy to get shortsighted and only look at a problem from a technical stand point. We usually concentrate in providing the best solution possible in terms of technology and often ignore the big picture. Also, its extremely lucrative, how we can introduce a new cutting edge technology to solve a given problem, but the business often has no time, no budget, and no risk appetite. Frustrating as it might sound, I have learnt to respect that. From a business standpoint, there has to be a balance between the time put in explore and learn this new technology vs the benefit it would actually bring in to the project/ app.

Put in some personal time for a POC if needed and if we can show the stakeholders that we can get it done, then its a win-a-win.


4. Wear any hat that the team needs

Don't consider the non-technical people as muggles, or look down on any role.
In the last couple of years I have worked as a QA, Developer, Support Engineer, Business Analyst, Architect, OPS, Scrum Master and often filled in for the Product Owners as well. I had this whole stack of hats hanging in my coat hanger and wear anything that the project needed at that point.
While it helps unblock the projects at various points, it also added to my personal profile. It helped me build a lot of relationships beyond just my team, my project, my role and profile. The more people you engage with, increases your visibility among multiple stakeholders. They are also more likely to get you involved earlier or give you relevant, timely feedback. Those relationships are important.

5. Leader is known by his followers

Finally, every time any of my mentees have thanked me, I have said the same thing. A leader is known by his followers. Try to mentor the team, keeping in mind that you need to groom them to be independent and eventually have them mentor others some day. Quoting an example from my own experience: every time a team member approached me with a query, I always took time to explain them the history and relevance of the solution and not just how to solve that particular story/ bug. That way, they stay engaged, understand the whole ecospace of the project (technical and functional) and will start getting the big picture sooner. I learnt that from my first mentor.

Tuesday, June 19, 2018

12-Factor Apps in Layman's terms

Popular platform-as-a-service provider Heroku has published a set of best practices called the Twelve-factor-app for building deployable sowtware-as-a-service apps that are

  • Portable
  • Continually Deployable and
  • Scalable
Here are the twelve principles in a more precise and understandable version:

CODEBASE: One codebase tracked by revision control, many deploys 

Put all your code in a version control system, the best option for me is the git.
You can have multiple branches but never multiple repositories for the same service/ microservice. There can be only one codebase per app, but there can be many deployments. 

DEPENDENCIES: Explicitly declare and isolate dependencies

This has been more important ever since configuration tools like Chef and Puppet have been used extensively used by DevOps to automate the configuration and deployment process.

Consider maven as dependency management tool, manifest will be pom.xml, which fetches dependencies as jar (artifacts) from various repositories.

All your dependencies, third party or otherwise must be declared and completely isolated across environments. Most of the modern programming languages have a built-in support for this. You can declare all libraries in your code, and during the deployment pass the environment variable as a part of the command line that picks up the correct file to deploy

CONFIG: Store config in the environment

Configuration are those part of the application that change based on the environment where the application is deployed on. For example database connection properties, application specific information such as host IP and port etc.

There should be a strict separation between config and code. Code should remain the same irrespective of where the application is being deployed, but configurations can vary.

BACKING SERVICESTreat backing services as attached resources

Backing services refer to the infrastructure and other services by which the application communicates over the network. Database, Message Brokers, other API-accessible consumer services such as Authorization Service, Twitter, GitHub etc., are loosely coupled with the application and treat them as resource.

The idea is to be able to swap any of these resources with a different provider without having to make any changes to the source code.

BUILD, RELEASE, RUN: Strictly separate Build and Run stages

The twelve-factor-app suggests strict separation of the build, release and run stages.
Build - converting source code into an executable bundle known as the build. (jar, war, ear etc.)
Release - Or Deployment. Getting the build and combining it with a configurations of the specific  environment, assign it a unique release number, and made ready to run.
Run - Execute the package on the specific environment.

The 'build'cycle does most of the heavy lifting and the run stage should be very light weight. Tools that help in achieving a full CI/CD pipeline are Jenkins, Thoughtworks go, and Codeship to name a few.

PROCESSES: Execute the app as one or more stateless processes

Modern applications are deployed on many nodes usually with a load balancer to direct the traffic to enable quick request handling. In such cases, we cant guarantee that consecutive  requests from the same client would go to the same node.  Hence its unwise, to rely on data stored in the previous requests, since it would not be available if the next request is directed to another node.
As a rule, you want each of those instances of running code to be stateless.

There are a number of ways to achieve this:

  • Save the state of the process in the database or shared storage.
  • Use scalable cache storage like Memcahe or Redis.
  • Package assets in executables (e.g. by using webjars at build time)


PORT BINDING: Export services via port binding

This is an extension of  the Backing Service factor.
Make sure  that your application is visible to others via port binding, so that other services can use it like a resource.

CONCURRENCY: Scale out via the process model

This is all about scalability. The idea with more smarter applications is to scale horizontally by deploying more copies of our application on multiple nodes rather than scaling vertically, i.e running a single instance on a much powerful system.

In particular, you’ll be able to do more stuff concurrently, by smoothly adding additional servers, or additional CPU/RAM and taking full advantage of it through the use of more of these small, independent processes.

DISPOSABILITY: Maximize robustness with fast startup and graceful shutdown`

Processes in twelve-factor-apps should start and stop in minimal time.

Fast startup is based on our idea of scalability and also gives way to the usage of microservices as opposed to monolithic applications. If an application takes 30 seconds to start up and handle requests, it defeats the idea of rapid releases.

Graceful shutdown also involves leaving the system in the correct state. All resources should be freed and all unfinished tasks should be returned to queue. Crashes also should be handled. Crashes, if they happen, the app should be start back up cleanly.

DEV/ PROD PARITYKeep development, staging, and production as similar as possible 

In the recent years, its become pertinent to have short and rapid production cycles. This means that the time window between the development of a change and deployment of the said change is become very small, sometimes a matter of hours. With this window being so small, you do not want to spend time on the deployment process, or carry the risk of something breaking in production.

Hence, its highly recommended to keep the developers environment as similar to the production as possible. This includes using the same third party(or otherwise) services, same configuration management tools, same softwares and libraries.

LOGS: Treat logs as event streams

Logs are highly useful. They can carry variety of information on many levels. They can come extremely handy while trouble shooting the errors in production. Read : Logging the right way

Other than trouble shooting customer issues, you can use error monitoring service like  Airbrake or Papertrail or data mining services like Logstash or Splunk.

ADMIN PROCESSESRun admin/management tasks as one-off processes

Your app might need some admin jobs at times, for example cleaning up the database for bad data, switching on/off configurations or features, generating reports for analytics or audit purposes.

The twelve factor app suggests that you do these one off tasks from an identical environment in production. Don''t directly access the database, do not access it from a terminal window.

SUMMARY:

The twelve principles mentioned above might not seem novel, you might already be using some already in your engineering cycles. But if you are running a microservice architecture, its important that you take these principles seriously. You might not see the benefit right away, but it will become highly important when you are running multiple services across multiple environments.

Friday, May 5, 2017

Logging the right way

Logging is an important aspect of software development, one that is many a time ignored by developers.
it's important to have clean and helpful logs. They can come real handy when you have to debug an issue.

Below are a few important points to keep in mind while writing your logs

1. Using a good API

In my experience,I find the slf4j API better than the others.
Consider this:

in log4j:
logger.debug("Found employee : name: "+name+"designation: "+designation+" department: "+department);
This is difficult to read and has 3 string concatenation.

it can be rewritten with slf4j as
logger.debug("Found employee : name: {} designation:{} department: {}",name, designation, department);
Now, this is more readable, and avoids the string concatenation.

2. Watch what you are logging

Every time you log, watch what you are logging. Sometimes loggers can be the cause of many exceptions

Example 1: logger.debug("User Details :{}", user.getName());
This might throw a null pointer if the user is null

Example 2: logger.debug("User Details :{}", user);
This is an example where the hibernate object has been sent to the logger. This can give way to one or more of these issue :
Out of memory error, N+1 select problem, lazy initialization exception, logs storage completely used up

It' always a good idea only to log the ids of domain objects instead of the complete object. They are enough information for debugging.
Log no more than necessary in release/production, excessive logging can make you app slow.

The following logger format works best for me:
Who(UserName) , When (Timestamp), Where(Context, ServletOrPage,Database), What (Command), Result (Exception/Error)


3. Use categories

The severity values INFO, WARN, ERROR, and DEBUG, can be used to filter logs based on the severity.
The benefit that loggers have over System out is the logging categories and levels.
To read more about logging severity, visit
https://www.tutorialspoint.com/log4j/log4j_logging_levels.htm

Logging is an I/O intensive activity and can get in the way of the application's performance, particularly when logging is done in the traditional way that is, synchronously writing to a FileAppender in the context of application threads.
It can often contribute to heavy heap consumption and garbage collection.
hence, excessive logging can lead to performance issues. So be concise, and log relevant data that would help in debugging.

You can also use isDebugEnabled() to prevent filter the logs for production.

4. Log files locally
Use a file local to the server to write the loggers. It provides a local buffer and you aren't blocked if the network goes down.

5. Rotation policies
Log files can get huge. Maintain a rotation policy to roll logs periodically. You can also have a strategy to destroy or back up your log files after a period of time.

REFACTORING

 What is Refactoring? A software is built initially to serve a purpose, or address a need. But there is always a need for enhancement, fixin...