Simple Code Advice

When you do code, please avoid declaring the not read-only variable as private property for a class. For example:

avoid-declaring-variable-as-private

If your user reports a bug, then when you want to search for the root cause of the problem, it will be challenging because it is tedious to check.

searching-variable-used

Suggestion

For maintainability purposes, always remember KISS (Keep It Simple Stupid) principle. So for solving that problem, this is how I do it:

internal class RoomReservationHelper
{
	private readonly IPluginExecutionContext _context;
	private readonly IOrganizationService _service;
	private readonly IOrganizationServiceFactory _serviceFactory;

	public RoomReservationHelper(IPluginExecutionContext context,
		IOrganizationServiceFactory serviceFactory, IOrganizationService service)
	{
		_context = context;
		_serviceFactory = serviceFactory;
		_service = service;
	}

	public void Execute()
	{
		var roomReservation = (Entity)_context.InputParameters["Target"];
		var resvNo = roomReservation.Attributes["gent_name"].ToString();

		var anotherRoomReservations = GetRelatedRoomReservation(roomReservation);
	}

	private Entity[] GetRelatedRoomReservation(Entity roomReservation)
	{
		var name = roomReservation.Attributes["gent_name"].ToString();
		var number = .Attributes["gent_roomnumber"].ToString();
		...
	}
}

The Changes?

  • We define the variable when it’s needed.
  • We break 1 function into a smaller function. Make it more clear, more readable.
  • All the property of the class is read-only, so this makes sure you’re class is thread-safe.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.