[Gambas-user] tip to make it better

Benoit Minisini gambas at ...1...
Wed Apr 21 19:40:59 CEST 2004


On Wednesday 21 April 2004 07:01, Frank Berg wrote:
> Hi,
>
> i try to make an module, witch can work with ini files like
> getPrivateProfileString(...) in VB
>
> my first try is to read the ini file in an string array..
>
> an question to the CodeMasters: what can i make better??
>
> snip .....................
>
> PRIVATE iniFile AS String[]
> PRIVATE iniFileName AS String
> PUBLIC iniErrorCode AS Integer
> PUBLIC iniErrorText AS Integer
>
> ' read the hole File in an string array,
> PRIVATE FUNCTION read_ini_File(myFileName AS String ) AS String[]
>     DIM f AS File
>     DIM s1 AS String
>     DIM s2 AS String
>     IF myFileName<>iniFileName THEN
>         DO
>             TRY OPEN myFileName FOR READ LITTLE AS #f

Why LITTLE ? This is useless for reading text files.

>             SELECT CASE Error()
>                 CASE 0 ' open command is OK
>                 ' OPEN myFileName FOR READ LITTLE AS #f   ' open the TRY
> line correct?? WHILE NOT Eof(f)
>                     READ #f,s1,1
>                     IF s1<>Chr(13) THEN s2=s2 & s1
>                 WEND

To read line in text files, you have LINE INPUT.

>                 CLOSE #f
>                 iniFile=Split(s2,Chr(10))
>                 iniFileName=myFileName
>                 iniErrorCode=0
>                 iniErrorText="OK"
>                 RETURN iniFile
>             CASE 45 ' file not found
>                 OPEN myFileName FOR CREATE AS #f
>                 CLOSE #f                                                   
>     ' must i close?? 

What for ? You are creating a void file.

>     CASE ELSE 
>                 iniFileName=""
>                 TRY CLOSE #f                        ' will it close if it
> can?? 

Why trying to close something that was not opened ?

> TRY iniFile.Clear                     ' will it clear the array if it 
> can?? 

Why do you want to clear the array if you don't return it ?

> iniErrorCode=Error() 
>                 iniErrorText="Unknown Error for me"   ' where can i read
> obout the error code numbers.. 

The Error class is for you.

> EXIT 

What is this keyword ?

>             END SELECT
>         LOOP
>     ENDIF
>
> CATCH
>     iniFileName=""
>     TRY CLOSE #f
>     TRY iniFile.Clear
>     iniErrorCode=Error()
>     iniErrorText="Unknown Error for me"

I think I have read this code already... :-)

> END
>
>
> snap ...................

I think you can write the same thing clearly with half the lines, by using 
FINALLY.

Each time you write twice the same line of code in a function means that you 
should do better :-)

For example, manage the bad cases at the beginning of the function, and 
returns. This is far easier to read.

I mean :

PRIVATE FUNCTION read_ini_File(myFileName AS String ) AS String[]

  IF BadCase1 THEN RETURN

  IF BadCase2 THEN
    ...
    RETURN
  ENDIF

  OPEN ...

    ...

  CLOSE ...
  RETURN ...

CATCH

  ...

END

Note that file is automatically closed when the function exits when its handle 
is only stored in a local variable.

See what is written in the ./app/Util project in the gambas source package.

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...




More information about the User mailing list