Tips on becoming a good coder?

Okay so I have been doing GLua for a while now. I feel fairly confident with it but do you guys have any tips for me to become better. Like ways to make things more efficient of cut down on net usage etc

if you have to use something more than once, stick it in a variable
avoid creating new tables wherever possible (avoid creating new anything wherever possible)
never use net.WriteTable

Do things on an event basis. That is a good way to keep things clean. Comment your code. For example

Don’t do exactly what I do (since it’s based on style) if you don’t like that, but it lets people see what it’s supposed to do, etc.

And just keep trying to optimize. Whatever you create, there’s usually a better way.

I Do that anyway but thanks for the other, Although I have never had to send a table with net library, but say I did how else would I do it if I could not send it?

Start using something like C#. You will pick up structure, OOP and reusable code way faster than with lua.

The common mistakes:

  • Never use ply:SendLua().
  • Never use RunString().
  • Never trust the client. Always check the data send from the client.
  • Never use Material and surface.CreateFont inside a function.
  • Never use functions inside functions (Unless it’s vgui).
  • Never override gamemode functions (Unless you’re making a gamemode).
  • Never make backdoors. The community will lose its trust … along with your reputation.

Think logically.

I like Chessnut’s. It’s simple, to the point and if you can’t do it then… well… sorry.

A way that I learned things was just let your creativity flow and don’t be afraid to experiment with things.

Become one with the syntax.

I’ve written quite a lot of tutorials so far, feel free to add me on Steam. Also I have a few guides I’m starting to write for some of the most common / small mistakes which cause big problems and some of them have already been noted in this thread…

Generalized Optimization Guide:
https://dl.dropboxusercontent.com/u/26074909/tutoring/optimization/_generalized_optimization_guide.lua.html

I’d also recommend some utilities ( not that they’ll make you a “good” coder, but it helps when things are organized )… Take a look at AutoSizer, Desktops, Notepad++ ( and the upgrades page ), Process Explorer… Useful Programs ( SteamCMD, Autosizer, Desktops, Process Explorer ) and Notepad++ Upgrades
https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_useful_programs_and_notepadpp_upgrades.lua.html

And here are some helpful topics:
Generalized Lua Help ( Links to Wikis, Answers the question of “Where do I post a simple question or DarkRP Specific question”, links to other resources compiled by forum members )
https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_general_lua_learning.lua.html

RunString can be used, but with warning. I use it only server side in functions that are working only when the GameMode decides it. Just to set values on special tables (Jobs Enum and Library System).

I swear to god you’re a robot, or a spambot.

Edit:

Be bop beep

When I said he was a robot, I was talking about how he works and how it produces great results that I can’t understand as a mere human - I wasn’t saying it as an insult. I sincerely hope people don’t pick this up as the new inside joke.

More general, but still completely related.

Don’t stick with Lua. Lua is a tiny fraction of what you should learn to become a better programmer.

Specializing in a specific language or tool-set isn’t a bad thing, but it only gives you one perspective to an entire dimension.

Expand to and familiarize yourself with different paradigms and languages.
Work with others.
Challenge yourself.
Think logically, but also use abstract reasoning and don’t be afraid to experiment.
Understand the systems and operations beneath the languages and tool-sets.

Read. Read books written by both great programmers and authors of existing languages.
Read articles and blog posts.
Study code. Both good and bad. Really try to learn what makes them good or bad.

Ultimately, a great way to learn how to be a good programmer, is to learn what makes one a bad programmer.

Mastering the art of Programming is a long, long road. Just get in your car, fuel up, and start driving.
You will have to take detours and pit stops to refuel, but you will get there eventually.

Write out exactly what you want to do in English, then start replacing your English with code and looking up hooks.

“When player right-clicks with my weapon, I want the mouse sensitivity to slow down to half the normal speed.”

“When self.Owner:KeyDown( IN_ATTACK2 ), I want self:AdjustMouseSensitivity( .5 )”


if self.Owner:KeyDown( IN_ATTACK2 ) then
    self:AdjustMouseSensivity( .5 )
else
    self:AdjustMouseSensitivity( 1 )
end

(I actually don’t know if this works, this is just off the top of my head)

Learn the basic mistakes, learn from it and try to avoid them.

Ahem, what? So you say that stuff below is very bad coding, right?



function adv.DataMat(mat, opts)
    -- OH NOES! Material() call in a function!
    return Material(adv.StrFormat{"../data/$1
.png", mat}, opts)
end




function SF.WrapFunction(func, instance)
	if wrappedfunctions[func] then return wrappedfunctions[func] end
	
	local function returned_func(...)  -- OH NOES! Function inside function!
		return SF.Unsanitize(instance:runFunction(func, SF.Sanitize(...)))
	end
	wrappedfunctions[func] = returned_func
	wrappedfunctions2instance[returned_func] = instance
	
	return returned_func
end


I think he meant don’t call surface.CreateFont in loop otherwise you will possibly cause a memory leak.
But about the Material, i don’t think it’s realy bad to call it in loop. In all cases you need to call it in loop to create animated materials with render.something.

It’s okay to call both in a loop if you need to create multiple fonts/materials with similar paramters. But it is NOT okay to call either one in a loop or hook with same input, it’ll just leak memory with copies.