473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

more elegant way than this

hello, I have this function below which is simple and easy to understand

private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do better.
is there a more "professional" way or "elegant" way to do this?

Ed
Nov 21 '05 #1
13 1181
How about:

Private Function ListHeight(ByVal UserScreenHeight As Integer) As Integer

Return IIf(UserScreenHeight < 1024, 30, 50)

End Function

BTW you can't use Int in VB.Net. I thas to be integer or Int16, etc

Ged

"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> wrote in message
news:uP**************@TK2MSFTNGP11.phx.gbl...
hello, I have this function below which is simple and easy to understand

private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do better.
is there a more "professional" way or "elegant" way to do this?

Ed

Nov 21 '05 #2
Edward,
hello, I have this function below which is simple and easy to understand
It is very functional Personally those are attributes that all methods should strive for!

is there a more "professional" way or "elegant" way to do this? Only thing I would consider changing is the name of the function, normally I
name functions & sub as action statements (verb or verb noun), where as I
normally name Properties as a noun.

So I would name the function GetListHeight, as a ListHeight function
suggests that I am listing (List as in verb) Height (noun), if I were
listing Height, I would probably make it plural so it reads better...

Hope this helps
Jay
"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> wrote in message
news:uP**************@TK2MSFTNGP11.phx.gbl... hello, I have this function below which is simple and easy to understand

private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do better.
is there a more "professional" way or "elegant" way to do this?

Ed

Nov 21 '05 #3
Ged,
Try adding Option Strict On to the source file, you will receive compile
errors.

As IIf expects object parameters & returns an object, you would need a
DirectCast, CType, or CInt around the IIf to cast the value returned back to
Integer.

Hope this helps
Jay

"Ged Mead" <ge*********@tesco.net> wrote in message
news:uA*************@newsfe2-gui.ntli.net...
How about:

Private Function ListHeight(ByVal UserScreenHeight As Integer) As Integer

Return IIf(UserScreenHeight < 1024, 30, 50)

End Function

BTW you can't use Int in VB.Net. I thas to be integer or Int16, etc

Ged

"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> wrote in message
news:uP**************@TK2MSFTNGP11.phx.gbl...
hello, I have this function below which is simple and easy to understand

private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do
better.
is there a more "professional" way or "elegant" way to do this?

Ed


Nov 21 '05 #4
Thanks, Jay.

Good point ;-} . I hadn't spotted that.

Ged

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Ged,
Try adding Option Strict On to the source file, you will receive compile
errors.

As IIf expects object parameters & returns an object, you would need a
DirectCast, CType, or CInt around the IIf to cast the value returned back to Integer.

Hope this helps
Jay

"Ged Mead" <ge*********@tesco.net> wrote in message
news:uA*************@newsfe2-gui.ntli.net...
How about:

Private Function ListHeight(ByVal UserScreenHeight As Integer) As Integer
Return IIf(UserScreenHeight < 1024, 30, 50)

End Function

BTW you can't use Int in VB.Net. I thas to be integer or Int16, etc

Ged

"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> wrote in message
news:uP**************@TK2MSFTNGP11.phx.gbl...
hello, I have this function below which is simple and easy to understand
private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do
better.
is there a more "professional" way or "elegant" way to do this?

Ed



Nov 21 '05 #5
Hi Edward

In addition to the other answers, I prefer to have only one exit point. Thus

<code>
Private Function GetListHeight (ByVal userScreenHeight As Integer) As
Integer

Dim height As Integer

If userScreenHeight < 1024 Then
height = 30
Else
height = 50
End If

Return height

End Function
</code>

HTH

Charles
"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> wrote in message
news:uP**************@TK2MSFTNGP11.phx.gbl...
hello, I have this function below which is simple and easy to understand

private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do better.
is there a more "professional" way or "elegant" way to do this?

Ed

Nov 21 '05 #6
Edward,

You make the same error as I always do, I wanted they made that "then" not
needed anymore, it does nothing. I would keep it this way you do, however
possible is as well

private function GiveListHeight (byval UserScreenHeight as integer) as
integer
if UserScreenHeight < 1024 then return 30
return 50
end function

However I never do that like this and would take your way.

Cor
Cor

