inherit
50924
0
Feb 28, 2021 14:59:32 GMT -8
BFD
Last place must get awful crowded at the Nice Guy Olympics
1,708
July 2005
dwinman164
|
Post by BFD on Dec 3, 2009 9:44:39 GMT -8
Is it really more efficient? Or is it an assumption based on writing "less code"? If I remember rightly, the way it's parsed is different to the standard if statement. It's interesting when you combine 3 or 4 ternary operators! ^_^ It might buy you some performance gain in an interpreted language, but when optomized by any decent compiler, I am confident that both techniques result in the same or functionally identical underlying code...and would bet signficant cash that the performance would be the same. It uses less variables, therefore it uses less memory, therefore it's more efficient. For example: $test = 'win'; if ($test == 'win') $output = 'you rock'; else $output = 'you suck'; echo $output; This could be simplified to: $test = 'win'; echo ($test == 'win' ? 'you rock' : 'you suck'); $output never has to be defined, so it takes up no memory at all (except for $test). If you write the if statement your way, then maybe. But you could just as easily write: $test = 'win'; if ($test == 'win') echo 'you rock'; else echo 'you suck'; My point is that a language construct is not as likely to buy you performance as a good algorithm and only using needed variables(unless you are using immutable types...) As for actual coding style, I try to separate as much as possible while not introducing excessive line breaks. The goal is to make it readable, but keep to one page on the screen as much as possible (this was actually a requirement in most of my computer science classes, especially the ones that weren't just language learning courses). One of the things that drives me nuts is when people are inconsistent in how they organize their code...for instance, if you declare your variables at the beginning in one function, don't define them inline in the next. I also despise comments at the end of a line of code, especially a long one. If it is important enough to comment, it is important enough to guarantee someone reading the code knows it is there.
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Dec 3, 2009 11:27:07 GMT -8
As for actual coding style, I try to separate as much as possible while not introducing excessive line breaks. The goal is to make it readable, but keep to one page on the screen as much as possible (this was actually a requirement in most of my computer science classes, especially the ones that weren't just language learning courses). This is exactly what I go for and what my professor wants us to do. I'm guessing you've already graduated?
|
|
inherit
50924
0
Feb 28, 2021 14:59:32 GMT -8
BFD
Last place must get awful crowded at the Nice Guy Olympics
1,708
July 2005
dwinman164
|
Post by BFD on Dec 3, 2009 11:42:48 GMT -8
As for actual coding style, I try to separate as much as possible while not introducing excessive line breaks. The goal is to make it readable, but keep to one page on the screen as much as possible (this was actually a requirement in most of my computer science classes, especially the ones that weren't just language learning courses). This is exactly what I go for and what my professor wants us to do. I'm guessing you've already graduated? Almost 16 years ago. I have been a software developer/DBA/Consultant for over 15 years.
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Dec 3, 2009 19:05:52 GMT -8
It was a off-the-top-of-my-head code. It could obviously been written more efficiently. That wasn't the point of it at all. -_-
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Dec 3, 2009 19:56:39 GMT -8
This is exactly what I go for and what my professor wants us to do. I'm guessing you've already graduated? Almost 16 years ago. I have been a software developer/DBA/Consultant for over 15 years. Very nice. The only other professionally employed software developer I've known about on PBS other than the administrators was Peter, and I think he mainly did website development.
|
|
inherit
128590
0
Aug 25, 2012 20:46:48 GMT -8
Nick (Goodbye!)
AMF!
1,813
July 2008
nickos
|
Post by Nick (Goodbye!) on Dec 3, 2009 20:05:59 GMT -8
I use the first or second option usually.
|
|
inherit
50924
0
Feb 28, 2021 14:59:32 GMT -8
BFD
Last place must get awful crowded at the Nice Guy Olympics
1,708
July 2005
dwinman164
|
Post by BFD on Dec 4, 2009 8:43:45 GMT -8
It was a off-the-top-of-my-head code. It could obviously been written more efficiently. That wasn't the point of it at all. -_- It may not be your point, but it does illustrate a good one about altering coding style for perceived performance gains. More often than not, you can get the same performance benefit by rethinking the code and keep using a more easily understood construct as you would by implementing a language "trick" that might not be as clear. First and formost, performance is best obtained by good design and appropriate library selection. Also keep in mind that in a professional programming environment, code maintenance is not always done by programmers with expertise in the targetted language, and after an application is in production for a decent amount of time, almost never by the original programmers. While you may be able to save a few cpu cycles by leveraging nuances of particular language constructs, it isn't worth it if you wind up costing a fortune in extra maintenance costs. If you are ever foced to write code that uses new, advanced or obscure language features, you should comment extensively. In fact, one of my former coworkers (currently holds a phd in computer science) worked by the goal that code should be commented and styled to be understood by a beginner programmer, possibly one who did not even know the language. And that policy has paid off many times...especially when guiding an IT contact at a customer through correcting critical bugs.
|
|
inherit
12045
0
Nov 19, 2012 14:52:05 GMT -8
Renegade
As unique as mice pudding milkshake
40,557
August 2003
renegade
|
Post by Renegade on Dec 18, 2009 13:07:38 GMT -8
commenting code is something i always mean to get into the habit of, and then enver actualyl seem to do
|
|
inherit
123128
0
Feb 3, 2020 13:53:38 GMT -8
Malagrond
Remember, remember the 5th of November.
813
April 2008
malagrond
|
Post by Malagrond on Dec 19, 2009 11:14:28 GMT -8
My code always ends up looking like: for($var = 0;$var < 5;$var++){ if($var==1){ function(); } }
Rare is the day when I use tabs/spaces. I've recently started commenting, but it's really only for my benefit since the comments are unique phrases that allow me to Ctrl+F my code more efficiently ~Mala
|
|