11 Replies - 2121 Views - Last Post: 25 January 2013 - 06:53 AM Rate Topic: -----

#1 ybadragon  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Single line If, or Multiline?

Posted 23 January 2013 - 07:37 PM

Hello all,

I learned about single line If statements today, and really like the idea of them. I was wondering what you all preferred, as in Single line If, Multiline If/Else, or Select Case Statement, and why do you prefer one over the other?

I like the single line If statements when I'm performing difficult logic in my program, because I can see it all in that one line. On the other hand using single line if statements makes it harder to debug a program.

Looking forward to hearing your answers! :bigsmile:/>

Is This A Good Question/Topic? 0
  • +

Replies To: Single line If, or Multiline?

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Single line If, or Multiline?

Posted 23 January 2013 - 08:02 PM

I prefer multi-line Ifs by a large margin. The saving is, after all, only two lines, and if editor real estate is that important to me, I'll bracket the multi-line If with a "region" directive.
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1031
  • View blog
  • Posts: 4,013
  • Joined: 02-July 08

Re: Single line If, or Multiline?

Posted 23 January 2013 - 08:12 PM

2 options If Else...
More than 2 options Select Case

Select Case is nice since it does top down processing of choices and stops when found.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Single line If, or Multiline?

Posted 23 January 2013 - 08:14 PM

Quote

using single line if statements makes it harder to debug a program.


There's your answer! Single-line IFs should be used sparingly, and only for a single (obvious) statement. Some would say don't use them at all. (If you need to add further statements later you'll only have to break it apart anyway.)

There are plenty of discussions on the internet about when to chose IF..ELSE or SELECT..CASE. Use SELECT..CASE when it is natural to do so is my advice. I would have to check, but I feel sure that there won't be a performance difference [someone might correct this last statement - I recall in Javascript there can be (or used to be)].

We use SELECT..CASE when choosing between sets of discrete (or clearly defined ranges) of values.

Quote

Select Case is nice since it does top down processing of choices and stops when found.


Doesn't IF..ELSE?!

This post has been edited by andrewsw: 23 January 2013 - 08:17 PM

Was This Post Helpful? 0
  • +
  • -

#5 ybadragon  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: Single line If, or Multiline?

Posted 23 January 2013 - 10:01 PM

I appreciate all of your inputs, and I'll keep them in mind.

Quote

The saving is, after all, only two lines, and if editor real estate is that important to me, I'll bracket the multi-line If with a "region" directive.


If it were only two lines, I wouldn't really mind it, but for what I'm doing, it save much more than two lines.

It's a program I've created for work to get some customer ID's.

This is a sample, just to show the logic, of what I created at work for this purpose. I had to substitute in strings in lines List(Of String), as well as the other vars, for security reasons.

Spoiler


Also, I know not to use generic names for variables, and default names for forms and buttons, this was just a spur of the moment thing that I whipped up to show you.

This post has been edited by ybadragon: 23 January 2013 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

#6 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: Single line If, or Multiline?

Posted 24 January 2013 - 09:56 AM

I personally prefer Multiline If statements. Select case can get very, very annoying if it gets too long, in my opinion.
Was This Post Helpful? 0
  • +
  • -

#7 ybadragon  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: Single line If, or Multiline?

Posted 24 January 2013 - 10:39 AM

Quote

Select case can get very, very annoying if it gets too long, in my opinion.


That is how I feel, I have seen (where I work) Select Case statements that go on for over 50 cases. It was extremely difficult to read, so I created a class structure to fix all of them. The select case statement is still 32 cases, but I had to create a class for each type of file that needed processed.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Single line If, or Multiline?

Posted 25 January 2013 - 03:36 AM

If I had so many select..case clauses I would consider converting it to a dictionary lookup. Example.
Was This Post Helpful? 0
  • +
  • -

#9 ybadragon  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: Single line If, or Multiline?

Posted 25 January 2013 - 06:06 AM

View Postandrewsw, on 25 January 2013 - 10:36 AM, said:

If I had so many select..case clauses I would consider converting it to a dictionary lookup. Example.


I have used Dictionary in the past, and that is a good article for it's use, I'll look into it some more when I get to work.

I was also looking into event handlers, since I've needed to make a class for each type of file, the just having each class have a listener to listen for a string in the event call.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Single line If, or Multiline?

Posted 25 January 2013 - 06:24 AM

Quote

I was also looking into event handlers, since I've needed to make a class for each type of file, the just having each class have a listener to listen for a string in the event call.


This is not directly tied to the subject of SELECT..CASE. But you should not have to create a class for each type of file. I would look into Streams (the IStream Interface), creating your own interface, .. Well, there are many ways to handle different file-types other than creating a class for each one.
Was This Post Helpful? 0
  • +
  • -

#11 ybadragon  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: Single line If, or Multiline?

Posted 25 January 2013 - 06:33 AM

View Postandrewsw, on 25 January 2013 - 01:24 PM, said:

This is not directly tied to the subject of SELECT..CASE. But you should not have to create a class for each type of file. I would look into Streams (the IStream Interface), creating your own interface, .. Well, there are many ways to handle different file-types other than creating a class for each one.


I'll look into that. Yeah, I didn't mean to go of topic there.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Single line If, or Multiline?

Posted 25 January 2013 - 06:53 AM

View Postybadragon, on 25 January 2013 - 06:33 AM, said:

I didn't mean to go of topic there.

I wasn't criticising :) I just mentioned it for others who might read this thread. Andy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1