"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> schreef in bericht
news:uP**************@TK2MSFTNGP11.phx.gbl...
hello, I have this function below which is simple and easy to understand

private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do better.
is there a more "professional" way or "elegant" way to do this?

Ed

Nov 21 '05 #7
Or more likely Charles
Nov 21 '05 #8
Cor,
How about:
private function GiveListHeight (byval UserScreenHeight as integer) as
integer
if UserScreenHeight < 1024 then return 30 else return 50
end function Which is Ged's example without needing DirectCast.
I will use a "guard statement" (similar to what you have) when there is more
logic in the Else block over the Then block. Or when validating parameters:
private function ListHeight (byval UserScreenHeight as integer) as
integer
If UserScreenHeight < 0 Then Throw New
ArgumentOutOfRangeException("UserScreenHeight", UserScreenHeight, "User
Screen Height cannot be Negative.")
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function


Although I'm not sure if this function needs to validate its parameter...

Just a thought
Jay
"Cor Ligthert" <no************@planet.nl> wrote in message
news:u7*************@TK2MSFTNGP14.phx.gbl... Edward,

You make the same error as I always do, I wanted they made that "then" not
needed anymore, it does nothing. I would keep it this way you do, however
possible is as well

private function GiveListHeight (byval UserScreenHeight as integer) as
integer
if UserScreenHeight < 1024 then return 30
return 50
end function

However I never do that like this and would take your way.

Cor
Cor

"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> schreef in bericht
news:uP**************@TK2MSFTNGP11.phx.gbl...
hello, I have this function below which is simple and easy to understand

private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do
better. is there a more "professional" way or "elegant" way to do this?

Ed


Nov 21 '05 #9

"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> wrote
hello, I have this function below which is simple and easy to understand

It is very functional but seems pretty lame to me, like I could do better.
is there a more "professional" way or "elegant" way to do this?

No one has mentioned this style yet:

Private Function ListHeight (Byval UserScreenHeight As Integer) As Integer
ListHeight = 50
If UserScreenHeight < 1024 Then ListHeight = 30
End Function
LFS
Nov 21 '05 #10
Edward,
<me too> :-)
I just wanted to add you can also use Select Case:

Private Function GetListHeight(ByVal userScreenHeight As Integer) As
Integer
Select Case userScreenHeight
Case Is < 1024
Return 30
Case Else
Return 50
End Select
End Function

</me too>

I've been at shops that prefer Select Case over If, although I really don't
see what it buys you in the above...

I could see using Select Case if you had multiple return values.

Select Case userScreenHeight
Case Is < 0
Throw New ArgumentOutOfRangeException("UserScreenHeight",
userScreenHeight, "User Screen Height cannot be Negative.")
Case Is < 800
Return 25
Case Is < 1024
Return 30
Case Else
Return 50
End Select

Of course you can do variations on the above...

Just a thought
Jay

"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> wrote in message
news:uP**************@TK2MSFTNGP11.phx.gbl...
hello, I have this function below which is simple and easy to understand

private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do better.
is there a more "professional" way or "elegant" way to do this?

Ed

Nov 21 '05 #11
"Cor Ligthert" <no************@planet.nl> schrieb:
private function GiveListHeight (byval UserScreenHeight as integer) as
integer
if UserScreenHeight < 1024 then return 30
return 50
end function


Well, I prefer the original solution or the 'Select Case' solution too,
because it's easier to extend and understand.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #12
I like this one the best. We often used to have geeky debates over a!=0?b:c
in C++. The conclusion was that if it doesn't buy you anything (in terms of
performance in a performance critical piece of code) then prefer the more
readable solution, which is not neccessarily the one with greatest brevity.


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eB**************@TK2MSFTNGP15.phx.gbl...
Edward,
<me too> :-)
I just wanted to add you can also use Select Case:

Private Function GetListHeight(ByVal userScreenHeight As Integer) As
Integer
Select Case userScreenHeight
Case Is < 1024
Return 30
Case Else
Return 50
End Select
End Function

</me too>

I've been at shops that prefer Select Case over If, although I really
don't see what it buys you in the above...

I could see using Select Case if you had multiple return values.

