Code Reviews

Code reviews help programmers improve their code and learn more about the software they build.

A code review is the process of examining a peer’s code to see they have adhered to the coding guidelines for the group or organisation they belong to. Constructive feedback will help the person who wrote the code learn how to improve.

What does it mean to refactor the code?

When writing code, you usually write enough to get it working, then once it works, you look for ways to split up the code into separate methods and classes. This is called refactoring.

When refactoring code, you are looking for things like duplicate code, where the same lines of code are used multiple times. You can put this code in a separate method and have the original code call the new method from each of the places where it was before. Now if you need to change the code which runs in this new method, you only have to change it in one place rather than in all of the places where it appeared before.

The other benefit to this is being able to give the method a meaningful name. Where it was a set of lines of code before, perhaps it was performing a calculation, you can name the method to say what it is doing e.g. CalculateDistanceBetweenTwoLocations().

This is also useful when you have a method which is doing lots of things, you can put them into separate methods with meaningful names. So instead of one method with many lines it could look like this:

public double GetDistanceToDestination()
	LocationPoint userLocation = GetTheCurrentUserLocation();
	LocationPoint destinationLocation = GetTheDestinationLocation();
	return CalculateDistanceBetweenTwoLocations(userLocation, destinationLocation);

What are external dependencies?

When you write code, you want it to be testable. It is hard to test it if the code itself creates new objects. You want to be able to control what objects are used in the test.
It is best to pass in these objects as variables into the parameters of the constructor. This way your code is not relying on any external dependencies.

//How not to do it
public class DependencyExample
	//This method creates a new instance of the NotificationService within it. This is an external dependency and makes it difficult to test.	
	public bool SendNotificationToCustomer(string emailAddress, string details)
		NotificationService notifications = new NotificationService();
		return service.Send(emailAddress, details);

//A better way to do it
public class DependencyExample
	//We have created a private variable for the _notifications service to it can be set and used within the methods.
	private NotificationService _notifications { get; set; }

	//This is the constructor for the class. We can set the new _notifications private variable here
	public DependencyExample(NotificationService notifications)
		_notifications = notifications;
	//Now it uses the private variable _notifications which was set up in the constructor
	public bool SendNotificationToCustomer(string emailAddress, string details)
		return _notifications.Send(emailAddress, details);

Learn More


  • Paul Seal

    Paul Seal is a .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with my wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Also In The April 2017 Issue

We all use fonts yet rarely notice they are designed. Here are some interesting details to help you notice fonts.

This iPad app is a creative tool kids can use to explore and record what they learn in school.

Smart software design makes it easy for you to learn how to use it without help.

A new version of a fun Mario-like game that teaches kids coding has been released. Learn HTML and save kittens!

Mark is a designer who also knows a lot about how to use technology to create design.

Design is about solving problems, from donuts to race cars, how we eat to what to wear in cold weather.

Everyone know the difference between saying, “Let’s eat, grandma!” and “Let’s eat grandma!” Computers don't.

The user interface often determines whether or not people can easily use your software.

State is an important concept in computer science as well as our everyday lives.

Links from the bottom of all the April 2017 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for April 2017.

Computers collect garbage the way humans do. Here's how they manage memory space.

Code reviews help programmers improve their code and learn more about the software they build.

Interested but not ready to subscribe? Sign-up for our free monthly email newsletter with curated site content and a new issue email announcement that we send every two months.

No, thanks!