Jump to content

which one is better


BiSolpos

Recommended Posts

Hello

Which one is better?

Are they different?

 

addEventHandler("onClientGUIClick", window,
	function (button, state)
		if button ~= "left" then return end

		if source == btnClose then
			closeMenu()
		elseif source == btnChange then
			local AmountKey = guiGetText(EditKey)
			if AmountKey == "" then
				return outputChatBox("error", 255, 25, 25, true)
			end
			if string.len(AmountKey) ~= 16 then
				return outputChatBox("error", 255, 25, 25, true)
			end
      		closeMenu()
      		outputChatBox("successful", 255,126,0, true)
		end
	end)

 

 

 

addEventHandler("onClientGUIClick", window,
	function (button, state)
		if button ~= "left" then return end

		if source == btnClose then
			closeMenu()
		elseif source == btnChange then
			local AmountKey = guiGetText(EditKey)
			if AmountKey == "" then
				outputChatBox("error", 255, 25, 25, true)
			else
				if string.len(AmountKey) ~= 16 then
					outputChatBox("error", 255, 25, 25, true)
				else
					closeMenu()
      				outputChatBox("successful", 255,126,0, true)
				end
			end
		end
	end)

 

 

Thank you 

Link to comment
Hello
 
I would say it depends on preference, I personnaly like the second way (using return). It makes not too many scopes and looks to me a good way to dissociate clearly validation of received arguments before executing the function. 
 
Some tips / observations about your code :
 
1 _ To check string length you can directly use # ( instead of string.len() ) for example
if (not #string > 0) then


2 _ Looks like a tool to give money. If you try to check money range, why don't you convert the string to a int/number ( using tonumber() ) and then doing necessary checks (if you need to check minimum / maximum range).

3 _ I don't understand string.len(AmountKey) ~= 16 which means if your string has not 16 char it return the error.

Edited by Mkl
  • Like 1
Link to comment
  • Moderators
5 hours ago, BiSolpos said:

Are they different?

A different technique: Guard Clauses

You might want to read this, to get a better understanding of the technique.

The result is more or less the same, except there are some differences with the returned values. In your function it doesn't matter, but there are functions where it does matter a lot.

 

  • Like 1
Link to comment

Definitely first one. I would format it like this though, with the start of the function on the same line. It's the common way of writing it because you'll have less indentation.

addEventHandler("onClientGUIClick", window, function(button, state)
    if button ~= "left" then return end

    if source == btnClose then
        return closeMenu()
    end

    if source == btnChange then
        local AmountKey = guiGetText(EditKey)

        if AmountKey == "" then
            return outputChatBox("error", 255, 25, 25, true)
        end

        if string.len(AmountKey) ~= 16 then
            return outputChatBox("error", 255, 25, 25, true)
        end
                
        outputChatBox("successful", 255,126,0, true)
        closeMenu()
    end
end)

Good luck!

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...