![]() |
Curly brackets!
Hi all
I have just completed my first useful script (not completely functional yet but that's for another thread!) and I am boggled by the number of curly brackets i have ended up with lol! I basically had to go through using comments to number them and check they were all opened and closed correctly. Funds do not allow the purchase of coda yet (which i presume has a clever gismo to prompt me on this) so I wondered what basic methods you more experienced coders use to keep track of these slippery little blighters! Thanks :) |
Ok Iknow - double posting but found something I didn't know about. Smultron (open source text editor for macs) does do something just never realised!
When you make a bracket (square, curly, round) it flashes the coressponding one, of beeps if it can't find one. Yey! Would still ike to hear tips tho as I don't always hear the beep! :) |
You can try to do things short handed, so if you have a quick conditional, you don't have to use brackets like so:
if($a) { echo "hello"; } That's 2 brackets you have to worry about. But you can just do this: if($a) echo "hello"; |
Would this work if i have lots of if elses inside a main if else?
|
Quote:
if($var) echo "Hello"; elseif ($var == "something") echo "Hello 2"; else echo "Hello 3"; Just don't get too carried away I guess.... If you really have a lot of conditionals, it might be faster to use switch function..... |
Whilst removing curly braces where they're not strictly necessary might seem like a good idea, in practice it's all too easy run into problems as a result. I guess my most practical advice would be to use good indentation in your code.
For example: PHP Code:
PHP Code:
|
Yeah i'm thinking i might try the switch function. Thanks for that both :)
Ya never know i might post some code later... a bit too ashamed of my teensy weensy effort to show it yet lol :D |
Quote:
Just do yourself some justice and try to code as much as possible and then ask for help unless you have a "theoretical" question about how something works... I think what irks a lot of people here and on other message boards are people who don't even bother to try and they just want someone to provide code for them... |
Quote:
Always indent your code, without indents code is half impossible to read. That should take care of half your problems right there. |
Quote:
I just feel, if I need to check something, and echo it, I'd rather save the trouble of using brackets, but if I have to use multiple else/if statements, then I would use brackets... Here's an example if I want to do a quick exit... $a = 10; if($a == 10) exit; |
Salathe said it right when he said don't indent too deeply. I always avoid that whenever I can. I dislike even having an
if within an if; sometimes it's unavoidable. Nor do I like adding else statements, again, sometimes it cannot be helped!For instance, to give a simple example, instead of adding an else for a return statement, I would aim to return that within the if statement, and knowing that if, as below, it did evaluate to true then it wouldn't reach the second return, and thus saving us an else.php Code:
In addendum to this, I also like to make good use of continue in loops. For example:php Code:
That also saves us an else, and makes our code, at least in my eyes, much easier to read.I don't know if I can explain this awfully well, but I like to exclude all the items we don't want in the subsequent code first, instead of deciding what we need to include, and then placing an else at the bottom to handle the exclusions.This is the way I wouldn't do it (I wish to save the good way for the end so your mind retains it!) php Code:
Last but not least, the way I would do it, as in the first two examples, is like so: php Code:
In simple form it could be shown as the following: (I found this UML diagram creator yesterday via Reddit. Seems quite useful! Seems it can also be used for lovely flow charts.) The underlining assumption behind this way of working is that the chunk of code required for processing exclusions will be smaller than the code for processing inclusions. Inclusions being the items we want to work with. This saves us having huge chunks of code for processing inclusions, and then having lots of isolated else statements at the bottom handling exclusions.Hopefully this more real-life example will crystallise this in your mind: php Code:
|
wow WH:
You are quite thorough....you and Salathe... Anyways, do you think there are any performance benefits of including the inclusions first? Or is this just simply just a preferable method that utilizes "continue" which I normally don't use in my everyday tasks...but I can see how it is useful in your example.. Also, it's probably just preference with a mixture of good coding practice, but in your other example you have this: PHP Code:
I know how much you hate using "else"... PHP Code:
EDIT: I see you added more after I posted which kinda answers my question. Quote:
|
In your example, you really don't need that
else for the return because it's going to happen irrespective of anything else. In those examples where it either is or isn't, then check for an is, and the isn't will be the default.Think of it in binary terms, yes/no, true/false, 0/1. If you check for the yes (true, 1), then the no (false, 0) is the resulting value, and can be returned as such. It's when you introduce more complicated conditionals that it requires more thinking on your behalf, such as with hexadecimal (base-16), and even our base-10 counting system. Anything other than base-2 (binary), where we only have the possibility of 2 outcomes, which is fundamentally how computers work (and many more things, too), and so tends to be used quite commonly in everyday life, and to keep the topic relevant, in programming also. I don't think there is any such performance gain, it's personal preference for readability, really. |
Quote:
Thanks, so this would work.... PHP Code:
But I can see how that would look confusing so I wouldn't want to do it that way either.... I think for advanced programmers like yourselves, there becomes a thin line between preference and proper coding...although I tend to see more on the latter.... I've seen debates in the past on just where to place the brackets... ex: if() { or if() { Stuff like that when at the end of the day it's just preference... |
Blimey!
Well I need some chocolate now and a brain transplant. I'd hate to see your advanced section! Since we have veered a bit i will take this opportunity to ask a more specific question. Basically the if elses i was talking about would do this: Firstly check if all my variables (answers to questionnaire) were not set. If not set, prompt to fill in. Then progressing to validating some user info using some simple regexes (5 variables in total), then to print out a thankyou message and format an email to myself with all the inputed info. I have all the code.... just not sure how I am going to join it. If I use switch then can i ask first if the variables are set, then that each of the user info variables are valid then print thankyou and send mail? I don't think I can use foreach because they are diff regexes for each input. I really thought I had this but brain is mush now :( |
Quote:
I'll try to come up with a quick example unless someone comes up with one before I do.... |
Ok this is a really bad example but it does work somewhat and perhaps you can pick some stuff out of it. I personally am trying to code everything in OO now, but I just wanted to provide you with something.
Again, this is pretty bad...but it works...if anything, maybe you can learn not to do it this way...LOL... PHP Code:
|
1 Attachment(s)
This may seem quite complex and superfluous, and it probably is just that for an example. However, I've tried to keep it as modular as possible (No use of exceptions, classes, et cetera...) as I remember what I was like when I first began learning PHP. Didn't know a damn thing! :-)
The theory for the script is as follows:
php Code:
php Code:
I apologise, as it does look confusing to begin with, even to me! However, you'll begin to understand it, I am sure, after a few glances, and after a few questions. |
Quote:
That must have been so long ago.....hard for me to even imagine... :-P |
Ah ok i will have a play tomorrow. Thanks. Might post a link once i have fiddled some more. :) Thanks all for your eager responses :D
|
| All times are GMT. The time now is 10:39 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0