realbasic-nug
[Top] [All Lists]

Coding Conventions/Style Tips Questions

To: REALbasic Network Users Group <realbasic-nug at lists dot realsoftware dot com>
Subject: Coding Conventions/Style Tips Questions
From: Chris Little <clittle at renlearn dot ca>
Date: Fri, 4 Jan 2002 10:23:33 -0600

Hi all,

I am an experienced C++ programmer and the company I work for has decided to use RB for a new project.

Some of the coding guidelines that I use for C++ will work well for me in RB but I am wondering about others.  In particular I don't want to write in a style that will affect performance (although I know that writing for clarity should be a priority over performance since our instincts are so often wrong about what is truly a bottle-neck in code).

I was reading Theo Smith's web site, in particular the section on Speed/Code size tips, http://www.elfdata.com/programmer/tips.html.  Very interesting and informative but I have some questions about some of his tips.

1. Is there really a difference in this code?

if mybool = true then -> if mybool then
if not mybool then -> if mybool = false then // = false is faster than not!

2. I had planned on defining one variable to a line, a practice that saves some bugs in C++ and should improve readability.  Theo claims that

dim s as string, p as string, q as string

Should be written as

dim s, p, q as string

I would have thought that the compiler would make these lines equivalent.  In fact writing

dim s as string
dim p as string
dim q as string

should be equivalent too.  There is another tip that suggests I should look for unused variables in dim statements and remove them.  Doesn't the compiler strip these?

3. Don't return 0 / "" / nil / false unless you have to

if whichByte <= memsize then
  if mystring.byte(whichByte) = byteToCompare then
    return 1
  else
    return 0
  end if
else
  return -1
end if

->

if whichByte <= memsize then
  if mystring.byte(whichByte) = byteToCompare then
    return 1
  end if
else
  return -1
end if

This tip seems error prone.  I try to follow the one return point rule to simplify reading of code. i.e.

dim aResult as integer
if whichByte <= memsize then
  if mystring.byte(whichByte) = byteToCompare then
    aResult = 1
  else
    aResult = 0
  end if
else
  aResult = -1
end if
return aResult

I would have expected little or no speed/code size difference between these three versions.

4. I tend to write methods and functions that do one thing.  This leads to a lot of small pieces of code.  Is there much overhead in function calls?

5. Get objects to deal with themselves

somewin.someeditfield.text = "a"
somewin.someeditfield2.text = "b"
somewin.someeditfield3.text = "c"
somewin.someeditfield4.text = "d"

->

somewin.somemethod

sub somemethod()
someeditfield.text = "a"
someeditfield2.text = "b"
someeditfield3.text = "c"
someeditfield4.text = "d"
end sub

Although I use the second style anyways, I'm surprised that there is much of a speed/code size difference.

I guess what I am really asking is what is the current state of the RB compiler?  Does it dead strip code and variables, constant propagation, common sub-_expression_ reuse, etc...

Can anyone suggest other sites that I should look at for information like this.

Chris

PS. Theo I hope you don't take this negatively.  I'm just surprised at some of the information that you provide.

<Prev in Thread] Current Thread [Next in Thread>