Mega Code Archive

 
Categories / Delphi / Examples
 

Making your source code with arrays easier to read

If your source code include references to arrays, it maybe better to refer to items in those arrays using pre-defined constant variable names rather than plain old numbers. Let's take the following code for example, which basically sets up fields of a database table: Fields[ 0 ].AsString := 'Joe Blow'; Fields[ 1 ].AsInteger := 25; Fields[ 3 ].AsString := 'Hiking'; Of course, unless you know which fields are at which array position, it could make it harder to maintain above code. Let's replace those numbers with constant names: const // TC = Table Clients (clients.dbf) for example TC_FULLNAME = 0; TC_AGE = 1; TC_HOBBIES = 3; // ... other code ... Fields[ TC_FULLNAME ].AsString := 'Joe Blow'; Fields[ TC_AGE ].AsInteger := 25; Fields[ TC_HOBBIES ].AsString := 'Hiking'; The extra time it would take to create those constant names will most likely be paid off when you have to modify your source code in the future, specially if someone else has to maintain your code.