Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Not all code paths return a value

Discussion in 'Editor & General Support' started by theo9743, Apr 17, 2020.

  1. theo9743

    theo9743

    Joined:
    Feb 18, 2020
    Posts:
    12
    Hi there,

    To start off with please forgive me if this is not in the right place!

    So, I've devised a simple ID checker whose only conditions to return true are that it is a certain length, and currently exists in my database. For some reason, It claims not all code paths return a value even though they do as far as I can tell? I'm using SQLite for reference.

    Many thanks in advance.

    Code (CSharp):
    1. private bool PlayerIDChecker(string id)
    2. {
    3.     if (id.Length < 3 || id.Length > 16)
    4.     {
    5.         return false;
    6.     }
    7.  
    8.     // Next check requires Initialising file path to DB
    9.     connectDB = "URI=file:" + Application.dataPath + "/HighScoreDB.db";
    10.  
    11.     using (IDbConnection dbConnection = new SqliteConnection(connectDB))
    12.     {
    13.         dbConnection.Open();
    14.         using (IDbCommand dbCmd = dbConnection.CreateCommand())
    15.         {
    16.             // The EXISTS statement will return a 1 if the desired data exists, and a 0 if not
    17.             sqlQuery = String.Format($"SELECT EXISTS (SELECT * FROM USERS WHERE PlayerID = '{id}')");
    18.             dbCmd.CommandText = sqlQuery;
    19.  
    20.             string exists = dbCmd.ExecuteScalar().ToString();
    21.  
    22.             // Record exists
    23.             if (exists == "1")
    24.             {
    25.                 return true;
    26.             }
    27.             // Record doesn't exist
    28.             else if (exists == "0")
    29.             {
    30.                 return false;
    31.             }
    32.         }
    33.         dbConnection.Close();
    34.      
    35.     }
    36.  
    37. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So.... what value does this method return once it executes dbConnection.Close() on line 33?
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Your return values are inside using blocks, and you have code that needs to run outside of those blocks.
    If this would run, you would never actually close the database connection on line 33, because it would never reach that point.

    Personally, I like using the "only one return statement" approach to writing methods. I find it easier to debug and it guarantees that all code paths can run.
    Code (CSharp):
    1. private bool PlayerIDChecker(string id)
    2. {
    3.     bool result = !(id.Length < 3 || id.Length > 16);
    4.  
    5.     if(!result) {
    6.         // Next check requires Initialising file path to DB
    7.         connectDB = "URI=file:" + Application.dataPath + "/HighScoreDB.db";
    8.  
    9.         using (IDbConnection dbConnection = new SqliteConnection(connectDB))
    10.         {
    11.             dbConnection.Open();
    12.             using (IDbCommand dbCmd = dbConnection.CreateCommand())
    13.             {
    14.                 // The EXISTS statement will return a 1 if the desired data exists, and a 0 if not
    15.                 sqlQuery = String.Format($"SELECT EXISTS (SELECT * FROM USERS WHERE PlayerID = '{id}')");
    16.                 dbCmd.CommandText = sqlQuery;
    17.  
    18.                 string exists = dbCmd.ExecuteScalar().ToString();
    19.                 result = exists == "1";
    20.             }
    21.             dbConnection.Close();
    22.         }
    23.     }
    24.  
    25.     return result;
    26. }
     
    Joe-Censored and theo9743 like this.
  4. theo9743

    theo9743

    Joined:
    Feb 18, 2020
    Posts:
    12
    Ah I see! Thanks for your help