Raised This Month: $51 Target: $400
 12% 

Odd SourcePawn "feature"?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 03-12-2012 , 19:11   Odd SourcePawn "feature"?
Reply With Quote #1

In C++ I used to leave brackets off if the enclosed statement was only one line. E.g.

PHP Code:
if (== b)
    
DoSomething() 
In SourcePawn I was using the following lines:
PHP Code:
if (args == 0)
    if (
== b) {
        
Line1
        Line2
    
}
else {
    
Line3
    Line4

This is possible in C++ since the first if statement contains only one "block" (another if statement). However I get logical runtime errors using this, and must use brackets for the first if statement too. Why does SourcePawn do this, and not copy C++'s implementation?

Last edited by 11530; 03-12-2012 at 19:13.
11530 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-12-2012 , 20:12   Re: Odd SourcePawn "feature"?
Reply With Quote #2

Quote:
Originally Posted by 11530 View Post
This is possible in C++ since the first if statement contains only one "block" (another if statement). However I get logical runtime errors using this, and must use brackets for the first if statement too. Why does SourcePawn do this, and not copy C++'s implementation?
Er... because Pawn isn't C++?

It is a bit odd that the error occurs at runtime, but not at compile-time, though.

I didn't find any reference to this gotcha in the Pitfalls section of the Pawn Language Guide (PDF) though.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 03-12-2012 at 20:13.
Powerlord is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 03-12-2012 , 22:20   Re: Odd SourcePawn "feature"?
Reply With Quote #3

Well, Pawn is close enough to C/C++ that I thought it would in this simple case.

Edit: Found the relevant section in the guide, thanks Powerlord:
Quote:
When if statements are nested and else clauses are present, a given
else is associated with the closest preceding if statement in the
same block

Last edited by 11530; 03-12-2012 at 22:35.
11530 is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 03-13-2012 , 09:13   Re: Odd SourcePawn "feature"?
Reply With Quote #4

8 years of active time as developer have showed me that you shouldn't not use brackets.
There is no real advantage in not putting them.

It makes your code really unreadable and when you add more than one line of code you will have to add them afterwards anyway.

Only beginners write conditional statements without the brackets.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Lord Canistra
Senior Member
Join Date: Mar 2009
Location: Tallinn, Estonia
Old 03-13-2012 , 10:10   Re: Odd SourcePawn "feature"?
Reply With Quote #5

Nah, real beginners always write brackets since they don't know those aren't always required.
__________________
Lord Canistra is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 03-13-2012 , 10:25   Re: Odd SourcePawn "feature"?
Reply With Quote #6

Quote:
Originally Posted by Lord Canistra View Post
Nah, real beginners always write brackets since they don't know those aren't always required.
No, beginners usually copy code from other people (especially in Sourcepawn) and so they take over the bad coding styles of others.

Please do yourself a favor and use brackets
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0

Last edited by berni; 03-13-2012 at 10:26.
berni is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-13-2012 , 10:48   Re: Odd SourcePawn "feature"?
Reply With Quote #7

Quote:
Originally Posted by berni View Post
Please do yourself a favor and use brackets
It's even part of the official code style!
http://wiki.alliedmods.net/Contribut...d#Coding_Style
__________________
asherkin is offline
napalm00
Veteran Member
Join Date: Jun 2011
Location: Italy, sadly
Old 03-13-2012 , 10:54   Re: Odd SourcePawn "feature"?
Reply With Quote #8

Using brackets is a good habit: in case you need/want to add something else after the statement you won't have to add them since you already have them placed.
__________________
napalm00 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-13-2012 , 11:24   Re: Odd SourcePawn "feature"?
Reply With Quote #9

Quote:
Originally Posted by napalm00 View Post
Using brackets is a good habit: in case you need/want to add something else after the statement you won't have to add them since you already have them placed.
I've gotten burned by this at my day job... one of the other developers doesn't use brackets when he just has single line bodies to if statements, but I changed one from a single line to multiple line and then had to figure out after deploying it to test why it broke.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Lord Canistra
Senior Member
Join Date: Mar 2009
Location: Tallinn, Estonia
Old 03-13-2012 , 12:26   Re: Odd SourcePawn "feature"?
Reply With Quote #10

Shame on me, but I'm obsessed with _removing_ braces around one-statement if/else/for/etc bodies. There's some weird urge for me to save extra couple of lines on these.
;_;
__________________
Lord Canistra is offline
Reply



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 05:16.


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