tags:

views:

551

answers:

9

Is it considered bad practice to use colons to put two statements on the same line?

+12  A: 

There is nothing inherently wrong with using the colon to combine statements. It really depends on the context but as long as it doesn't reduce readability, there is nothing wrong with it.

As a general rule I avoid using the colon for this purpose. I find it's more readable to have one statement per line. However this is not a colon specific issue. I avoid doing the same with a semi-colon in C# or C++. It's just a personal preference.

JaredPar
+1 Agreed. I did VB for more than 10 years, and I didn't even *know* you could put statements on the same line separated by a semi-colon. I wouldn't do this unless the vertical scrollbars on my computer were broken.
MusiGenesis
hey! I just learned my computer has vertical scrollbars!!!
Smandoli
+3  A: 

I'll take the other side. I don't like dense lines of code. It is easier to skim code when lines are not combined.

Combining statements also makes it easier to create long functions that still fit on a single screen.

It isn't a major sin, I just don't like it.

I also don't like one line If statements.

jrcs3
+6  A: 

In general, I'd advise against it, as it makes for busier code.

However, for simple tasks, there is nothing wrong with it. For instance:

for i = 1 to 10: ProcessFoo(i): next

I think a line like this is short enough not to cause confusion.

AngryHacker
+2  A: 

It is considered bad practice in most of the sites at which I have worked. And by most of the VB developers with whom I have worked. And in my head. If I see it, I will admit that I would almost certainly change it. I say "almost" because I admit there's a possibility that I could find a piece of code that looked better that way. I don't expect to see it in my lifetime, though.

I also really don't like one-line Ifs either.

Both are most likely hangovers from the days of VGA (640x480) monitors; that's no excuse these days.

Mike Woodhouse
+2  A: 

I've never seen this mentioned in an official capacity at any of the companies which I've worked. But I do think that using colons excessively can start to make your code less readable and more of a pain to maintain.

I do tend to use these myself at times, for example when checking for cancel in one of my recent projects:

If _bCancel Then Status = CancelProcess() : Return Status

Putting this in kept my code readable than the alternative IF block.

But it can be taken too far, I've recently inherited a project which is replete with examples of taking colon usage too far :

    Select Case GetStringValue(Index).Trim.ToLower
        Case "yes", "y" : GetBooleanValue = True
        Case "no", "n" : GetBooleanValue = False
        Case Else : GetBooleanValue = Nothing
    End Select

Personally I find the above to be a bit much.

David_Jarrett
+1  A: 

I've seen it used in class declarations when using inheritance or implementing an interface:

Public Class DerivedClass : Inherits BaseClass
   ...
End Class

But like the others, I also discourage its use.

Chris

Chris Dunaway
that's vb.net of course. And I think it has C# envy, which is why Inherits is on the same line as the Class
MarkJ
+6  A: 

It's a good practice, in moderation (readibility!), with two considerations. Sometimes readability is enhanced by concatenating two lines:

  • if the lines are short and intimately related (close recordset, set recordset to Nothing).
  • if the lines are short and trivial (such as turn hourglass off, turn screen echo on).

The second consideration is debugging. Step-by-step review is not helped by this concatenation (for examples, setting a breakpoint or determining precise point of error). So any code that has substance should never be concatenated.

Smandoli
UPDATE: You also want to be very careful to not compromise control structures, such as IF blocks. An IF block may seem more readable when reduced from 12 lines to 7, but your code deteriorates in terms of portability, agility. Here too the debugging consideration becomes relevant quickly.
Smandoli
+2  A: 

I've only ever used it when I'm clsoing a recordset and setting the variable to nothing. I figure one line instead of two gives me more lines of code on the screen and doesn't detract from readability.

I've seen it used in simple select cases such as the following but that would be as far as I would go.

 Select Case success
      Case ERROR_FILE_NO_ASSOCIATION: msg = "no association"
      Case ERROR_FILE_NOT_FOUND: msg = "file not found"
      Case ERROR_PATH_NOT_FOUND: msg = "path not found"
      Case ERROR_BAD_FORMAT:     msg = "bad format"

from http://vbnet.mvps.org/index.html?code/system/findexecutable.htm

And even then I would've lined up the "msg =" portion.

Tony Toews
Do ever think of the person who will inherit your code? Have you considered that I cannot place a breakpoint in the VBE IDE on the line msg = "no association" without causing break mode in *every* case?
onedaywhen
Your comment is irrelevant in the only two cases I've ever place two staements on the same line.
Tony Toews
It's entirely relevant for the code you've posted here!
onedaywhen
<shrug> Then we agree to disagree.
Tony Toews
What have we disagreed about? You placing two statements on the same line would mean that I cannot put a breakpoint on the second statement. Isn't that unequivocal?
onedaywhen
So what. You'd never put a breakpoint on a simple Select Case like that.
Tony Toews
What if a defect had been reported that the message "no association" was showing unexpectedly. A search through the code reveals but one instance of that phrase. So I'd want to set a breakpoint on that line. See what I mean?
onedaywhen
BTW I have to do put break points on Case statements quite often when maintaining legacy VBA code, so this a practical point born out of experience of working with other developers, some of who like single line statements.
onedaywhen
Your objections are exceedingly strenuous for such a minor point. But do what you like.
Tony Toews
Stop press: "Geek on SO pays attention to detail." Once again: I don't just do what *I* like, I also consider the person who will inherit my code.
onedaywhen
+2  A: 

To me you shouldn't say "never do thus", you should just say "If you do this, a possible problem is such and such." Then just weigh the pros and cons for yourself. The pro is brevity/few lines of code. Sometimes this can aid readability. For instance some people use it to do vb.Net declarations:

Dim x As Long: x = 1

Or wait loops:

Do Until IE.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

But obviously you can really make it rough on someone too:

Public Sub DoYouKnowWhatThisDoes()
    MsgBox Example
End Sub

Private Function Example()
    Const s$ = "078243185105164060193114247147243200250160004134202029132090174000215255134164128142"
    Const w% = 3: Const l% = 42: Dim i%, r$: For i = 1 To l Step w: r = r & ChrW$(Mid$(s, i, w) Xor Mid$(s, i + l, w)): Next: Example = r
End Function

Another practical reason that you might not want to use this approach is breakpoints. Breakpoints can only be set by the line. So if you have several things executing on the same line you can't isolate the second thing. It will stop on the first statement. (This is also one of the reasons some people don't like single line ifs.) It just complicates debugging a little.

I usually don't use colons in production code for this reason. However I do use them to improve the brevity of "copy/paste" code that I post in forums and elsewhere. YMMV:)

Oorang