Wednesday, February 26, 2020

How to Refactor code in visual studio?


Code Refactor

What is Code Refactoring?

Source code restructuring improved the code quality and maintainability of your existing project without changing its run time behavior. In simple terms, refactoring is the process to changing code order and make it easier to maintain and understand without affecting its run time behavior.

Refactoring the code improves the non-functional attributes of the application such as code readability, code maintainability, and code extensibility.  It reduces the code complexity and help developers to easy understand code and fix hidden bugs in application by removing unwanted code complexity and simplifying the logic of program.

On the other side, if the code refactoring is not executed well then its possible to increase code complexity more and more. If we continuously refactoring the code on time interval then it reduce the code complexity and maintainability.

Code Smell


Visual studio Programmers use the term “Code Smell” that indicate the characteristic of the source code. Code smell is not bug and its not stop executing program but it indicate weaknesses in design that may possible to slow down performance or increase the risk of bugs or system failures in the future.

Some common code smells are listed below.  
  1. Application level code smells
  2. Class level code smells
  3. Method level code smells

1. Application level code smells:

  • Duplicated Code: Its indicate that repeated code is written more than one location.
  • Contrived Complexity: Its indicate that the code is written in simple term to write same logic to avoid complex code structure.
  • Shotgun Surgery: Its indicate that same change needs to be applied across multiple classes.

2. Class level code smells:

  • Large Class: Its indicate that class has become too large.
  • Feature Envy: Its indicate that class using the methods of another class in excess.
  • Inappropriate Intimacy: Its indicate that class having dependencies to another class
  • Refused Bequest: Its indicate that class overrides base class method in such a way that the contract of the base class is not honored by the derived class.
  • Lazy Class / Freeloader: Its indicate that class does too little.

3. Method level code smells:

  • Too many parameters: Its indicate that method having too many parameters so its may be difficult to read and calling method and testing of the function become more complicated. Its may indicate that the definition of the function is not well defined, and the code should be refactored in a way that responsibility is assigned clearly.
  • Long method: Its indicate that method, function, or procedure that has grown too large
  • Excessively long identifiers: The naming conventions use to provide disambiguation. This can be implicit in software architecture.
  • Excessively short identifiers: Its indicate that name of a variable can reflect its actual functionality.
  • Excessive return of data: Its indicate that function/method returns more than what each of its callers required.
  • Extreme long lines of code: Its indicate that written code that is very long, hard to understand and debug.

Where to find Visual Studio Refactoring option?


Visual Studio Professional versions contain the Refactor option inside the Edit menu of the IDE. This menu item get enabled only when a C# file is kept open in the IDE.

In the Visual Studio 2017 version, we have the following options under the Refactor menu for refactoring the C# code:
  1. Extract Method Refactoring
  2. Rename Refactoring
  3. Encapsulate Field Refactoring
  4. Extract Interface Refactoring
  5. Remove Parameters Refactoring
  6. Reorder Parameters Refactoring


1. Extract Method Refactoring:

Extract Method refactoring is one of the C# refactoring techniques that provides a facility to create a new method from a code section in an existing member.

Using the Extract Method, we can create a new method by extracting a selection of code from inside the code section of an existing member. The new, extracted method contains the selected code only and the selected code in the existing member is replaced with a call to the new method. Turning a fragment of code into its own method lets us to quickly and accurately reorganize code for better reuse and readability.

How to Use:

1. Select the code section that you waned to extract.
2. On the Edit->Refactor menu, click Extract Method.
3. The Extract Method dialog box appears.
   We can also use the keyboard shortcut CTRL+R, M to display the Extract  
 Method dialog box or we can also right-click the selected code, point to   Refactor, and then click Extract Method to display the Extract Method dialog box.
4. Enter a name for the new method in the New Method Name box.
5. A preview of the new method’s signature is shown under the Preview Method Signature.
6. Click OK.

2. Rename Refactoring:

Rename is another C# refactoring technique that provides a way to rename the code identifiers such as fields, local variables, methods, namespaces, properties, and types. Rename can be used to change the names in the comments and in strings also and also to change the declarations and calls of an identifier.

