Skip to main content

Notifications

Community site session details

Community site session details

Session Id :

Simple Code Advice

Temmy Wahyu Raharjo Profile Picture Temmy Wahyu Raharjo 2,914

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

avoid-declaring-variable-as-private

If your user report bug, then when you want to search the root cause of the problem, will be challenging because it is very hard to check.

searching-variable-used

Suggestion

For maintainability purpose, 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;</code>

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?

  1. We define variable when it’s needed.
  2. We break 1 function to smaller function. Make it more clear, more readable.
  3. All the property of the class is readonly, so this make sure you’re class is thread safe.

This was originally posted here.

Comments

*This post is locked for comments