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
Absurd Alhazred
Mar 27, 2010

by Athanatos

Steve French posted:

So are files containing code, both as written by humans and compiled code.

So let's just use JSON for everything!

That's data-driven development! :v:

Adbot
ADBOT LOVES YOU

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

Steve French posted:

So are files containing code, both as written by humans and compiled code.

So let's just use JSON for everything!

No lie, some of my first personal projects used Python modules as configuration; I'd just import them and access the data I needed that way.

In my defense, the fact that this basically meant that configuration files could do `rm -rf /` and similar did eventually occur to me. :v:

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

I don't have a problem with that.

You configure Django in settings.py

Ranzear
Jul 25, 2013

Pollyanna posted:

You could just do this in that case:

code:
            if (responseJSON == null)  // assuming you're checking specifically for the null case and not for just plain falsiness
            {
                console.log("gently caress")
            }

            return responseJSON;

There may also have been a case for returning dummy test data instead of null while the source of the incoming stuff was still indev, but you're right that it still wouldn't need the else block except out of paranoia.


TooMuchAbstraction posted:

No lie, some of my first personal projects used Python modules as configuration; I'd just import them and access the data I needed that way.

I currently use a .php file with just a config class full of const values. This file can be found by my autoloader by that class name like any other class I write. No parsing, no passing.

code:
<?php
class Config {
	const maxbuttcount = 42;
	const buttsperhour = 69;
	const perfectbuttratio = 1.618;
}
Consts because I think semicolons are more ideal than commas, but whatever.

Then again, maybe this really is a horror (simplified a bit, I even have a mapping of abbreviations to class names!):

code:
<?php
function autoload($class) {
	$classfile = strtolower($class);
	if (file_exists($classfile.".php")) {
		require_once($classfile.".php");
	} elseif (file_exists("../lib/".$classfile.".php")) {
		require_once("../lib/".$classfile.".php");
	}
}

spl_autoload_register("autoload");
And every file just needs
code:
<?php
require_once('../lib/autoload.php');
to be able to include any class on demand.

I write mostly static functions in classes (they're actually faster than bare statics), but this should work for any sort of class stuff.

So with that header you can just call Config::perfectbuttratio anywhere.

Ranzear fucked around with this message at 05:02 on Sep 11, 2017

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I usually just put the constants right in the code, scoped to where they're needed. Extremely rarely do I need to pull them out as configuration to my program: 90% of the time such config values are basically unused.

Ranzear
Jul 25, 2013

These are mostly per-server configuration values kept outside of version control and locked down. MariaDB password, cross-server secret, live vs dev values, etc. Stuff that can't be put right into the db like the regular configurables.

Ranzear fucked around with this message at 05:23 on Sep 11, 2017

ThisIsNoZaku
Apr 22, 2013

Pew Pew Pew!
Modern versions of Spring can use Java classes for configuration, primarily for beans.

Steve French
Sep 8, 2003

It's more important in some development environments than others. For us, we mainly change configuration values when something needs to be updated in production, or in general to vary behavior between local development, staging, and production environments (which downstream service endpoint are we using? which database? what's our metrics namespace?)

Another big factor is language: setting configuration in code for our scala services is a poor option because it's much nicer to be able to update configurations at runtime, and use the same build/deployment artifact in all environments, rather than compile and package all of that hard coded into our JARs and docker images.

Easier to get away with that in Python or PHP

Ranzear
Jul 25, 2013

Forgot to mention my first experience with YAML was someone using it for the config file for a minecraft mod. It took me way too long to figure out how insistent it was about four spaces and never tabs.

Never again.

Then I sent a nastygram to the dude who used JSON instead but you had to add most keys you wanted to change manually because only a few were pre-populated. I take it back for the former, but still not the latter.

Jeb Bush 2012
Apr 4, 2007

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.
iirc the only reason the C spec includes undefined behaviour is so that the compiler can format your hard drive if you use tabs instead of spaces for indentation

Sebbe
Feb 29, 2004

Ranzear posted:

code:
<?php
function autoload($class) {
	$classfile = strtolower($class);
	if (file_exists($classfile.".php")) {
		require_once($classfile.".php");
	} elseif (file_exists("../lib/".$classfile.".php")) {
		require_once("../lib/".$classfile.".php");
	}
}

spl_autoload_register("autoload");
And every file just needs
code:
<?php
require_once('../lib/autoload.php');
to be able to include any class on demand.

I write mostly static functions in classes (they're actually faster than bare statics), but this should work for any sort of class stuff.

So with that header you can just call Config::perfectbuttratio anywhere.

You should be careful loading modules from paths relative to your working directory, rather than relative to the script's location. (Or absolute paths, of course.) You might get code you don't expect loaded.

If you've made a command-line tool, for instance, and there just so happens to be a file with the right name in the folder you run it in, that will get loaded instead of the code you expect. If this is an untrusted folder, and your application is trusted, this might pose a security problem.

It might not be relevant in your use-case, but it is something to consider. In the latest release, Perl spent a great deal of effort removing "." from its default search path for this exact reason: https://news.cpanel.com/cpanel-tsr-2016-0001-full-disclosure/

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe
The reason why code-as-config was dodgy in my case is that the use case was a videogame. I was a little perturbed by the idea of distributing a game where the game data (which players might want to modify and share with each other) was actually code that could e.g. do `rm -rf /`.

Of course game mods often include code content these days, but a) if that's not necessary, e.g. for simple asset mods, then it shouldn't be done, and b) I would hope that game mod systems properly sandbox the code they're running.

