Simple Trick to Immediately Improve for New Developers

I’ve written before about writing clean code and being a software professional, both of which are large contributors to becoming a better developer. However, today I want to talk about 1 small thing new developers can do to immediately improve their capabilities in the eyes of their peers. That one thing is consistency!

This might seem like such a silly thing, but before you click away, hear me out. The most important part of software development tends to happen after the initial piece of functionality is written. The reality is a large part of an application’s cost is spent during maintenance. Part of this reality is that you will have to come back to the work you are doing now and relearn the code that you had written. This means it is incredibly important to make your code consistent, which has the benefit of making it easier to read and easier to modify. Let me give you an example.

Imagine the company you work for sells flowers. You create a very simple page which allows you to create a flower order and add flowers to it. On your first attempt, this is how the controller code turns out:

[java]
public with sharing class FlowerOrderController
{
private Flower_Order__c flowerOrder;
public List availableFlowers{
get;
set;
}

public FlowerOrderController(ApexPages.StandardController stdController)
{
flowerOrder = (Flower_Order__c)stdController.getRecord();
}

private void setAvailableFlowers(){
availableFlowers = new List();
List flowersFromDb = [SELECT Id, Name FROM Flower__c WHERE Is_Active__c = true];
for(Flower__c singleFlower:flowersFromDb){
availableFlowers.add(new Flower(singleFlower));
}
}

public PageReference saveFlowerOrder(){
Savepoint sp = Database.setSavepoint();
try
{
insert flowerOrder;
List selectedFlowers = new List();
for(Flower flowerWrapper:availableFlowers){
if(flowerWrapper.isSelected){
selectedFlowers.add(new Flower_Order_Item__c(
Flower__c = flowerWrapper.singleFlower.Id,
Flower_Order__c = flowerOrder.Id
));}}

insert selectedFlowers;
return new ApexPages.StandardController(flowerOrder).view();
}
catch(Exception ex)
{
Database.rollback(sp);ApexPages.addMessages(ex);
}
return null;
}

public class Flower
{
public Flower__c singleFlower {get;
set;}
public Boolean isSelected {get;set;}

public Flower(Flower__c singleFlower)
{
this.singleFlower = singleFlower;
}}

}
[/java]

How many times have you seen something like this? This code will run just fine and it should do exactly what we expect. The problem is that it is difficult to read. This will make it very hard to modify in the future, as you have to relearn what the code does when you go to make a change. The most frustrating part of all of this? There is simply no reason for it. It is very easy to clean something like this up, and it provides a ton of value. New developers focus so much on just getting something to work, that once they do, they sometimes just stop to make sure it continues to work. They got the job done, right? Well, why not spend 5 minutes, clean up the file, and you can get something like this:

[java]
public with sharing class FlowerOrderController {
private Flower_Order__c flowerOrder;
public List availableFlowers {get;set;}

public FlowerOrderController(ApexPages.StandardController stdController){
flowerOrder = (Flower_Order__c)stdController.getRecord();
}

private void setAvailableFlowers(){
availableFlowers = new List();
List flowersFromDb = [
SELECT
Id, Name
FROM
Flower__c
WHERE
Is_Active__c = true
];
for(Flower__c singleFlower:flowersFromDb){
availableFlowers.add(new Flower(singleFlower));
}
}

public PageReference saveFlowerOrder(){
Savepoint sp = Database.setSavepoint();
try{
insert flowerOrder;
List selectedFlowers = new List();
for(Flower flowerWrapper:availableFlowers){
if(flowerWrapper.isSelected){
selectedFlowers.add(
new Flower_Order_Item__c(
Flower__c = flowerWrapper.singleFlower.Id,
Flower_Order__c = flowerOrder.Id
)
);
}
}
insert selectedFlowers;

return new ApexPages.StandardController(flowerOrder).view();
}catch(Exception ex){
Database.rollback(sp);
ApexPages.addMessages(ex);
}

return null;
}

public class Flower{
public Flower__c singleFlower {get;set;}
public Boolean isSelected {get;set;}

public Flower(Flower__c singleFlower){
this.singleFlower = singleFlower;
}
}
}
[/java]

I didn’t change a single thing between those two examples, except for consistently formatting my code. That simple change, which only took about a minute or two, immediately improves the quality of the work I have done and makes it much easier to maintain in the future. Moral of the story is that it is important to make sure things are clean before you finish your work. Spending a few minutes to simply format your code, can help tremendously in the future. Once you get into the habit of constantly keeping it formatted, it will become second nature and you will immediately be viewed in a better light by your fellow devs. Enjoy!

Advertisement

Go to Smartblog Theme Options -> Ad Management to enter your ad code (300x250)

Comments are closed.