Code refactoring: Reading string data from file and converting the data type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #1

    Code refactoring: Reading string data from file and converting the data type

    Hi ,

    When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ).

    Please suggest me which is the best way of handling the file data.

    I- Method:
    ----------------

    Store as single line string data's(upto end of file ) in a list and make use of this string list for the entire application.

    When we required that single line data, which is having different data types,we convert it and get that details and make use of these data types in our application when and as required

    In a nut shell,while reading the data we are storing as string data,when it is required in the application to do geometric operation ,we convert the data into int /float from the string at that location

    II- Method:
    ---------------

    Traverse the singleline data and convert the string data into the respective datatypes and store in a variable (say list).

    In a nut shell,while reading the data itself ,we are converting the respective data types and storing it,instead of keeping the data in string format


    Store as single line string data for the entire application and whenever you required that single line data,in different data types,you convert it and get the deatils and make use of these data types in your application.

    Thanks
    PSB
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by psbasha
    Hi ,

    When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ).

    Please suggest me which is the best way of handling the file data.

    I- Method:
    ----------------

    Store as single line string data's(upto end of file ) in a list and make use of this string list for the entire application.

    When we required that single line data, which is having different data types,we convert it and get that details and make use of these data types in our application when and as required

    In a nut shell,while reading the data we are storing as string data,when it is required in the application to do geometric operation ,we convert the data into int /float from the string at that location

    II- Method:
    ---------------

    Traverse the singleline data and convert the string data into the respective datatypes and store in a variable (say list).

    In a nut shell,while reading the data itself ,we are converting the respective data types and storing it,instead of keeping the data in string format


    Store as single line string data for the entire application and whenever you required that single line data,in different data types,you convert it and get the deatils and make use of these data types in your application.

    Thanks
    PSB
    I would create a dictionary, set or list from the file data with the values converted. That is not required, but it is my preference. I want to do the work necessary on import to make the data readily available.

    Comment

    • psbasha
      Contributor
      • Feb 2007
      • 440

      #3
      Originally posted by bvdet
      I would create a dictionary, set or list from the file data with the values converted. That is not required, but it is my preference. I want to do the work necessary on import to make the data readily available.

      Most of the experienced developers follow the same approach ,and I hope it is a best practice also.

      But performance wise which is best way of doing.

      Prepare the data in the begining itself by converting ,so that it will be easy for us to do any data manipulation.An d no need to think in the middle of the application development ,whether the data is string or int or etc. we will know that our data has converted in the correct format.

      Otherwise we will get confused when we get different results,and we have to check for each value,whether it is string or other datatype,with "print" out statements.

      Once I faced the problem .when I am passing the ID as "int" to the method.Thinking that I have converted the ID from 'string' to 'int'.When i was struggling for longer time, and after anlayizing the data I came to know that one data type is in "string" format and the input data what i am passing is "int" format


      Thanks
      PSB

      Comment

      • psbasha
        Contributor
        • Feb 2007
        • 440

        #4
        But performance wise which is the best way of doing.??

        Can anybody suggest on this

        -PSB

        Comment

        Working...