Raised This Month: $115 Target: $400
 28% 

Writing good code, solving problems.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
I am inevitable
Member
Join Date: May 2019
Location: 0xA6DA34
Old 07-14-2019 , 11:04   Writing good code, solving problems.
Reply With Quote #1

Hi.

Reading code off the internet sometimes triggers me. I immediately start thinking: "hmm, why didn't you that, or that?". (I'm not trying to be that smartass or anything, and I'm by any means not the best, just wanted to make things a little clearer, okay, good).

Before we dive in, when writing something, always ask yourself these questions:
- What do I want to know?
- What do I already know?

Trust me, it's extremely helpful.

Here're a couple of dilemmas with examples, and more compact solutions:

1. A guy named Jerry wants a specific command to output the client's team. This is how he did it:
Spoiler


Now, this code is perfectly fine, but. We can shorten it... a lot:
Spoiler


2. This one day I was writing a stats plugin and there was this one command that was supposed to create a menu, showing you the top players on the server, based different categories. Here's how I first did it:
Spoiler


And this too is just fine, but, we can be even more efficient with our lines, and here's the solution I found:
Spoiler


I believe that programming isn't just about making things work, it's about making it nice aswell.

With that being said, you can almost always make your code just a little bit better.

(Also, I don't even know if this page is useful, but, meh).
__________________
I do make plugins upon requests, so hit me up on discord if you're interested: cucklord67xD#3823

Last edited by I am inevitable; 07-14-2019 at 13:43.
I am inevitable is offline
Cruze
Veteran Member
Join Date: May 2017
Old 07-14-2019 , 12:13   Re: Writing good code, solving problems.
Reply With Quote #2

Won't it be like this? Correct me if I'm wrong ._.
PHP Code:
PrintToChat(iClient"Yay, you're in the %sTerrorist team!"iTeam == "Counter-" NULL_STRING); 
__________________
Taking paid private requests! Add me on steam!

Discord: Cruze#4947 | AlliedModders Releases | Github | Steam | PayPal
Cruze is offline
I am inevitable
Member
Join Date: May 2019
Location: 0xA6DA34
Old 07-14-2019 , 12:18   Re: Writing good code, solving problems.
Reply With Quote #3

Quote:
Originally Posted by Cruze View Post
Won't it be like this? Correct me if I'm wrong ._.
PHP Code:
PrintToChat(iClient"Yay, you're in the %sTerrorist team!"iTeam == "Counter-" NULL_STRING); 
Woops, yeah
__________________
I do make plugins upon requests, so hit me up on discord if you're interested: cucklord67xD#3823
I am inevitable is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 07-14-2019 , 12:53   Re: Writing good code, solving problems.
Reply With Quote #4

NULL_STRING has an undefined value, so you can't depend on it being an empty string. Better use an empty string "" right away!
__________________
Peace-Maker is offline
I am inevitable
Member
Join Date: May 2019
Location: 0xA6DA34
Old 07-14-2019 , 13:43   Re: Writing good code, solving problems.
Reply With Quote #5

Quote:
Originally Posted by Peace-Maker View Post
NULL_STRING has an undefined value, so you can't depend on it being an empty string. Better use an empty string "" right away!
Okay cool, I didn't know, ty ^^
__________________
I do make plugins upon requests, so hit me up on discord if you're interested: cucklord67xD#3823
I am inevitable is offline
DarkDeviL
SourceMod Moderator
Join Date: Apr 2012
Old 07-15-2019 , 02:29   Re: Writing good code, solving problems.
Reply With Quote #6

Quote:
Originally Posted by I am inevitable View Post
Now, this code is perfectly fine, but. We can shorten it... a lot:
It would have been even 'shorter', if you didn't create a new "iTeam" variable, and simply used:

like in the first example.


The ternary operator:

PHP Code:
iTeam == "Counter-" "" 
isn't always as useful, such as for example if the code you want to act based on the result of the condition is spanning more than one line, in these situations the classic if-then-else conditions may be more appropriate (and make the code more readable in the end).
__________________
Mostly known as "DarkDeviL".

Dropbox FastDL: Public folder will no longer work after March 15, 2017!
For more info, see the [SRCDS Thread], or the [HLDS Thread]
DarkDeviL is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 07-17-2019 , 06:08   Re: Writing good code, solving problems.
Reply With Quote #7

Code:
PrintToChat(iClient, "Yay, you're in the %sTerrorist team!", iTeam == 3 ? "Counter-" : "");
That is perfectly fine up until you want localizations (check out the Russian localization of team names on CS:GO), or different phrases depending on the team. At the very least I'd rather keep the entire team name in the third parameter to avoid these sorts of phrase concatenations.

Additionally, Menu_Top_Menu_Handler is in poor form -- the third parameter is not always going to be a client index, per the MenuAction documentation. You've gated off MenuAction_End, where the parameter in question is the MenuEnd reason, and so you're now always leaking handles (because the values that you think are clients are either 0 or negative). That said, I'd also take that approach to simplifying the query setup.

As far as clear code goes, I'm a fan of "Avoid Else, Return Early" to keep indentation to a minimum, but that's mostly a matter of opinion and not a strict style to adhere to.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH)

Last edited by nosoop; 07-17-2019 at 08:50.
nosoop is online now
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:39.


Powered by vBulletin®
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Theme made by Freecode