Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make my AS3/Air code better?

Hello everyone this is my little Frankenstein code, don't make fun of it, it works! So you would pass in the table name and a data as an Associative array which are objects. I'm pretty sure this is not good code as I was and still am learning ActionScript. So what can I change or how would you guys make it better?

public function save(table:String,data:Object):void
        {
            var conn:SQLConnection = new SQLConnection();
            var folder:File = File.applicationStorageDirectory;
            var dbFile:File = folder.resolvePath("task.db");
            conn.open(dbFile);

            var stat:SQLStatement=new SQLStatement();
            stat.sqlConnection=conn;

            //make fields and values
            var fields:String="";
            var values:String="";
            for(var sRole:String in data)
            {
                fields=fields+sRole+",:";
                stat.parameters[":"+sRole]=data[sRole];
            }
            //trim off white space
            var s:String=new String(fields);
            var cleanString:String=s.slice( 0, -2 );

            //over here we add : infront of the values I forget why
            var find:RegExp=/:/g;
            var mymyField:String=new String(cleanString.replace(find,""));
            cleanString=":"+cleanString;

            var SQLFields:String=mymyField;
            var SQLValues:String=cleanString;

            stat.text="INSERT INTO "+table+" ("+SQLFields+")VALUES("+SQLValues+")";

            stat.execute();
        }
like image 776
Deyon Avatar asked Oct 18 '10 15:10

Deyon


2 Answers

The part where you build your query is quite a horror, to be honest. Half the code removes junk you added just a few lines before. This makes it hard to read and understand. Which is a sign of poor code quality. The following is far shorter and simpler:

        //make fields and values
        var fields:Array = [];
        for(var field:String in data) {
            fields.push(field);
            stat.parameters[":"+field]=data[fieldName];
        }
        var sqlFields:String = fields.join(",");
        var sqlValues:String = ":"+fields.join(",:");

        stat.text="INSERT INTO "+table+" ("+sqlFields+")VALUES("+sqlValues+")";

        stat.execute();
like image 154
back2dos Avatar answered Oct 19 '22 07:10

back2dos


Someone once told me that a stupid idea that works isn't stupid. As programmer's our first goal is (often) to solve business issues; and as long as our code does that then we are successful. You don't need to apologize for code that works.

In terms of what I would do to change your snippet; I might just encapsulate it a bit more. Can the folder, dbFile, and db file name (task.db) be added either as properties to your class or arguments to the method?

Can you separate out the creation of the SQL Statement from the connection handling from your data parsing?

like image 3
JeffryHouser Avatar answered Oct 19 '22 07:10

JeffryHouser