HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Reading code from array efficiently

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
readingefficientlyarraycodefrom

Problem

I'm trying to read data from an array. I manage to get the information I want but my code is extremely inefficiently written.

My array looks like this:

var arTab:Array = new Array();
arTab[0] = new Array(2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012);
var promilleTab:Array = new Array();
promilleTab[0] = new Array(9631, 8593, 8363, 8128, 8514, 8534, 8560, 8146, 8241, 8019,         8759);
var forerkortTab:Array = new Array();
forerkortTab[0] = new Array(13481, 12785, 12585, 12492, 13470, 14181, 14622, 14082, 14287, 13640, 14180);
var hastighetTab:Array = new Array();
hastighetTab[0] = new Array(9863, 12217, 14920, 14929, 15425, 18010, 15909, 14197, 13276, 11158, 12264);
var trafikklovenTab:Array = new Array();
trafikklovenTab[0] = new Array(18862, 19196, 20101, 20026, 21381, 21845, 20005, 19446, 20900, 19346, 20265);


I am using combo boxes to pick a year from the "arTab" array and another to pick the felony done.(the 3 other arrays)

What I am doing right now looks something like this:

function skrivUt(Event:MouseEvent)
{
    if(bruddListe == 1)
    {
        if(arListe == 1)
        {
            txtSvar.text = "I " + arTab[0][0] + " ble det anmeldt " + promilleTab[0][0] + " for promillekjøring";
        }
        if(arListe == 2)
        {
            txtSvar.text = "I " + arTab[0][1] + " ble det anmeldt " + promilleTab[0][1] + " for promillekjøring";
        } 
        if(arListe == 3)
        {
            txtSvar.text = "I " + arTab[0][2] + " ble det anmeldt " + promilleTab[0][2] + " for promillekjøring";
        }
    {
{


This is just a small example but the list goes on because I have to do 11 results for all the 4 combo choices.

Is there any way I can write this in a shorter and more efficient way?

Solution

Sure,

you are basically copy pasting to keep accessing arTab and promilleTab, you might instead just derive the indexes in arTab from bruddListe and arListe, like this:

function skrivUt(Event:MouseEvent)
{
  txtSvar.text = "I " + arTab[bruddListe-1][arListe-1] + " ble det anmeldt " + promilleTab[bruddListe-1][arListe-1] + " for promillekjøring";

etc. etc.


or you could keep things a bit more readable, by splitting it up, I dont know if this is correct ActionScript, but conceptually you should grok this:

function skrivUt(Event:MouseEvent)
{
  var ar:String =  arTab[bruddListe-1][arListe-1];
  var promille:String = promilleTab[bruddListe-1][arListe-1];
  txtSvar.text = "I " + ar + " ble det anmeldt " + promille + " for promillekjøring";


Furthermore, some other observations :

  • Try to keep your variables and function Names in English, it's the right thing to do



  • it seems as if bruddListe and arListe are global variables, you should avoid those

Code Snippets

function skrivUt(Event:MouseEvent)
{
  txtSvar.text = "I " + arTab[bruddListe-1][arListe-1] + " ble det anmeldt " + promilleTab[bruddListe-1][arListe-1] + " for promillekjøring";

etc. etc.
function skrivUt(Event:MouseEvent)
{
  var ar:String =  arTab[bruddListe-1][arListe-1];
  var promille:String = promilleTab[bruddListe-1][arListe-1];
  txtSvar.text = "I " + ar + " ble det anmeldt " + promille + " for promillekjøring";

Context

StackExchange Code Review Q#51563, answer score: 5

Revisions (0)

No revisions yet.