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:

public with sharing class FlowerOrderController 
{
    private Flower_Order__c flowerOrder;
    public List<Flower> availableFlowers{
        get;
        set;
    }
    
	public FlowerOrderController(ApexPages.StandardController stdController)
    {
flowerOrder = (Flower_Order__c)stdController.getRecord();
    }
    
    private void setAvailableFlowers(){
        	availableFlowers = new List<Flower>();
        List<Flower__c> 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<Flower_Order_Item__c> selectedFlowers = new List<Flower_Order_Item__c>();
        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;
        }}


}

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:

public with sharing class FlowerOrderController {
    private Flower_Order__c flowerOrder;
    public List<Flower> availableFlowers {get;set;}
    
	public FlowerOrderController(ApexPages.StandardController stdController){
        flowerOrder = (Flower_Order__c)stdController.getRecord();
    }
    
    private void setAvailableFlowers(){
        availableFlowers = new List<Flower>();
        List<Flower__c> 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<Flower_Order_Item__c> selectedFlowers = new List<Flower_Order_Item__c>();
            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;
        }
    }
}

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!

2 Responses to “Simple Trick to Immediately Improve for New Developers”

  1. March 26, 2017 at 9:11 pm #

    Great advice. I would also recommend using a .editorconfig file for ensuring team-wide consistency and sharing your code formatting rules if your IDE allows for it. That way you and your team don’t even have to think about formatting.

    • March 27, 2017 at 7:21 am #

      That is a very good point Rob. Some of this is very simple to automate as well. Very quick and easy way to make sure you stay consistent.

Leave a Comment