Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Why Does This Code Come Up As Too Complex

Discussion in 'Programming Questions and Suggestions' started by NickNDS, Feb 23, 2017.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. NickNDS Trainee Engineer

    Messages:
    4
    It should pull the first 3 decimal numbers from the given string and return them as a Vector. In Visual Studios it works perfectly.
    In Visual Studios "X: -123.456, y=--78.9f ZZ100.101" would return "-123.456, -78.9, 100.101".

    Why does Space Engineers register it as too complex?



     
  2. Burillo Junior Engineer

    Messages:
    648
    usually "code too complex" means you have an infinite loop somewhere. i'll take a look at the code in a moment.

    EDIT: it looks like you're doing way too many string operations. i can see you're doing Substring... left and right, which is very expensive. i would suggest doing this instead:

    string substr = text.Substring(i, 1);

    and then compare against that substring. or even:

    bool has_dot = text.CharAt(i) == '.';

    and then use that value in your ifs. lots of similar issues everywhere as well - cache values, rather than recalculating them each time. you could also rewrite your code to be much shorter, as it's pretty much duplicated in places. learn to use functions. also, look into using Regex for parsing, although i'm not sure how performant those are in .NET.

    i would also advise using StringBuilder rather than adding strings to each other.

    overall, your code is horrible, sorry. if you explain the exact format your GPS are supposed to follow, we can rewrite it much more efficiently.
     
    Last edited: Feb 23, 2017
    • Agree Agree x 1
  3. Phoera Senior Engineer

    Messages:
    1,713
    Code:
    bool has_dot = text[i] =='.';
     
    • Agree Agree x 1
  4. Burillo Junior Engineer

    Messages:
    648
    yes, was just too lazy to figure out how to make it non-BBcode :)
    --- Automerge ---
    @NickNDS your code should be rewritten as followes:

    Code:
    Vector3D getVector(string str) {
    		var numbers = new List<double>();
    		var regex = new System.Text.RegularExpressions.Regex("-?[0-9]+(\\.[0-9]+)?");
    		var matches = regex.Matches(str);
    		foreach (Match m in matches) {
    			string num = m.Value;
    			Console.WriteLine(num);
    			numbers.Add(Convert.ToDouble(num));
    		}
    		if (numbers.Count != 3) {
    			return new Vector3D();
    		}
    		return new Vector3D(numbers[0], numbers[1], numbers[2]);
    	}
    
    this will get you a vector out of your string. modify as needed. not sure how fast would it be compared to more low level parsing, but sure as hell better than what you got :)
     
    • Agree Agree x 1
  5. NickNDS Trainee Engineer

    Messages:
    4
    Thanks for all the replies. I appreciate the better solution Burillo.
     
  6. d4rky1989 Apprentice Engineer

    Messages:
    332
    If you use the standard vector string format of SE you can have it as simple as:
    Code:
    Vector3D vec;
    if (!Vector3D.TryParse ("string containing vector data", out vec)) {
        // error handling
    } else {
        Echo ("Vector: " + vec.ToString());
    }
    
    Edit:
    I think the String format for SE vectors is: "{X:<x-value> Y:<y-value> Z:<z-value>}". But I'm not sure about it. Simply print one vector using "Echo (new Vector3D(1, 2, 3).ToString());" and you'll get the correct format
     
    Last edited: Feb 25, 2017
    • Agree Agree x 1
Thread Status:
This last post in this thread was made more than 31 days old.