Pollyanna
Mar 5, 2005

Milk's on them.


Hey, if you have a property called "validate", which has a function associated with it, what do you expect that function to do?

If you guessed "take the data and return true or false depending on whether the data is valid or not", you're absolutely wrong! What it actually does is munge the data into a particular format we want, e.g. clamping between values or between a range of dates.

This most definitely did not confuse me for a good two hours, no siree. :shepicide:

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
I think every project I've ever worked on that has had "validate" functions has at some point had at least one which actually did format conversion.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

JawnV6 posted:

Adjust the value of "human" as appropriate

I'm a human and I can read it, therefore it's human-readable.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
In order to avoid style wars, I check all my source code in as esprima AST .json files. This way, each person can choose their favorite style when they reserialize the AST back to JavaScript.

Pollyanna
Mar 5, 2005

Milk's on them.


Plorkyeran posted:

I think every project I've ever worked on that has had "validate" functions has at some point had at least one which actually did format conversion.

All of them do that, though. To the point where the documentation explicitly says that validation just sets the values, and all the utility functions are in the `parse` module.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Suspicious Dish posted:

In order to avoid style wars, I check all my source code in as esprima AST .json files. This way, each person can choose their favorite style when they reserialize the AST back to JavaScript.

For a long time thats the basic reasoning I used for indenting with tabs, aligning with spaces...each person could set their favorite indent width.

Problem is no one could be hosed to adjust their editors setting for width of tabs.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Pollyanna posted:

Hey, if you have a property called "validate", which has a function associated with it, what do you expect that function to do?

If you guessed "take the data and return true or false depending on whether the data is valid or not", you're absolutely wrong! What it actually does is munge the data into a particular format we want, e.g. clamping between values or between a range of dates.

This most definitely did not confuse me for a good two hours, no siree. :shepicide:

It returns a valid date I don't see the problem :v:

JawnV6
Jul 4, 2004

So hot ...
Yeah are you sure the function isn't named "validdate"

Nippashish
Nov 2, 2005

Let me see you dance!
Validate, as in "make valid" :colbert:

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

Nippashish posted:

Validate, as in "make valid" :colbert:

That feels like it should be "validize" or something.

Bongo Bill
Jan 17, 2012

Normalize

PhantomOfTheCopier
Aug 13, 2008

Pikabooze!

Thermopyle posted:

Problem is no one could be hosed to adjust their editors setting for width of tabs.
You did. I do. We're not the ones with the problem.

So we let them become lazy because they just whine to their mommies that someone uses a different editor. Guess what happened? Now we have Unicode spaces appearing at the front and back of lines leading to runtime errors (because you know why, snakes are nice but not that language).

Coffee Mugshot
Jun 26, 2010

by Lowtax
Why don't you just pick a style and run a formatter when saving your code?

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Then you lose your sense of superiority.

Jeb Bush 2012
Apr 4, 2007

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.
vertical tabs for indentation, ascii bell character for alignment

Pollyanna
Mar 5, 2005

Milk's on them.


Munkeymon posted:

It returns a valid date I don't see the problem :v:

JawnV6 posted:

Yeah are you sure the function isn't named "validdate"

Nippashish posted:

Validate, as in "make valid" :colbert:

They're actually objects that can represent any particular piece of data, and that object has a validate property that supposedly holds a function that determines if it's valid or not, but apparently that's not actually the case. If it was validDate it'd be funny as poo poo tho.

JawnV6
Jul 4, 2004

So hot ...
I worked with a contractor that didn’t escape strings. When the device was booting, it would print some garbage that included a bell character. My terminal would dutifully ding every clean boot.

By the time I sorted it out I was trained to the bell, if init was messed up it wouldn’t ding, so I left it in like a Pavlovian dog.

FrantzX
Jan 28, 2007

JawnV6 posted:

I worked with a contractor that didn’t escape strings. When the device was booting, it would print some garbage that included a bell character. My terminal would dutifully ding every clean boot.

By the time I sorted it out I was trained to the bell, if init was messed up it wouldn’t ding, so I left it in like a Pavlovian dog.

What's worse, using a bell character, even by accident, or the fact that it still works?

Odette
Mar 19, 2011

Thermopyle posted:

For a long time thats the basic reasoning I used for indenting with tabs, aligning with spaces...each person could set their favorite indent width.

Problem is no one could be hosed to adjust their editors setting for width of tabs.

Prettier along with standard solves this problem, especially if you've setup githooks to format upon add/commit.

Stuff like gofmt is awesome, gently caress these stupid formatting flamewars.

Illusive Fuck Man
Jul 5, 2004
RIP John McCain feel better xoxo 💋 ðŸ™Â
Taco Defender

Pollyanna posted:

Hey, if you have a property called "validate", which has a function associated with it, what do you expect that function to do?

stamp the parking ticket so the customer doesn't need to pay when they leave!

PhantomOfTheCopier
Aug 13, 2008

Pikabooze!

Odette posted:

Stuff like gofmt is awesome, gently caress these stupid formatting flamewars.
Automatic formatting on commit is an interesting point, but we should ask why, after 60 years, are we even having this conversation? On the coder preference side, formatting on commit could actually destroy elements that have been added to improve reading ease. Even PEP8 admits "foolish consistency..." because there are cases where the programmer knows best what formatting is needed. Likewise, commit formatters don't solve the issue of "checkout unformatters"; if it takes one command to "remove nasty tabs before I can edit", well then it takes a command "to remove nasty spaces before I can edit" and we're right back where we started. The reason this is still being discussed is not simply because of preference, but because it's apparently not the easiest thing to "solve".

I think there's a larger horror here: Any system that requires perfect alignment to operate is flawed. A greater issue isn't "spaces versus tabs", it's "why the gently caress isn't this damned thing working at all?!" when one is trying to handle things like Python and YAML. With tabs it's immediately obvious when something is incorrect: Simply changing your tabs to 8 or 20 or 2 or 17 wide in your screen, and the alignment errors are made clear almost instantly, and this all editors handle. With spaces, your eyes glaze over and you're stuck looking at the code one way, gently caress you gentle code reviewer, you will only see what we want you to see, good luck getting out of the mindset of the programmer that made the mistake in the first place.

Yes, the words chosen might be dramatic, but this is the reality. Just yesterday I was handed a YAML config that someone else copy/paste but wasn't working. There were basically two of these within a 500 line config:
code:
- id: 5
  items:
    part_no:   A4786
    descrip:   Water Bucket (Filled)
    price:     1.47
    quantity:  4 
    part_no:   E1628
    descrip:   High Heeled "Ruby" Slippers
    size:      8 
    price:     133.7
    quantity:  1 
- id: 7
  items:
    part_no:   A4787
    descrip:   Water Bucket (Empty)
    price:     1.47
    quantity:  4 
    part_no:   E1629
    descrip:   High Heeled "Glass" Slippers
    size:      8 
    price:     133.7
    quantity:  1 
"Yep, looks like a list of items!", nice human-readable, makes perfect sense, and sent it out for test. Durr, some failure message. Hmm, I guess I'll divide and conquer... oh wait what has the engineer done here? Sheesh, they failed their copy/paste course, blah. How did they miss the hyphens on part_no that signify the start of the hash? Would I have seen it with tabs? I must honestly say that I don't know, but maybe not, though I do believe it would have been much easier to see those blocks amidst the other 500 lines if they were far indented. I was forced into the same thinking the copy/paste phale had made: "Looks good to me".

In the end, after fixing the five YAML formatting errors they made, and the two completely-missing parameters they somehow deleted, there's still significant issues with the values themselves, so I'm giving up and completely dumping their config. I can't really post the example; they have the complete list, but they're not mapped to the subsystems correctly, nor are the values correct (mebi versus mega ffs).

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

You know, I just thought of why I don't like YAML at all.

It's got its own rules that I've got to remember for no reason other than config files and its not even that widely used to begin with.

At least with JSON/XML/code configuration I already know the rules. Yeah, those options all have other problems but apparently those problems haven't been bad enough for me to grow the disdain for them as config file formats that I have for YAML.

(also, I just knew my comment about tabs/spaces would get some replies)

canis minor
May 4, 2011

At least JSON doesn't introduce security concerns (like unserialization) / DoS (like XML)

I'm not touching JSONP

FlapYoJacks
Feb 12, 2009
I'm going insane seeing poo poo like this:

C++ code:

ret = fcntl(fd_sock, F_SETFL, O_NONBLOCK);
if (ret < 0) {
    return ret;
}
return 0;

Spatial
Nov 15, 2007

Two projects I was introduced to today have code quality so bad they have to compile with optimisations disabled just to get predictable behaviour.

No problem, it's just code for a hard real-time system on a bottom-end microcontroller where space and time are both critical

Pollyanna
Mar 5, 2005

Milk's on them.


Spatial posted:

Two projects I was introduced to today have code quality so bad they have to compile with optimisations disabled just to get predictable behaviour.

No problem, it's just code for a hard real-time system on a bottom-end microcontroller where space and time are both critical

Government?

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Jeb Bush 2012 posted:

vertical tabs for indentation, ascii bell character for alignment

One of my TAs in a freshman course told us on day one or two that his favorite thing to do to unlocked screens in the CS building was add vbell to their PS1, rather than do the replace desktop with a screenshot of the desktop prank.

Adbot
ADBOT LOVES YOU

Sebbe
Feb 29, 2004

Munkeymon posted:

One of my TAs in a freshman course told us on day one or two that his favorite thing to do to unlocked screens in the CS building was add vbell to their PS1, rather than do the replace desktop with a screenshot of the desktop prank.

I used to have a lot of fun with this script: https://github.com/eckankar/prank-script

All reversible; some more easily than others.

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