How to Invoke Rename Refactoring:

  • Code Editor: In the Code Editor, rename refactoring is available when you position the cursor on certain types of code symbols. When the cursor is in this position, we can invoke the Rename command by typing the keyboard shortcut (CTRL + R, CTRL + R), or by selecting the Rename command from the shortcut menu or Refactor menu. 
  • Class View: When we select an identifier in the Class View, rename refactoring is enabled in the shortcut menu and Refactor menu.
  • Object Browser: When we select an identifier in the Object Browser, the rename refactoring is available only from the Refactor menu.
  • Property Grid: Changing the name of a control will initiate a renaming operation for that control. The Rename dialog box will not appear.
  • Solution Explorer: Rename command is available on the shortcut menu. 

Rename Operations:

  • Field: Changes the declaration and usages of the field to the new name.
  • Local Variable: Changes the declaration and usages of the variable to the new name.
  • Method: Changes the name of the method and all references to that method to the new name including static and instance methods.
  • Namespace: Changes the name of the namespace to the new name in the declaration, all using statements, and fully qualified names. 
  • Property: Changes the declaration and usages of the property to the new name.
  • Type: Changes all declarations and all usages of the type to the new name, including constructors and destructor(s).        

3. Encapsulate Field Refactoring:


The Encapsulate Field is a refactoring operation which enables you to quickly create a property from an existing field, and then seamlessly update your code with references to the new property.When any field is declared as public, other objects have direct access to that field and can modify it, undetected by the object which owns the field. By using properties to encapsulate and protect the field, you can disallow any direct access to the fields. 

To create the new property, the Encapsulate Field operation first changes the access modifier for the field that we want to encapsulate to private. Next, it generates the Get and Set accessor for that field. In some cases, only a Get accessor is generated, such as when a field is declared as read -only. The refactoring engine now updates the code with references to the new property in the areas mentioned in the Update References section of the Encapsulate Field dialog box.

How to Use:

1. In the Code Editor, place the cursor in the declaration, on the name of the field that you want to encapsulate. 
2. On the Refactor menu, click Encapsulate Field.
3. The Encapsulate Field dialog box appears. We can also type the keyboard shortcut CTRL+R, E to display the Encapsulate Field dialog box. We can also right-click the cursor, point to Refactor, and then click Encapsulate Field to display the Encapsulate Field dialog box.
4. Specify settings and Click the OK button.
5. If you selected the Preview reference changes option, then the Preview Reference Changes window opens. Click the Apply button.

4. Extract Interface Refactoring:


Extract Interface is a refactoring operation that provides an easy way to create a new interface with members that originate from an existing class, struct, or interface. When several clients use the same subset of members from a class, struct, or interface, or when multiple classes, structs, or interfaces have a subset of members in common, it can be useful to place the subset of members in a separate interface. The Extract Interface generates an interface in a new file and places the cursor at the beginning of the new file. We can specify which members to extract into the new interface, the name of the new interface, and the name of the generated file using the Extract Interface dialog.

How to Use:

1. With the cursor positioned on the method, click Extract Interface on the Refactor menu. The Extract Interface dialog box appears.
2. We can also type the keyboard shortcut CTRL+R, I to display the Extract Interface dialog box.
3. Or we can also right-click the mouse, point to Refactor, and then click Extract Interface to display the Extract Interface dialog box.
4. Click Select All.
5. Click OK.

5. Remove Parameters Refactoring:


Remove Parameters is a refactoring technique which provides a way to remove parameters from methods, indexers, or delegates. Remove Parameters changes the declaration of the method and at the locations where the member is called, the parameter is removed to reflect the new declaration.

We perform the Remove Parameters operation by first positioning the cursor on a method, indexer, or delegate. While the cursor is in position, to invoke the Remove Parameters operation, click the Refactor menu, press the keyboard shortcut, or select the command from the shortcut menu.

How to Use:

1. Place the cursor on method A, either in the method declaration or the method call.
2. From the Refactor menu, select Remove Parameters to display the Remove Parameters dialog box. We can also type the keyboard shortcut CTRL+R, V to display the Remove Parameters dialog box. We can also right-click the cursor, point to Refactor, and then click Remove Parameters to display the Remove Parameters dialog box.
3. Using the Parameters field, position the cursor on the parameter and then click Remove.
4. Click OK.
5. In the Preview Changes — Remove Parameters dialog box, click Apply.

6. Reorder Parameters Refactoring:


Reorder Parameters is one of the C# refactoring techniques that provides a way to change the order of the parameters for the methods, indexers, and delegates. Reorder Parameters modifies the declaration first and at any locations where the member is being called, the parameters are rearranged as per the new order.

In order to perform the Reorder Parameters operation, place the cursor next to a method, indexer, or delegate. When the cursor is in position, invoke the Reorder Parameters operation by pressing the keyboard shortcut, or by clicking the command from the shortcut menu.

