Forum Moderators: coopster

Message Too Old, No Replies

can i run lines into each other?

         

al1911

5:12 pm on Feb 3, 2005 (gmt 0)

10+ Year Member



what i mean is, rather than doing this...

if (this == that) {
do this;
then do this;
then do this;
}

...can i just do this...

if (this == that) { do this; then do this; then do this; }

Timotheos

8:39 pm on Feb 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Functionally it should work. Why not just give it try? What suffers though is readability.

ergophobe

7:23 pm on Feb 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Keep in mind that you aren't sending this stuff out over the net, so a few extra bytes for readability are worth it. It has a negligible effect.

You would free up more memory by eliminating one large array, for example.

jatar_k

8:24 pm on Feb 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yes but eventually you will kick yourself for using that formatting

if you have 30K lines of code and there are 4 to 10 individual lines per numbered line in the file and then you get an error on line 2456.

well, is it one of the 10 lines on that line or one of the 7 on any of the 5 lines above that?

debugging will become very time consuming.

Timotheos

8:42 pm on Feb 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good point jatar_k. The worst would be forgetting a ; and then trying to find it in one long line. Woo boy.

ergophobe

11:01 pm on Feb 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I hadn't even thought of the debugging aspect either.

In my case, it's mostly just a matter of not wanting to go blind!

I have even mostly switched to the ultra space-inefficient "Allman" style braces instead of "K&R" style, as in

if (cond)
{
}

instead of

if (cond) {
}

On indenting/braces and the various styles

[cs.bris.ac.uk...] (search on Allman)

According to the Wikkipedia [wkonline.com], Allman and Whitesmith are most popular (it ran overwhelmingly Allman when Dan Fernandez did a bit on it on his blog).

Why? Because with consistent tabbing it becomes so much easier to make sure you have you nesting right and I just find it a lot more readable, though I can see why K&R didn't use it back in the days of punch cards and tiny monitors.

Timotheos

8:53 am on Feb 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting. I was just reviewing the PEAR coding standards [pear.php.net] and they're using K&R. From what I can tell many PHP frameworks/projects are following these standards.

I've been using the unique Timotheos style

if (whatever) { 
echo "this";
}
else {
echo "that";
}

but now that you've got me thinking I'm considering following the PEAR standards.

Tim

grandpa

1:38 pm on Feb 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



From resource listed in msg# 6:

No mixed code and comments
n++; // increment x

This one nailed me recently. I was using brackets on a single line if statement. Later, cleaning up the code, I pulled the statement all together, so I had:
if ($a) { $b // comment }

A few hours later I was cured of mixed code and comments.

al1911

2:35 am on Feb 10, 2005 (gmt 0)

10+ Year Member



thanks everyone for the replies
i think it very interesting how there are different known ways of formatting the code
tell me though, do programs that automatically generate php code for you, like dreamweaver, all use the same type of formatting?

personally, i have found for simple line it's easier for me to read one line...
like...

if ( this == that ) { echo "something"; }

but if the code contains lots more functions, attributes and such, it would be obvious that it would be easier to format the code with indenting. what i think is most important is that the designers chooses a formatting that works for him/her and uses it all the time. when working in a team, you may need to choose a formatting structure that others can easily read too.

in any case, my original question has been answered
thanks again :)

jatar_k

6:28 am on Feb 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'll give you another little tip

if your if only contains a single line then inline is acceptable, you also don't need braces

if ( this == that ) echo "something";

also the same for else

if ( this == that ) echo "something";
else echo 'something else';

;)