Select Case userScreenHeight
Case Is < 0
Throw New ArgumentOutOfRangeException("UserScreenHeight",
userScreenHeight, "User Screen Height cannot be Negative.")
Case Is < 800
Return 25
Case Is < 1024
Return 30
Case Else
Return 50
End Select

Of course you can do variations on the above...

Just a thought
Jay

"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> wrote in message
news:uP**************@TK2MSFTNGP11.phx.gbl...
hello, I have this function below which is simple and easy to understand

private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do
better. is there a more "professional" way or "elegant" way to do this?

Ed


Nov 21 '05 #13
Robin,
Interesting: I find the original multi-line If-Then-Else to be more readable
then the select case. As I don't see the select case is buying you anything
more (then the multi-line If-Then-Else).

If at a later date I needed to add "conditions" to the If-Then-Else I would
either use a ElseIf or change it to a Select Case at that time... FWIW:
Changing it to a Select Case is the Substitute Algorithm refactoring:
http://www.refactoring.com/catalog/s...Algorithm.html

I agree rarely in C++ would I use the ternary operator (a!=0?b:c )

I also agree on the comments about performance, as I only worry about
performance when that routine was proven to be a performance problem via
profiling.

Just a thought
Jay

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:ck*******************@news.demon.co.uk...
I like this one the best. We often used to have geeky debates over
a!=0?b:c in C++. The conclusion was that if it doesn't buy you anything
(in terms of performance in a performance critical piece of code) then
prefer the more readable solution, which is not neccessarily the one with
greatest brevity.


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eB**************@TK2MSFTNGP15.phx.gbl...
Edward,
<me too> :-)
I just wanted to add you can also use Select Case:

Private Function GetListHeight(ByVal userScreenHeight As Integer) As
Integer
Select Case userScreenHeight
Case Is < 1024
Return 30
Case Else
Return 50
End Select
End Function

</me too>

I've been at shops that prefer Select Case over If, although I really
don't see what it buys you in the above...

I could see using Select Case if you had multiple return values.

Select Case userScreenHeight
Case Is < 0
Throw New ArgumentOutOfRangeException("UserScreenHeight",
userScreenHeight, "User Screen Height cannot be Negative.")
Case Is < 800
Return 25
Case Is < 1024
Return 30
Case Else
Return 50
End Select

Of course you can do variations on the above...

Just a thought
Jay

"Edward W." <ed******@aol.comXSPAMMERIHATEYOU> wrote in message
news:uP**************@TK2MSFTNGP11.phx.gbl...
hello, I have this function below which is simple and easy to understand

private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function

It is very functional but seems pretty lame to me, like I could do
better. is there a more "professional" way or "elegant" way to do this?

Ed



Nov 21 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve | last post by:
Hey, well I'm really pleased with myself for writing a little script today that changes variables according to a radio button. The thing is, I have this feeling part of my script could be a lot...
8
by: Will | last post by:
I just discovered Python and looked briefly at one of the tutorials for beginners... It looks a lot like the old Command line Basic... I'm sure it does much more but... 1 - Can you create...
6
by: Kamilche | last post by:
Is there a more elegant way to change the working directory of Python to the directory of the currently executing script, and add a folder called 'Shared' to the Python search path? This is what...
17
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
8
by: Braky Wacky | last post by:
Hello, I have an ASP.NET webpage that uses an instance of System.Web.UI.HtmlControls.HtmlInputFile for uploading files to our server. I came across the documentation at MSDN for upping the...
16
by: Ben Finney | last post by:
Howdy all, On dirtSimple.org, PJE wrote: "Why is Python "blessed" with so much reinvention? Because it's often cheaper to rewrite than to reuse. Python code is easy to write, but hard to...
4
by: Iain King | last post by:
When I loop over one list I use: for item in items: print item but often I want to loop through two lists at once, and I've been doing this like I would in any other language - creating an...
10
by: sherifffruitfly | last post by:
Hi all, This is how I'm currently getting Friday of last week. It strikes me as cumbersome. Is there a slicker/more elegant way? Thanks for any ideas, cdj
35
by: Bjoern Hoehrmann | last post by:
Hi, For a free software project, I had to write a routine that, given a Unicode scalar value U+0000 - U+10FFFF, returns an integer that holds the UTF-8 encoded form of it, for example, U+00F6...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.