How to Use:

1. Place the cursor on the method, either in the method declaration or the method call.
2. On the Refactor menu, click Reorder Parameters.
3. The Reorder Parameters dialog box appears.
4. In the Reorder Parameters dialog box, select the parameter in the Parameters list, and then click the down button. Alternatively, you can drag the parameter after the destination parameter in the Parameters list.
5. In the Reorder Parameters dialog box, click OK.
6. If the Preview reference changes option is selected in the Reorder Parameters dialog box, the Preview Changes - Reorder Parameters dialog box will be displayed. We can see the preview of the changes in the parameter list for the Method in both the signature as well as the method call.
7. If the Preview Changes - Reorder Parameters dialog box appears, click Apply.

Conclusion


Thus, Refactoring the code improves the non-functional attributes of the application such as code readability, code maintainability, and code extensibility.  It reduces the code complexity and help developers to easy understand code and fix hidden bugs in application by removing unwanted code complexity and simplifying the logic of program.

Saturday, February 22, 2020

What is AWS (Amazon Web Service)?

Amazon Web Service

Amazon web service is a cloud computing service platform that offering reliable, scalable, flexible, easy-to-use and cost-effective cloud services. AWS is a easy to use and comprehensive computing platform offered by Amazon. AWS platform is developed with a combination of below:
  • Infrastructure as a service (IaaS)
  • Platform as a service (PaaS)
  • Packaged software as a service (SaaS)

In simple terms AWS allows you to do below things:
  • It offers to run cloud based products.
  • Securely store all your data on the cloud so you can access them worldwide.
  • Access managed databases like MySQL DB, PostgreSQL DB, Oracle DB or SQL Server DB.
  • Delivered data in seconds/minutes (Depends based on data) around the world using a Content Delivery Network (CDN).
  • Bulk Emails.

AWS History

Launched AWS services in 2002
Launched AWS cloud products in 2006
Holds its first customer event in 2012
Reveals revenues achieved of $4.6 billion in 2015
Surpassed $10 billon revenue target in 2016
Released snowball and snowmobile in 2016
Offered nearly 100 cloud services in 2019

Advantages of AWS

Below are the advantages of using AWS services:
  • AWS allowing organizations to use the familiar operating systems, programming models, architectures and databases.
  • Its a cost-effective service that allowing you to pay for only what you are using, without any commitments.
  • Avoid to spend money on running data centers and maintenance.
  • Provides fast deployments.
  • Easy to add/remove capacity.
  • Quick Cloud access.
  • Cost of Ownership is very chip compared to any other servers.
  • Provides Centralized management and Billing.

Disadvantages of AWS

Below are the advantages of using AWS services:
  • If you required extra intensive assistance, you have to select paid support packages.
  • AWS may have some common issues for cloud computing when you are moving to a cloud environment. For ex, backup protection, business downtime, limited control capabilities.
  • AWS sets default limits on resources which are different from region wise. These resources consist of images, snapshots and volumes.
  • If Hardware-level changes done with your application that may impact to performance and usability of your applications.

AWS Compute Services

Below are the list of Cloud Compute Services that offered by Amazon:

  1. EC2(Elastic Compute Cloud) - EC2 is like a virtual machine system in the cloud on which you have to control OS level configurations. You can also run this cloud server whenever you want
  2. LightSail Lightsail computing tool automatically deploys and manages the storage, compute, and networking capabilities required to run your applications.
  3. Elastic Beanstalk  -  Elastic Beanstalk tool do automated deployment and provisioning of resources like a highly scalable production website.
  4. EKS (Elastic Container Service for Kubernetes)-  It allows you to Kubernetes on Amazon cloud environment without installing and managing your own Kubernetes control plane.
  5. AWS Lambda  -  This AWS service allows you to run functions in the cloud. This is a cost saver tool as you have to pay only when your functions get execute.

AWS Service Migration

Migration services generally use for transfer data physically between your data center and AWS.

  1. DMS (Database Migration Service)  - Its allow you to migrate on-site databases(DB) to AWS. It also helps you to migrating one type of database to another database. - for ex, Oracle databse to MySQL database.
  2. SMS (Server Migration Service)  -  It allow you quick and easy to migrating on-site servers to AWS.
  3. Snowball - Snowball application is useful when you wanted to transfer terabytes of data inside and outside of AWS environment.

