Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
ephphatha
Dec 18, 2009




pigdog posted:

Looks like PHP to me... foreach (my $entry in $options) implies $options to be an array, but unlike in PHP the all $variables in Perl are scalars.

Probably because I hosed up writing the code out from memory. This is perl code, and the horror I was pointing out was the developers aversion to the modulo operation.

Adbot
ADBOT LOVES YOU

Progressive JPEG
Feb 19, 2003

Gazpacho posted:

Some people just take comfort in developing the hard way that they know rather than the easy way they could learn.
My most recent experience with this is eventually convincing a coworker to just use a loving regular expression.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I guess I don't quite understand why a template engine is necessary for that sort of quick-and-dirty script. Obviously, if it was a lot more than it already was, yeah, but otherwise, I find the code readable, the intention is clear, and I see no reason to make it use templates, yet.

Coffee Mugshot
Jun 26, 2010

by Lowtax

nielsm posted:

Odd, seeing that PHP itself is a templating engine, that has massively grown out of its original purpose.

(By the way that code you quoted is not PHP, it's Perl.)

Oops. That's what I get for knowing PHP and not Perl. I just thought the code tags always said "Perl code." I must be misremembering something.

The Gripper
Sep 14, 2004
i am winner

Suspicious Dish posted:

I guess I don't quite understand why a template engine is necessary for that sort of quick-and-dirty script. Obviously, if it was a lot more than it already was, yeah, but otherwise, I find the code readable, the intention is clear, and I see no reason to make it use templates, yet.
I pretty much don't use templating engines for that kind of thing unless I'm doing something where there's a good chance the layout will need changing in the future. Pretty-printing data for reference isn't one of those cases for the most part (which is where I'd expect to find a block of code like that).

Coffee Mugshot
Jun 26, 2010

by Lowtax
I'd usually agree with you both, but maybe I have a bias after using Flask/Jinja and Symphony/Twig for a lot of projects lately. Even for simple html pages, I've always found something like:

Python code:
render_template('home.html', options, divideby)
HTML code:
<table>
  {% for entry in options %}
    <td>a checkbox and label using values from {{ entry }}</td>
    {% if loopcount / divideby == 4 %}
      </tr>\n<tr>
      {% set divideby = divideby + 1 %}
    {% endif %}
  {% endfor %}
</table>
easier to read than:

Perl code:
$divideby = 1;
$count = 1;
foreach (my $entry in $options) {
    $html .= "<td>a checkbox and label using values from $entry</td>";
    if ($count / $divideby == 4) {
        $html .= "</tr>\n<tr>";
        $divideby++;
    }
    $count++;
}
It seems like a good way to further separate logic from presentation. I also feel that the $html variable is really distracting because you end up having to read a lot of code to find out what it actually is/does at the point it gets echo'd. With a template, I just tell that I'm going to write some html in a loop and there's nothing else there. There's no temptation to store everything inside of $html.

I'm not saying this is the one true way. I just think devs should be less averse to using it for code clarity. But then again, I've only been using them for the past few months, so maybe I'm the horror.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Rainbow Pony Deluxe posted:

HTML code:
<table>
  {% for entry in options %}
    <td>a checkbox and label using values from {{ entry }}</td>
    {% if loopcount / divideby == 4 %}
      </tr>\n<tr>
      {% set divideby = divideby + 1 %}
    {% endif %}
  {% endfor %}
</table>

You're kidding me, right?

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
That particular layout task involves common off-by-one gotchas that a novice is going to struggle with regardless of whether they use a template system. At best the use of a template system will make any bugs stand out better on review.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
code:
<table>
  {% for row in args %}
    <tr>
      {% for entry in row %}
        <td>render stuff using {{entry}}</td>
      {% endfor %}
    </tr>
  {% endfor}
</table>
code:
render_template("butts.tpl", array_chunk(data, 4));
(I have nfi what the actual syntax for that templating system is, so think of this as psuedocode).

If you want your template system to be obviously correct, this is more like how you'd do it.

Spades
Sep 18, 2011

Rainbow Pony Deluxe posted:

Templating stuff

While still not perfectly readable, it's possible to use PHP's alternative construct syntax to make something more template-ish:

PHP code:
<table>
	<?php foreach($option as $entry): ?>
		<td><?php [some flavor of form-field generator function here] ?></td>
		<?php if $loopcount / $divideby == 4 ?>
			</tr>\n<tr>
			<?php $divideby++ ?>
		<?php endif ?>
	<?php endforeach ?>
</table>
Which can be captured with output buffering if neccesary, to my recollection. Haven't really poked at the code itself so the weird modulo thing is all original.

Spades fucked around with this message at 10:55 on Jan 6, 2013

Freakus
Oct 21, 2000
Html in code is bad, but code in templates is bad. There's more code than html in all rewrites I've seen - really, what's the point either way?

Doctor w-rw-rw-
Jun 24, 2008

Freakus posted:

Html in code is bad, but code in templates is bad. There's more code than html in all rewrites I've seen - really, what's the point either way?

For something like alternating row classes for styling, your choices IMO are:
  • Really simple branching in the template, i.e. just an if on a boolean variable
  • Not-stupidly-simple branching in the template
  • Breaking the slight variations in the template into their own separate template fragments, concatenate this in code
  • Concatenating hardcoded text in code
Enabling #1 unfortunately enables #2, but #1 is an entirely reasonable solution.

#3 is workable, but either you need to duplicate the templates a bunch, or you need template inheritance, at which point you've just replicated your problem of branching logic, but in a way that is somewhat more opaque than just a straight-up 'if' statement, unless you have a lot of variations that are used in a lot of different ways in a lot of different places in your code.

#4 is just dumb.

So all in all, I can see very basic logic in templates having its place, though anything approaching even a medium level of complexity would likely be inappropriate.

how!!
Nov 19, 2011

by angerbot

Doctor w-rw-rw- posted:

For something like alternating row classes for styling, your choices IMO are:

or just use nth-child(odd), nth-child(even)

Deus Rex
Mar 5, 2005

how!! posted:

or just use nth-child(odd), nth-child(even)

yeah, it's not like anybody needs to support IE8 or IE7

dexter
Jun 24, 2003

Deus Rex posted:

yeah, it's not like anybody needs to support IE8 or IE7

You're not rendering the page unusable in 7/8, they just don't get 100% of the styling. Pixel-perfect rendering across browsers is normally not worth the development cost.

Freakus
Oct 21, 2000

dexter posted:

You're not rendering the page unusable in 7/8, they just don't get 100% of the styling. Pixel-perfect rendering across browsers is normally not worth the development cost.
It's a far cry between "pixel perfect" and the types of things you might want with what is currently being discussed. Doing things such as alternating backgrounds when you have a large list of things is a huge usability difference.

That said, I find django's "cycle" tag pretty useful for those sorts of scenarios. It also supports IE8. Something similar could be done to support nth-child on the server side.

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
A server side integer variable isn't my idea of an exorbitant cost and you are getting sort of hung up on the specifics of an arbitrary example.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
Yeah, alternating row styling is hardly what I'd consider cosmetic, it actively affects the quality of the user experience when it comes to long tables.
Here's a client side alternative though.

http://selectivizr.com/

Coffee Mugshot
Jun 26, 2010

by Lowtax

Freakus posted:

Html in code is bad, but code in templates is bad. There's more code than html in all rewrites I've seen - really, what's the point either way?

I'd argue that for debugging purposes, you know exactly where to look. If some HTML renders weird or there's some weird typo, you know that it's something wrong with the presentation logic in the template so you can easily go to that exact point and follow any explicitly mentioned macros from there. When it's in the controller or view logic where you build the HTML, then you need to first find where you build that HTML at, since it could be spread across a lot of other logic before it gets echo'd.

There's not much of a difference, but it seems like a good idea to separate controller/view logic from actual presentation logic. And code in templates is a little bit better than interrupting HTML with weird breaks into <?php ?>. Most people don't use the construction syntax and then you get things like:

PHP code:
<?php if (condition) { ?>
Large amount of HTML output with inlined <?php //code ?>
<?php } ?>
which is extremely hard to debug or extend. Or maybe I'm just bitching about the last time I had to write some custom view logic in Wordpress.

shrughes
Oct 11, 2008

(call/cc call/cc)

Freakus posted:

Html in code is bad, but code in templates is bad. There's more code than html in all rewrites I've seen - really, what's the point either way?

Code in templates is not bad.

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.

shrughes posted:

Code in templates is not bad.

Moving stuff to the JS model behind it is both cleaner and easier to unit test, I would say.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Freakus posted:

Doing things such as alternating backgrounds when you have a large list of things is a huge usability difference.

As long as you don't actively block IE 7/8, my opinion is pretty much "deal with it."

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.

Wheany posted:

As long as you don't actively block IE 7/8, my opinion is pretty much "deal with it."

I have (in my terms of trade) a clause which says that I'll only support software I build on the last two versions of the few major browsers, and past that I'll just try my best to ensure that the side can still be used in some form.

I've had a couple of clients ask me about supporting IE 7 and 8, and my response is generally "If you can find the information/use the application at all on those browsers, then they're supported enough. It's not worth your money chasing pixel-perfect support for browsers that are so out of date"

SwimNurd
Oct 28, 2007

mememememe

Suspicious Dish posted:

I guess I don't quite understand why a template engine is necessary for that sort of quick-and-dirty script. Obviously, if it was a lot more than it already was, yeah, but otherwise, I find the code readable, the intention is clear, and I see no reason to make it use templates, yet.

As someone who supports a poo poo ton of "quick-and-dirty" code in production. gently caress developers like think like this. Templates are a good thing, even if the project is simple now. If for some reason down the line things need to change it is a whole hell of a lot easier to modify templates then to try to figure out what that rear end in a top hat 3 developers ago was trying to accomplish.

Bunny Cuddlin
Dec 12, 2004
If nothing else, consider it practice

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde

Freakus posted:

Html in code is bad, but code in templates is bad. There's more code than html in all rewrites I've seen - really, what's the point either way?
My point in this case is that, god willing, someone reviewing the template version with appropriate syntax highlighting will notice that it does not have TDs inside of TRs inside of TABLEs, realize that it's buggy, and correct the SOB; whereas with a string-building approach the tag structure won't be highlighted or otherwise separated from the generated content.

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:
Found about a bazillion instances of this in a ASP.NET MVC3 project
code:
<div>
   @{string SomeButton = "<input id='SomeButton' style='cursor:pointer' type='button' value='Blablabla'/>";
   @Html.Raw(SomeButton)}
</div>

kedo
Nov 27, 2007

code:
<span class="headline"> ... </span>
<span class="paragraph"> ... </span>
:doh: How can you know about classes but not know about super-basic HTML tags?

Bunny Cuddlin
Dec 12, 2004

kedo posted:

code:
<span class="headline"> ... </span>
<span class="paragraph"> ... </span>
:doh: How can you know about classes but not know about super-basic HTML tags?

just a guess, but maybe it's less frustrating for some people to just using divs and replicating the standard p styling instead of figuring out you need to do something like

code:
.container p:last-child {
	margin-bottom: 0;
}

Verloc
Feb 15, 2001

Note to self: Posting 'lulz' is not a good idea.
Recently inherited a C# codebase of dubious quality.

Spent the past few days trying to track down an asinine little error with our streaming app not properly identifying song data. The Test Page absolutely insisted that the metadata service was working properly and was returning info. According to the guy who wrote the system, The Test Page Is Infallible. Let me repeat that, THE TEST PAGE IS INFALLIBLE. It was written by the best .net engineer ever and will instantly detect any problem with services it exercises. Any problems that showed up elsewhere in the system, the first thing he would do is check agains The Test Page. Issue not showing up on The Test Page? Your loving problem, scrub. Stop wasting my time.

After beating my head against the problem I finally said fuckit, and fired up Wireshark in the hopes that maybe the mobile devices were doing something stupid like malforming the request somehow. A quick gander at requests from the mobile devices and then The Test Page showed me one interesting discrepancy. A single query string variable, "test=2" that The Test Page was passing, but nothing else was. Hrm, interesting, that gets appended to the request by a piece of javascript hidden 4 layers deep in includes on that page. Almost like someone was trying to hide it. Ok, let's search the handler code for this test variable and see what it does. Again, buried as deep as it can be buried under layers of delegate handlers, I discover this:

code:
            if (req.QueryString["test"] == "2")
                return ReturnTestPlaylistResultWithHardCodedOfferAndSongIds();
ReturnTestPlaylistResultWithHardCodedOfferAndSongIds is aptly named, it makes the handler return a hard coded success message and plausible looking (but totally fake) metadata.

The Test Page Is Infallible indeed. :catstare:

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

kedo posted:

code:
<span class="headline"> ... </span>
<span class="paragraph"> ... </span>
:doh: How can you know about classes but not know about super-basic HTML tags?

It's HTML5. The class="paragraph" makes it a microformat.

Zamujasa
Oct 27, 2010



Bread Liar

Verloc posted:


ReturnTestPlaylistResultWithHardCodedOfferAndSongIds is aptly named, it makes the handler return a hard coded success message and plausible looking (but totally fake) metadata.

The Test Page Is Infallible indeed. :catstare:

Change it to look for a "3" instead, and then go back to him. :smug:

Plastic Snake
Mar 2, 2005
For Halloween or scaring people.

dwazegek posted:

Found about a bazillion instances of this in a ASP.NET MVC3 project
code:

<div>
   @{string SomeButton = "<input id='SomeButton' style='cursor:pointer' type='button' value='Blablabla'/>";
   @Html.Raw(SomeButton)}
</div>

This makes me want to puke.

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde

dwazegek posted:

Found about a bazillion instances of this in a ASP.NET MVC3 project
Oh, I can just guess:

[Junior dev drops XHTML tag into HTML document]
"Why the gently caress am I getting a warning? Stupid Macro$uck!! They can't do anything right!"
[Dev changes tag to a string echo, saves day]:c00lbutt:

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.
php:
<?
function dieGeneric() {
    die();
}

// ... some code ...

function suicide() {
    die();
}

// then in another file which is always included
function tantrum($message) {
    die();
}
?>
I suppose that the idea is that they would build in certain generic messages or something... but they never did it.

These aren't the only ones in the project, either, and most of them are used somewhere.

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.
php:
<?
function getFarFutureTime() {
    /* This is used to set a cookie to expire some time in the future.
    We'll make it 31/12/2025 so that an update to the site should be done before then.
    This is how Y2K style bugs are started, but if this is site remains unmodified
    till 2025, i'll do the update pro bono :) */
    $thetime = mktime(10,10,10,12,31,2025);
    return $thetime;
}
?>
Oh god.

And one more:

php:
<?
function dbError($conn, $message = '', $query = '') {
    //MailError($message  . "\n" . $conn->ErrorMsg() . "\n" . $query);
    //die($message . "\n" . $conn->ErrorMsg() .  "\n" . $query . "<br/>\n");
}
/*
function dbError($message = '') {
    global $conn;
    die($message . $conn->ErrorMsg() . "<br/>\n");
}
*/
?>

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
Lucky you — The dev before you commented his "e-mail the error to an alias" code.

LOOK I AM A TURTLE
May 22, 2003

"I'm actually a tortoise."
Grimey Drawer

bobthecheese posted:

php:
<?
function dieGeneric() {
    die();
}

// ... some code ...

function suicide() {
    die();
}

// then in another file which is always included
function tantrum($message) {
    die();
}
?>
I suppose that the idea is that they would build in certain generic messages or something... but they never did it.

These aren't the only ones in the project, either, and most of them are used somewhere.

This sounds not so much like a horror as a sadly neglected coding masterstroke.

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

Gazpacho posted:

Oh, I can just guess:

[Junior dev drops XHTML tag into HTML document]
"Why the gently caress am I getting a warning? Stupid Macro$uck!! They can't do anything right!"
[Dev changes tag to a string echo, saves day]:c00lbutt:

Might've been an intern who initially wrote it like this. But the horror goes a bit deeper than that. The guy who mostly works on this project is definitely not a junior developer, but he does heavily favor the copy-paste style of programming, so, what otherwise might've been a few isolated WTFs, are now repeated dozens or even hundreds of times throughout the code base.

For bonus fun, he seems to have realized the stupidity of the aforementioned code, and fixed it on a few pages.

By "fixed it" I mean he changed it into this:
code:
<div>
   @{string SomeButton = "<input id='SomeButton' style='cursor:pointer' type='button' value='Blablabla'/>";
   @SomeButton }
</div>
Of course this was also pushed to production without testing it, so we just had a bunch of users complaining that their buttons weren't really buttons anymore.

Adbot
ADBOT LOVES YOU

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
Well if you decide to fix it be really careful and go slow and test everything. In my previous job I rewrote some copy-pasted code to about a tenth of the code size and felt really good about it until a customer site in Japan said they were broken. (I had left one of their copies out of the new version.)

Gazpacho fucked around with this message at 21:18 on Jan 11, 2013

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply