Select/Case formatting

BlitzMax Forums/BlitzMax Beginners Area/Select/Case formatting

lotonah(Posted 2008) [#1]
This is another thing I've never seen covered: how do I make a Select/Case statement easier to read?

For example:

Select Lop
Case 16
IncLine=2
xPos=0
dummy = true
Case 24
IncLine=3
xPos=0
dummy = false
End Select

I'd like to see all of these small items on one line, if possible. Like this:

Select Lop
Case 16 : IncLine=2 : xPos=1 : dummy=true
Case 24 : IncLine=3 : xPos=0 : dummy=false
End Select

What's the proper formatting? Obviously the colons don't work under BlitzMax.


Amon(Posted 2008) [#2]
Use ;

:)


lotonah(Posted 2008) [#3]
Holy crap I feel stupid.

For some reason I was still equating ; with the old commenting system (as in, don't use for anything except comments).

Thanks again, Amon.


therevills(Posted 2008) [#4]
I prefer using tabs eg:

Select x
	Case 1
		i = 10
	Case 2
		j = 20
	Case 3
		k = 30
End Select


Slightly O/T, also if you didnt know you can use .. to continue a line eg:

If	x = 1 And .. 
	y = 2 And ..
	z = 3 ..
Then
	i = 10
End If


Which is great for really long if statements...


Grey Alien(Posted 2008) [#5]
I use tabs like the revills too.


lotonah(Posted 2008) [#6]
Hi, therevils

I've used your method in the past for certain things, I just sometimes am tired of seeing a lot of vertical lines going down my code like a=2 so I like to group them all together. So here's what the code now looks like:

Select Lop
Case 8 ;IncLine=1 ;xPos=0
Case 16 ;IncLine=2 ;xPos=0
Case 24 ;IncLine=3 ;xPos=0
Case 32 ;IncLine=4 ;xPos=0
Case 40 ;IncLine=5 ;xPos=0
Case 48 ;IncLine=6 ;xPos=0
Case 56 ;IncLine=7 ;xPos=0
Case 64 ;IncLine=8 ;xPos=0
End Select

A lot more compact, and I still find it quite readable.


Dreamora(Posted 2008) [#7]
and very worthless as you do repetitive if statements for something solved by a single if ...
if Lop mod 8 = 0
  IncLine = Lop / 8
  xPos = 0
endif



Who was John Galt?(Posted 2008) [#8]
If you don't indent, you're nuts.


CS_TBL(Posted 2008) [#9]
Apart from Dreamora's math solution, I try to avoid Select-cases like these when they have a lot of similar/repetitive statements in each Case.
The xPos=0 could well have been outside the Select-Case structure. Another thing to try is to use look-up tables, in use that instead of a Select-Case structure.

A golden rule in programming: "can it be done shorter/more compact, then it's usually better to do so". Even if the performance gain is minimal, the readability may improve as well as flexibility (and thus reusability), which will reward you in the long run.


Dreamora(Posted 2008) [#10]
Not needfully (thought for general it surely is true). if it is only reset in those cases ( assume thats a textlength - multiline splitter ), it must be within actually


lotonah(Posted 2008) [#11]
As for the xPos=0, this is just dummy code. And also, my code is ALWAYS indented.

Thanks for the extra help, but it's unneeded LOL

Cheers,
Don


Czar Flavius(Posted 2008) [#12]
put your code in [ code] [/ code] tags


Delerna(Posted 2010) [#13]
sorry to resurrect a dead thread but I found this thread searching for an answer to the "What is the line continuationcharacter in BMax"

Anyway, I see a number of all encompassing hard and fast "rule" statements relating to how to make code readable.
I agree, code readability is everything, but a rule that makes code readable in one situation can rendera similar piece of code unreadable in another situation......in my humble opinion!

For example
If	x = 1 And .. 
	y = 2 And ..
	z = 3 ..
Then
	i = 10
End If

Yep that does make the code more readable because it is easy to see the conditions that will cause i=10 to be executed

But what about this
If	   (x = 1 And .. 
	   y = 2 And ..
	   z = 3 ..
           ) ..
           or
          (x = 9 And ..
           y = 2 And ..
           x= 3
           ) ..
           or
          (x = 12 And ..
           y = 2 And ..
           x= 3
           ) ..
           or
          (x = 28 And ..
           y = 2 And ..
           x= 3
           or
          (x = 514 And ..
           y = 2 And ..
           x= 3
           ) ..
Then
	i = 10
End If


I don't think that is very readable at all.
In that similar piece of code I would do this.
If        ( x = 1   And  y = 2  And z = 3 ) ..
       or ( x = 9   And  y = 2  And x= 3  ) ..
       or ( x = 12  And  y = 2  And x= 3  ) ..
       or ( x = 28  And  y = 2  And x= 3  ) ..
       or ( x = 514 And  y = 2  And x= 3  )
Then
	i = 10
End If


now I can see the coditions much easier.
So, while general rules such as
"one statement of code per line" and
"One condition or parameter per line"
are usefull for making code readable it is also usefull to break those rules in some cases.
There is nothing wrong in utilizing the extra space on the right hand side of the screen, especially in these days of wide screen moitors.
You can use extra whitespace to separate things out horizontally in order to make it more readable. If two or more lines of code are related in some way then why not use ; to put them all on the same line.
Don't forget, Another "rule for making code readable is
"don't write a method or a fuction that can't be viewed on screen all at the same time"

Using line continuation can help you achieve that and can still be readable.

It just annoys me, slightly, to see someone ask a question and people come out from everywhere and try and say "you mustn't do that"

Same with the

A golden rule in programming: "can it be done shorter/more compact, then it's usually better to do so". Even if the performance gain is minimal, the readability may improve as well as flexibility (and thus reusability), which will reward you in the long run.


My god, if I tried to live by that at work I would be forever chasing the better/shorter/more compact solution and I would never get any software done and neither would I have my Job.
Yes the statement is true but it takes no account of a persons experience/knowledge base. A person starts out life as a programmer knowing nothing and from there he grows and develops his capabilities untill the day he dies. You will never stop finding better/more elegant ways to do things. The day I stop learning and growing is the day I want to be dead.
So yes, the golden rule is true but so is the statement a quality manager in a maufacturing company once made to me.

"Building quality into our product that is over and above what the customer expects is just a waste of our time and it just eats into our profits"

The same is true of software development. There may be a better way to do it that you are not aware of yet but if it does the job and its performance is acceptable and you have a deadline............

I understand the spirit of trying to be helpful but many times all that is required is an answer to the question. It's OK to do that and allow the person to develop and grow on their own.
Anyway, end of sermon....sorry


Amon...congratulations, you answered the question and mine also.
Thank you