Storage

  1. Amazon Glacier - It is a low-cost storage service. It offering secured and fast storage for data backup and archiving.
  2. Amazon Elastic Block Store (EBS) - To use with Amazon EC2 instances its providing block-level storage. Amazon Elastic Block Store volumes are network-attached and remain independent from the life of an instance.
  3. AWS Storage Gateway - This AWS service is connecting on-premises software applications with cloud-based storage. It offers secured integration between the organization on-premises and AWS(s) storage infrastructure.

Security Services

  1. IAM (Identity and Access Management)  - It is a secured cloud based security service that helping you to manage user(s), assign policies, form groups to manage more than one users.
  2. Inspector  - Its an agent that you can install on your virtual machine systems, which reports any security vulnerabilities.
  3. Certificate Manager - Its provides no cost SSL certificates for your domain(s) that are managed by Route53.
  4. WAF (Web Application Firewall) - Its provides application-level protection and allows you to block SQL injection and helping you to restrict cross-site scripting attacks.
  5. Cloud Directory - Its allows you to creating flexible, cloud-native directories for managing hierarchies of data along multiple dimensions.
  6. KMS (Key Management Service) - KMS is a managed service. Its helps you to create and control the encryption keys that allows you to encrypt your data.
  7. Organizations - Its allow to create groups of AWS accounts using organizations service to managing security and automation settings.
  8. Shield - Shield is managed DDoS (Distributed Denial of Service protection service). It provides safeguards against web applications that running on AWS.
  9. Macie - Its provides a data visibility security service that helps to classify and protect your sensitive critical content text.
  10. GuardDuty - Its provides threat detection to protect your AWS accounts and workloads.

Database Services

  1. Amazon RDS - It is very easy to set up, operate, and scale a relational database in the cloud environment.
  2. Amazon DynamoDB - It is a fast and fully managed NoSQL database service. Its a simple database service that allow cost-effective storage and retrieval of data. It also allows you to serve large request traffic.
  3. Amazon ElastiCache - Its a web based service which makes it easy to operate, deploy and scale an in-memory cache in the cloud.
  4. Neptune - It’s a reliable, fast and scalable graph database service.
  5. Amazon RedShift - Its Amazon’s data warehousing solution that you can use to perform complex OLAP queries.

Analytics

  1. Athena  - This analytics service allows perm SQL queries on your S3 bucket to find files.
  2. CloudSearch - You can use this AWS service to create a fully managed search engine for your website projects.
  3. ElasticSearch - Its same like CloudSearch. However, it provides extra features like application monitoring.
  4. Kinesis - This analytics service helping you to stream and analyzing real-time data at massive scale.
  5. QuickSight - Its a business analytics tool. It is helping you to create visualizations in a dashboard for data in AWS. For ex, DynamoDB,S3, etc.
  6. EMR (Elastic Map Reduce)  - This analytics service majorly used for big data processing like Hadoop, Spark, Splunk, etc.
  7. Data Pipeline - It allows you to move data from one place to another. For ex, S3 to DynamoDB.

Management Services

  1. CloudWatch - Cloud watch management services helping you to monitor AWS environments like RDS instances, EC2, and CPU utilization. It also triggers alarms depends on various data metrics.
  2. CloudFormation - It is a good way of turning infrastructure into the cloud based. You can use templates for offering a whole production environment in minutes.
  3. CloudTrail - It provides an easy method of auditing AWS resources. It helping you to log all changes.
  4. OpsWorks  - The service allows you to automated Chef/Puppet deployments on AWS environment.
  5. Config - This AWS management service monitors your whole environment. The tool auto sends alerts for changes when someone breaks certain defined configurations.
  6. Service Catalog - This management service helping to large enterprises to authorize which services user will be used and which won't.
  7. AWS Auto Scaling - The management service allowing you to automatically scale your resources up and down based on given CloudWatch metrics.
  8. Systems Manager - This AWS management service allowing you to group your resources. It allowing you to identify issues and act on them.
  9. Managed Services - It provides management of your AWS infrastructure which allows you to focus on your applications.

Internet of Things

  1. IoT Core - IoT Core is a managed cloud based AWS service. This service allows connected devices like light bulbs, sensor grids, cars to securely communicate with cloud applications and other devices.
  2. IoT Device Management - It allowing you to configure/manage your IoT devices at any scale.
  3. IoT Analytics - This service is useful to for analysis on data that collected by your IoT devices.
  4. Amazon FreeRTOS - This real-time operating system for microcontrollers helping you to connect devices like IoT in the local server or into the cloud server.

