Forums

Unfortunately no one can be told what FluxBB is - you have to see it for yourself.

You are not logged in.

#1 2015-04-14 11:51:02

Sxderp
Member
Registered: 2012-11-02
Posts: 101

One-line conditionals

chris98 wrote:

Alternatively, if it's just a single line of code that you don't want something like this is better for readability:

if ($pun_user['is_guest'])
       //replace links with guest portion
else
       //do normal link replacement

While this is perfectly valid, I don't agree with the convention. I prefer to have braces even if it's a one liner. I don't know if there's a performance difference but I prefer the uniform look of my code.

Offline

#2 2015-04-14 12:48:11

chris98
Member
From: England, United Kingdom
Registered: 2013-05-31
Posts: 1,292
Website

Re: One-line conditionals

There's no performance difference, but in my opinion, it hurts readability and it does adds excess code to the file.

Offline

#3 2015-04-14 14:04:17

GWR
Member
From: Germany
Registered: 2010-08-06
Posts: 214

Re: One-line conditionals

As we already went offtopic: I had the code formatted without braces first... but appended them to make it more clear for "beginners".

I prefer non-brace-ifs for simple things (especially when its compagnon "else" is not in range :-) ).


bye
Ron

Offline

#4 2015-04-14 15:48:12

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 6,674
Website

Re: One-line conditionals

Let's ignore for a moment that FluxBB omits braces for these one-liners... wink

You should really add them.
They are a simple measure to avoid forgetting them when adding another line to the conditional block. This is a common source of errors and by adding braces you make sure you can't encounter them.


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#5 2015-04-14 18:33:43

adaur
Developer
From: France
Registered: 2010-01-07
Posts: 843
Website

Re: One-line conditionals

@Franz: I'm learning a bit of Python at my school, and the most disturbing thing at the beginning was the absence of braces... but in the end, it has forced to produce cleaner code - which isn't that bad wink


FeatherBB - A simple and lightweight new generation forum system
Based on FluxBB, written in PHP, using Slim Framework for a proper OOP-MVC architecture.

Offline

#6 2015-04-14 20:25:53

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 6,674
Website

Re: One-line conditionals

Yeah, I can see that wink

The good thing with Python is that whitespace matters, so the type of errors that I was talking about cannot happen...


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#7 2015-04-15 08:21:33

GWR
Member
From: Germany
Registered: 2010-08-06
Posts: 214

Re: One-line conditionals

That is the exact thing I absolutely do not like with "python".

I tend to have "tabs" instead of "2|4|6| spaces". So every developer could adjust its visual intendation easily in their editor of choice ("tabwidth"). Now I get python files to fix (Kodi/XBMC-Addons) and voila... everything is using spaces, while I just [tab]-intend and get errors then).

Takes hour to remember about "kind of indention"-settings in Geany tongue.

Dunno why "if" needs a ":" in python ... if "indention" should indicate what comes in this if-clause.


Edit:
@Franz
Feel free to train your "split posts into a new topic"-skills ;-).

bye
Ron

Last edited by GWR (2015-04-15 08:22:17)

Offline

#8 2015-04-15 11:27:31

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 6,674
Website

Re: One-line conditionals

Done. smile


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#9 2015-04-15 13:07:25

Sxderp
Member
Registered: 2012-11-02
Posts: 101

Re: One-line conditionals

The : in Python might just indicate that the conditions are over, as you don't need parenthesis. I dunno, lol.

Anyway, I don't I particularly like languages that don't have a definitive end symbol for conditionals (or anything that should be 'blocked.' I find it difficult to read as sometimes when glancing over the code you might think something is in the conditional but it's just on the line under it.

---
On tre subject of weird syntax for conditionals I find bash scripts convoluted.

if [ expr ]
then
    # stuff
fi

Then is considered a separate statement and requires a new line. Or a semicolon after the conditions.

Offline

Board footer

Powered by FluxBB