Android Question how to elegantly compare if a passed value is bigger than a minimum value?

Cableguy

Expert
Licensed User
Longtime User
Hi guys

when passing a value to a sub, how can I check in the target sub if the passed value meets a minimum requirement (int value). This seems a very basic question BUT i'm looking for the elegant solution that uses shortcircuit conditioning... which I still cant get my head around.
 

Cableguy

Expert
Licensed User
Longtime User
I remember seeing a thread where, without any if condition, a value was replaced by another if it was smaller than a minimum value.
so i'm searching a way to replace...

if value>500 then
x=value
else
x=500
end if

by a more elegant code...
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Thanks Luca, that exactly what I was looking for!!
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Thanks Luca, that exactly what I was looking for!!
B4X:
Sub greaterORless(X as int, Y as int) as Boolean
Dim returnvalue as Boolean
if X > Y then
returnvalue = True
else
returnvalue = False
End If

return returnvalue
End Sub

Like this?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
B4X:
Sub greaterORless(X as int, Y as int) as Boolean
Dim returnvalue as Boolean
if X > Y then
returnvalue = True
else
returnvalue = False
End If

return returnvalue
End Sub

Like this?

Actually no. I'm building a custom view class, and I want to be able to limit the smallest size of my view. So wen I call myview.width=400 it will reset to 500.
Luka code was exactly what I remembered to have seen before, but just didn't know where to look for it.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
if greaterORless(x,y) Then...

can be simply write:

if x > y Then ...

:)
Yes, but he said Elegant way of doing it, as you said it yourself, "simply" is not the same as elegantly!

:D
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
So you mean if I have to do some calculations I have to write:

B4X:
Public Sum(x,y) As Int
    Dim Result As Int
    Result = x + y
    Return Result
End Sub

Dim Tot As Int = Sum(5,2)

instead of:
Dim Tot As Int = 5 + 2

?

:D
Hahaha funny, no but you're totally missing the point, read his post, he said "elegantly" i know a lot of people can code, but very few people can code with style and elegantly, most people just write spaghetti code.

:p
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
BTW I do not like spaghetti, they are uncomfortable; I prefer "pennette"

upload_2015-9-3_19-49-2.png



:)
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
hummm...Penne.... and some cannelloni!!!

OK, back to MY point: code elegancy

what is more elegant in these lines of code?

Private Dim A as int
A = 3

Versus

Private A=3 as int

or even

Private A as int: A=3
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
no but you're totally missing the point, read his post, he said "elegantly"

Probably you've read only his first post, in which he wrote of wanting to "check"... not to get a value.

Anyway, you answered too quickly, then writing nonsense (this happens to me often and I hope this happens to everyone :)).

Write a routine like yours, greaterORless, instead of writing directly: If x > y Then

is exactly the same as writing my routine Sum instead of writing directly: Dim Tot As Int = 5 + 2

Identical.

No lack of elegance, let alone of "spaghetti programming".


And if you are not able to admit that you are human and sometimes you're wrong... you can write that someone can only do "spaghetti programming".
:)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I use the above aswell. or const if it's fixed.

for me elegance would be the max operator or a plain if/then aswell.

I guess there's a difference between elegance and structured.
 
Upvote 0
Top