Application Services

  1. Step Functions  - It is a way of visualizing what's going inside your application and what different micro services it is using.
  2. SWF (Simple Workflow Service) - This service helping you to co-ordinate both automated tasks and human-led tasks.
  3. SNS (Simple Notification Service) - This service basically used to send notifications (Email and SMS).
  4. SQS (Simple Queue Service) - SQS is a pull-based service. This AWS service use to decouple your applications.
  5. Elastic Transcoder - This AWS service tool helping you to changing a video's format and resolution to support various devices like Smartphone’s, tablets and laptops of different resolutions.

Deployment and Management

  1. AWS CloudTrail - This services records AWS API calls and send backlog files to you.
  2. Amazon CloudWatch - Its used to monitor AWS resources like Amazon RDS and Amazon EC2 DB Instances. It also allowing you to monitor custom data metrics created by user's applications and services.
  3. AWS CloudHSM - This AWS service helping you to meet corporate, regulatory, and contractual, compliance requirements for maintaining data security by using the Hardware Security Module(HSM) appliances inside the AWS environment.

Developer Tools

  1. CodeStar  - Codestar is a cloud-based service for creating, managing, and working with various software development projects on AWS.
  2. CodeCommit - Its like services version that control service which allows you to store your code in the cloud and other assets privately in the cloud.
  3. CodeBuild - CodeBuild developer tool helps you to automates the process to building and compiling your code.
  4. CodeDeploy - CodeDeploy allows you to deploying your code in EC2 instances automatically.
  5. CodePipeline - CodePipeline is used to create a deployment pipeline like building, authentication, testing, deployment and production environments.
  6. Cloud9 - Cloud9 is useful for writing, running, and debugging code in the cloud environment.

Mobile Services

  1. Mobile Hub  - Mobile Hub basically used to adding, configuring and designing features for mobile applications.
  2. Cognito - Cognito allowing users to signup using his/her social identity credential.
  3. Device Farm - Device farm helping you to improve the quality of applications by quickly testing hundreds of mobile devices.
  4. AWS AppSync - AWS AppSync service is a fully managed GraphQL service that provides real-time data synchronization functionality and offline programming features.

Business Productivity

  1. Alexa for Business - Its allows you to build your own custom voice for your organization.
  2. Chime - Chime is used for online meeting and video conferencing.
  3. WorkDocs - WorkDocs is used for storing documents on the cloud.
  4. WorkMail  - WokMain allowing you to send/receive business emails.

Desktop & App Streaming

  1. WorkSpaces - Workspace also called as VDI (Virtual Desktop Infrastructure). It allowing you to use remote desktops in the cloud.
  2. AppStream - AppStream is basically used to streaming desktop applications in the web browser. For ex, using MS Excel in web browser.

Artificial Intelligence

  1. Lex - Lex is used to build chat bots quick and easily.
  2. Polly  - Polly is text-to-speech service that allows you to create audio versions of your text notes.
  3. Rekognition - Its a face recognition service. This type of service helping you to recognize faces and object inside images and videos.
  4. SageMaker - Sagemaker is basically useful for machine learning model. It allowing you to build, deploy and train machine learning models at any point.
  5. Transcribe - Transcribe is speech-to-text service that provides high-quality audio and affordable transcriptions.
  6. Translate - Translate is very similar to Google Translate tool that allows you to translate text in one regional language to another regional language.

Augmented Reality & Virtual Reality (AR & VR)

  1. Sumerian - Sumerian is a tools that providing high-quality virtual reality (VR) experiences on the web platform. This service allowing you to creating interactive 3D scenes and also publish it as a website so users can access.

Customer Engagement

  1. Amazon Connect - Amazon Connect allowing you to create your own customer care center in the cloud.
  2. Pinpoint - Pinpoint helping you to understanding your users and engaging with them.
  3. SES (Simple Email Service) - SES helping you to sending bulk emails at a relatively cost-effective price.

Game Development

  1. GameLift - GameLift is basically used to host dedicated game servers. It allowing you to scale seamlessly without taking your game offline. GameLift service managed by AWS.

Applications of AWS services

AWS are widely used for various purposes like:
  • Web site hosting
  • Application hosting/SaaS hosting
  • Media Sharing (Image/ Video)
  • Mobile and Social Applications
  • Content delivery and Media Distribution
  • Storage, backup, and disaster recovery
  • Development and test environments
  • Academic Computing
  • Search Engines
  • Social Networking