More Code != Better Code

This is something I thought I should do a quick post about, I have encountered it multiple times with both software engineer managers and recruiters in general and I always cringe when it comes up. “How many lines of code have you written?” and “How many lines of code did you write today?”. These statements typically come from those in charge of engineers with no former technical background or from recruiters that are just out to fill a quota or maybe they are completely unware of what the underlying mechanics of software development are and this is just a left over question that works well for other professions.
For instance if you were hiring a salesman, you might ask them “how many units of X did you sell ?”, This is a perfectly valid question as you could figure out how effective the sales would be because there is a direct correlation with a performance metric, in this case the performance metric is selling units. This also works for positions like accountants where you could ask “how many transaction reports have you done?”, From this you could figure out if an accountant was familiar with a transaction report and how many they have done in the past.
Software development is traditionally employed as a future cost savings measure or to build a particular software product that accomplishes a certain task, its value is in whether or not it accomplishes the task and how much total man-hours it saves. So if we apply the question “How many lines of code have you written?” we are not questioning a final end product like a salesman to get a figure. This question starts to lose meaning as the main goal was to save man hours or produce a certain result, It would be better to ask “How many X man hours did this application save?”. So asking about the number of lines of code in a particular application is irrelevant.
What’s worse is that this question is completely worthless in determining technical ability as well. When asking how many lines of code someone has written, you are asking about strictly quantity, there is no way to know how many lines of code might be redundant or if maybe the code is completely inefficient in doing its intended task. Without a gauge on the quality of code then the number of lines should be irrelevant. Let’s say a snippet of code was copied and pasted throughout various parts of an application, congrats the total line count increased, this must be good right? WRONG, this also presents a problem as all this code might have been able to be condensed into a helper class, which would do the exact same functionality but result in far fewer lines of actual code and would be easier to maintain, code duplication could be a sign that a system was designed improperly. So far this has been a look at the big picture, Let’s break this down to a section of code

public String getTitleNavContainer(boolean isThisLandingPage) {
	if ( isThisLandingPage ){
  	return StringUtils.EMPTY;
	}
	else{
  	return "title-nav-container";
	}
}
public String getTitleNavContainer(boolean isThisLandingPage) {
return isThisLandingPage ? StringUtils.EMPTY : "title-nav-container";
}

With these two examples the first one would be the best right? It has more lines of code so it must be better. Well the problem is both of these snippets do the exact same functionality. The second one is logically equivalent and is condensed into a single line and is simple and concise. This is where using lines of code as a metric completely falls apart, in programming languages you can write logically equivalent code in a variety of ways it makes a tough case to use it as a metric as a person’s coding style would directly affect the number of lines.

So when I hear “How many lines of code have you written?” and “How many lines of code did you write today?”. Maybe its time to take a step back and ask what are they trying to evaluate. As just using line counts as a metric doesn’t give you any actual insight to an application or the person that wrote the application.

Swapping Places

In my recent work in Unity freelancing, I found a need to turn a rapid prototype of a game into a fully working viable product, a lot of the original code was unusable and was not designed to be scaled or reused in any way. Very little thought was given to expanding on the prototype and the project was threatened with development hell if the problem of scalability and maintainability was not solved soon.
This involved creating entirely new classes to organize objects and behaviors as well as widespread use of Unity’s prefab system which for some reason the prototype had no prefabs used at all and all of the objects were just cut and pasted throughout the scene with no way to maintain or update them. For instance if I wanted to add another script to all of a specific type of “enemy” object I would have to manually go into each scene and see if there was a copy of the object and question and manually update it. Simply put this would be a total mess and time consuming and soul draining work.

This mess turned out to be easier to solve than I originally thought, first I created new prefabs for all the objects that needed replaced and then I wrote a small script that would instantiate the prefab with the same position and rotation as the original object. By doing this I could select large amount of objects in the Hierarchy panel and unity and quickly replace them without any hassle.

Here is the script that I used, when placed in a unity project the script should show under a header as “Custom -. Replace Objects”, just select objects in the Hierarchy panel that are going to be replaced and select the replace objects and a modal will appear asking for the prefab that will replace the selected objects, simple, yet effective.

using UnityEngine;
using UnityEditor;

public class ReplaceGameObjects : ScriptableWizard
{
	//Prefab that will replace objects
    public GameObject Prefab;
	//List of objects that will get replaced by the above prefab
    public GameObject[] ObjectsToReplace;
	//Keep the original object names of objects getting replaced
    public bool KeepOriginalNames = false;
    [MenuItem("Custom/Replace GameObjects")]
    static void CreateWizard()
    {
		//Create the script UI in Unity
        var replaceGameObjects = DisplayWizard<ReplaceGameObjects>("Replace GameObjects", "Replace");
        replaceGameObjects.ObjectsToReplace = Selection.gameObjects;
    }
    void OnWizardCreate()
    {
        foreach (GameObject go in ObjectsToReplace)
        {
			//Create new gameobject and set it to the same position and rotations as the old object
            GameObject newObject;
            newObject = (GameObject)PrefabUtility.InstantiatePrefab(Prefab);
            newObject.transform.SetParent(go.transform.parent, true);
            newObject.transform.localPosition = go.transform.localPosition;
            newObject.transform.localRotation = go.transform.localRotation;
            newObject.transform.localScale = go.transform.localScale;
            if (KeepOriginalNames)
                newObject.transform.name = go.transform.name;
			
			//Destroy original object
            DestroyImmediate(go);
        }
    }
}

Isometric Camera: 2D Is 3D, It’s All About Perspective

Isometric View as seen in Starcraft
Isometric View as seen in Starcraft

using UnityEngine;
using System.Collections;

public class IsometricCamera : MonoBehaviour {
    // The View camera 
    public GameObject ViewCamera = null;

    // Camera distance from object
    public float distance = 45.0f;
    // How high up the camera is from object
    public float height = 45.0f;
 
    // Damping for lerping the camera
    public float distanceDamping = 3.0f;
    public float heightDamping = 1.0f;

    void FixedUpdate()
    {

        if (ViewCamera != null)
        {
            // Heights / Distances as defined from above variables
            float wantedHeight = this.transform.position.y + height;
            float wantedDistance = this.transform.position.x + distance;
            float wantedDistanceZ = this.transform.position.z - distance;

            // Current heights / Distances of the object script is attached to
            float currentHeight = transform.position.y;
            float currentDistance = transform.position.x;
            float currentDistanceZ = transform.position.z;

            // Lerp to the wanted height
            currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
            currentDistance = Mathf.Lerp(currentDistance, wantedDistance, distanceDamping * Time.deltaTime);
            currentDistanceZ = Mathf.Lerp(currentDistanceZ, wantedDistanceZ, distanceDamping * Time.deltaTime);

            // Set the camera to the lerp positions and looka at the object
            ViewCamera.transform.position = new Vector3(currentDistance, currentHeight, currentDistanceZ);
            ViewCamera.transform.LookAt(this.gameObject.transform);
        }

    }
}