Any tips on keeping code short

Im making a an motd menu with my own custom tabs since DProperty sheet styling is useless and at the moment i have 1 tab, some various styling for the menu and theres just so much code for derma. Is that fine or are there ways to shorten it? Ill post some images soon.

Use skins ( Panel:SetSkin( name ); ), you can use helper functions to create your elements which can reduce x number of lines to 1 if you want to, any thing that you find yourself to be repeating you should turn into its own vgui element and just create one of them ( look at things like DImageButton, etc in garrysmod/lua/vgui/*.lua and you’ll see how one element contains x elements to achieve one combined element ).

There can be many ways to shorten the code, but without seeing exactly what you did it’ll just be speculation.

If you find yourself repeating the same code over and over, make a function that does that.

As an example, there’s a built in function called Label, which accepts the text and the parent:



local x = vgui.Create( "DLabel", parent_panel )
x:SetText( text )

--vs

x = Label( text, parent_panel )


You can expand this to do interesting things, like making your buttons automatically:



function Button( text, parent, command, ... )
  local x = vgui.Create( "DButton", parent )
  x:SetText( text )
  x:SizeToContents( )
  
  if type( command ) == "string" then
    x.DoClick = function( ) RunConsoleCommand( command, ... ) end
  elseif type( command ) == "function" then
    x.DoClick = function( ) command( ... ) end
  end

  return x
end

--So

x = Button( "Ban, 15M", row_panel, "banid", 15, v:UserID( ), "kick" )
--or
x = Button( "Refresh", row_panel, UpdateRow, row_panel )


Things like that are only worth doing if they improve your workflow or significantly cut into the size of the code - don’t golf your work if the gains aren’t worth it.