![]() |
Clean Coding - Common Sense
Hello everyone. I'm Chris Zakuto, I script a ton in GS2. Whenever I go onto another server, I find the owners begging me to fix something, so I go in to fix it, and oh no! I see messy code, which happens to give me a headache.
I'll explain how to make code easier to read by yourself, and easier to read and edit by others aswell. Part I -- Spaces This is one of the simplest things to do, and it's very easy to get used to. Just add spaces! For example, let's say we're setting a variable. PHP Code:
PHP Code:
Very simple and very easy to do. Try it out in your next script and see how much better it looks. Another thing I do is put spaces before and after parenthesis "(" and ")". Check these out. PHP Code:
PHP Code:
Part II - Separate Lines No, this actually makes your script look better. It's not just some way to increase line count (which doesn't actually matter at all...) For each brace "{" and "}" give it it's own line. This looks much better, and is a huge step closer to clean coding. For example PHP Code:
PHP Code:
Another thing I hate is an event that has a single line of code and has no brackets. PHP Code:
PHP Code:
Now that you have everything else down, this is probably the most important part, and all it takes is the tab button every once in a while. Take a look at this code. PHP Code:
PHP Code:
Part IV - Naming Functions This is really just something that makes your code look nicer. I've used several examples of functions here, such as dosomethingelse(), godosomething(), and milkacow(). These are a little ugly, but are incredibly easy to fix. What you do is take the first word of a function (I'll use the function godosomethingelse()), and then add the rest of the words with their first capitalized. For example, that godosomethingelse() can be split into: go do something else now change it to this go Do Something Else then put them together and you have goDoSomethingElse(); Much nicer. Hope you all enjoyed this guide. Leave comments as you wish, but please don't flame. Chris Zakuto |
Pretty much reinforcing some of what I've written in http://forums.graalonline.com/forums...ad.php?t=61805, there are a few more guidelines that I have previously written there to help keep code clean too.
Nice work, though. |
Quote:
|
The only problem I have with adding spacing just for personal looks is the fact I hate adding all that width and having to scroll horizontally.
|
Quote:
PHP Code:
PHP Code:
|
Ya, I do it mainly with arrays, but I hate doing it with if statements.
|
Real coders just do it however they like it then run their code through a beautifier when they are done ;)
see emacs/vim |
Umm what?
PHP Code:
PHP Code:
also, you space too much, you can use spacing to help visualize order of operations. and padding the conditional statements is a waste. PHP Code:
PHP Code:
|
Quote:
Yeah, Hell, it really is based all off preference. For me it's more of a mood thing, sometimes I think padding looks nice, but no padding is more compact and a bit cleaner. |
Style like a legend:
PHP Code:
|
Quote:
|
I prefer my way.
PHP Code:
|
Lol that's a mix of styles >_<
|
I follow these two simple things:
* New line for bracket ONLY at new functions * Spacing in variables (such as: var = pie + (1*4)), and in function paramaters (such as blah(p1, p2, p3)) |
Quote:
PHP Code:
|
Quote:
|
Quote:
PHP Code:
Quote:
|
Quote:
|
Quote:
legibility ++ :) |
Bleh, I code like this:
PHP Code:
|
Heres a sample of my coding, I'm extremely picky about the formatting.
PHP Code:
|
Quote:
|
A general guideline is to keep the formatting as similar to the rest of the project as possible. It might be a good idea to write a general guideline that all developers abide to.
Here's how the Angband source code is styled: PHP Code:
|
Quote:
Note how I use temp variables. It is something I began doing only recently and it makes the script look much more organized rather than using temp. repeatedly before all the temp variable names. |
Quote:
|
What does
PHP Code:
|
Quote:
When you have variables defined in function parameters they're declared as temp variables and you're able to reference them with or without the temp. before the variable name, this is the same idea. I place all the temp declarations at the beginning so I know what all I'm using and can make notes for other people using it. |
Quote:
|
Quote:
PHP Code:
The best idler is the (Server): TEST: The best idler is the (Server): TEST2:2 note that temp.hello is still null. The only way to drop the temp is if the var was passed as a paremeter, it will take presedence over an already global var. On another note, ziro, grey, and inver all have good styling imho |
Quote:
PHP Code:
HTML Code:
onCreated(): i = 34 temp.i= |
Ah, no wonder I had that array problem that time, oh well, it can be easily be fixed.
PHP Code:
Edit: It doesn't look pretty anymore though :cry: |
Quote:
|
Uh, I do this for the simple fact that I'm damn tired of typing temp. before every variable name, no other reason. I also like it better without temp., its more pretty like that. Also shortens the code in the long run.
|
Quote:
EDIT: Ding Ding Ding! |
Quote:
I try to write my scripts for people who have to work with them as well as for myself. |
Quote:
Edit: I would like it if un-prefixed variables defaulted to temp. rather than a global if no previous temp. or this. variables were found. |
Quote:
There's more to styling than whether it looks pretty or not; it's about catering for other people who may wish to edit the script or read it. It's for that reason that we prefer people to style scripts before posting asking for help with them here. |
You have a good point. :D
Though on Aeon, Napo and myself are the only major scripters and hes easily skilled enough to not be confused in any way. Though I suppose when I release some of my stuff to public I will restyle it... However until then nobody needs to be lookin' at my stuff :P /sign for un-prefixed variables going temp and not global. |
Clean coding looks prettier anyway ;)
|
I recently have been practicing better organization and clean coding in my scripts, heres a snippet.
PHP Code:
|
| All times are GMT +2. The time now is 08:56 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.