Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[BEGINNER] Trying to write efficient code

Name: Anonymous 2010-05-07 12:03

I am writing small, simple script that deals with a flat-file database. It only uses 1 text file for this and it is quite small at the moment. However, this text file has the potential to become big because as my script is used, it continually adds new information to this text file.

To keep it simple, this is what my script does: It adds a new line to the top of the db file.

I have two options here:

1. Load the entire db file into a list (memory),
unshift new line,
then write it back into the file.

2. Create a temporary file,
write new line to temp file,
using a while loop (line-by-line) write the entire db file into the temp file,
then replace (rename) the original db file with the newer temp file.

It seems to me that option 2 is better because it doesn't load the entire file into memory. Of course, I'm just a novice and drawbacks are not as obvious to me.

So I guess my questions are: Am I correct in my assumption that option 2 is better? Or am I missing something? Is one option more cpu-intensive than the other?

Name: Anonymous 2010-05-07 18:31

>>14

Write comments only where the code is incapable of explaining itself.
Prefer self-explanatory code over explanatory comments.  Avoid
`literate programming' like the plague.

  Rationale:  If the code is often incapable of explaining itself, then
  perhaps it should be written in a more expressive language.  This may
  mean using a different programming language altogether, or, since we
  are talking about Lisp, it may mean simply building a combinator
  language or a macro language for the purpose.  `Literate programming'
  is the logical conclusion of languages incapable of explaining
  themselves; it is a direct concession of the inexpressiveness of the
  computer language implementing the program, to the extent that the
  only way a human can understand the program is by having it rewritten
  in a human language.


http://mumble.net/~campbell/scheme/style.txt

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List