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
Tad Naff
Jul 8, 2004

I told you you'd be sorry buying an emoticon, but no, you were hung over. Well look at you now. It's not catching on at all!
:backtowork:

Manslaughter posted:

The API service I'm trying to communicate with is sending me ampersands inside of its xml. And not &, just plain old & by itself. :(

I don't think I've ever run across one that didn't do this :( yay for sanitizing XML before you can actually use it.

Adbot
ADBOT LOVES YOU

One Eye Open
Sep 19, 2006
Am I awake?

PleasingFungus posted:

if (bool_value == true) ... is longer code, to no benefit & reduced readability.

Quite the opposite, in readability terms. If you're skimming code, and your native language is English, you read it as "if bool_value is true" and are able to separate out the semantics of type, even if you missed the variable declaration. The compiler doesn't care either way. Anybody who has to work on your codebase might.

If you're using it with any variable that isn't bool (or doesn't have a clean conversion), then that is more of a problem than being explicit in your comparison.

E: vvvv That too. vvvv

One Eye Open fucked around with this message at 02:17 on Dec 20, 2013

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



One Eye Open posted:

Quite the opposite, in readability terms. If you're skimming code, and your native language is English, you read it as "if bool_value is true" and are able to separate out the semantics of type, even if you missed the variable declaration. The compiler doesn't care either way. Anybody who has to work on your codebase might.

If you're using it with any variable that isn't bool (or doesn't have a clean conversion), then that is more of a problem than being explicit in your comparison.

In readability terms you should use a variable that makes it read properly. if (shouldContinue && (hasConnection || canLog)) {...}

Sockser
Jun 28, 2007

This world only remembers the results!




Gazpacho posted:

If you've successfully replaced a 45,000 line system with 1200(!) unit-tested lines — actually done it, not talked about it and underdelivered — chances are you're already so far ahead in the best practices game that there's not much to tell you.

As for theory stuff, I'd suggest that you find out what textbooks top-rated CS programs are using on your subjects of interest and buy some of them to study.

The codebase I inherited was created by a mechanical engineer, handed to another mech, then to some dipshit who didn't know what he was doing, and then two CoEs before I got to it. Nobody ever had time to refactor or rewrite or do anything of the sort, so the entire thing was basically a ball of duct tape a rubber bands held together with paper clips and bandaids and powered by the souls of sacrificed orphans.

One of the first things I found when I got this job, like in my first month, was a chunk of 1000+ lines of code repeated four times in separate pieces of the code, which handily explained why none of my bugfixes were working. poo poo like that is what I had to deal with.

Also that discussion about making everything into a class from a couple pages back. The project is test automation software, like Jenkins but way cooler, so it handles basically two things, installing software and running software. The stuff I inherited would model a software plugin as a class and like
code:
MicrosoftExcel me = new MicrosoftExcel();
if(me.hasnewbuildavailable())
     me.installNewestBuild(me.getLocationWhereIshouldInstallThisButTheVariableIsDefinedEighteenOtherGoddamnedPlaces())
And I'd replace it with like, 3 or 4 lines that would just be FuckingGoInstallExcel()
because gently caress you, why would you make that a class?

Replacing a few dozen xml files with a MySQL database really helped as well.

For what it's worth (not much, I know), Visual Studio code metrics of old software (top) vs new software (bottom)


theres a couple projects of a few thousand lines each that can't have metrics calculated in the old poo poo for whatever reason, and looking at this now is telling me that I need to do some refactoring myself, whoops.

I'm sure if I went back to the old code and poked around I'd find tons of stuff that'd fit into this thread quite nicely

e: and while I'm on the subject, I have never found an article that succinctly explained how the gently caress VS calculates that maintainability index or what it even loving means. I'm pretty sure it doesn't actually mean anything but what do I know

Sockser fucked around with this message at 02:59 on Dec 20, 2013

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
http://blogs.msdn.com/b/codeanalysis/archive/2007/11/20/maintainability-index-range-and-meaning.aspx

Crazy Mike
Sep 16, 2005

Now with 25% more kimchee.

Volmarias posted:

Since you wrote it, and you're running it, have you considered fixing it?

It's an idea, of course I had no idea how much of a pain in the rear end reconstituting the unsent email and did not want to interrupt it. I had to step out of the office for a while and when I got back there was an email stating there was a typo in the email we're sending out and to fix the typo before continuing.

I managed to cancel the emailer, fix the typo and create a new SPROC to list all the unsent emails from the group we are sending and repopulate with that, and fire it up again. In my haste I forgot to take another five minutes to remove the MessageBox and recompile this thing.

One of the improvements for the emailer program is the ability to create emails to send and replacing the simple text box for the email body with a rich text box to show formatting. I showed a demo of the program for the other programmers Monday with the implicit understanding that there were things that needed to be fixed and no testing done on it. I did not expect my boss to tell me today that we are using this for real. I have a feeling this happens a lot in the programmer community. Things like this make me glad I'm on alternate work week because three day weekends rock.

ToxicFrog
Apr 26, 2008


Dren posted:

A list of examples of operators behaving in incongruous ways depending on their arguments is php-like.

I find
Python code:
(True and 'a')
returning 'a' to be kind of funky.

How is that funky? That's been a common convention for boolean operators (in languages that permit them to operate on non-booleans at all) since Lisp.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
It also makes a bit of sense when you consider the short-circuiting nature of boolean operators.

False and f() won't evaluate f(). You need to think of the and / or operators as combinators that return either the left-hand or right-hand expression, rather than something that returns the result of the purely logical AND / OR.

Is this a good thing? Not sure. It's obviously a lot easier to implement and more performant in a lower-level language, and it allows for some language tricks like fire(employee_name or "shrughes") for when you need a default value.

seiken
Feb 7, 2005

hah ha ha

Suspicious Dish posted:

The same thing is true in C as well.

(C99, 6.5.13p3) "The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0."

You are programming by superstition.

Pilsner
Nov 23, 2002

Scaevolus posted:

People get all emotional when you start calling their language "weak", and smug when their language is "dynamic".
Heh, I remember back in my early university days, our lecturer said that C++ was an example of a "low level language", and for example Java was a "high level language". I felt a bit offended as he said it, because I liked C++ back then (for some reason). I was like, WTF, what's "low level", as in bad, about it? :colbert:

Amarkov
Jun 21, 2010

Suspicious Dish posted:

Is this a good thing? Not sure. It's obviously a lot easier to implement and more performant in a lower-level language, and it allows for some language tricks like fire(employee_name or "shrughes") for when you need a default value.

It also allows for people not to explain those tricks, which causes everyone who learns Python first to try

code:
if check == (value1 or value2):

QuarkJets
Sep 8, 2008

Dren posted:

A list of examples of operators behaving in incongruous ways depending on their arguments is php-like.

I find
Python code:
(True and 'a')
returning 'a' to be kind of funky.

That's because you don't actually understand what 'and' does:

Python.org posted:

x and y: if x is false, then x, else y (2)

nielsm
Jun 1, 2009



In languages that lack an explicit expression-if construction, having "and" and "or" return its arguments is a massive help. For instance in Lua, foo and bar or baz, equivalent to C ternary foo?bar:baz. The same also works in Python, and I prefer it to the bar if foo else baz construct, only because it keeps the condition first instead of stuffing it in between the two values.

Qwertycoatl
Dec 31, 2008

nielsm posted:

In languages that lack an explicit expression-if construction, having "and" and "or" return its arguments is a massive help. For instance in Lua, foo and bar or baz, equivalent to C ternary foo?bar:baz. The same also works in Python, and I prefer it to the bar if foo else baz construct, only because it keeps the condition first instead of stuffing it in between the two values.

I don't like it, because it looks like it's equivalent to foo?bar:baz but isn't.

Suspicious Dish
Sep 24, 2011

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

seiken posted:

(C99, 6.5.13p3) "The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0."

You are programming by superstition.

It appears I am, yep. The program I was debugging before that did the equivalent(a && b) * 100 was in JavaScript, where the convention works. I thought it picked it up from C, but I guess not.

Athas
Aug 6, 2007

fuck that joker
Using booleans as integers also has value when working with architectures that are not boring. For example, consider vector machines - they can execute instructions that operate across an array of values, but each element of the array must be processed the same. In essense, this is a very simple 'map' operation.

Consider an expression like this:

code:
let result = map(fn (x) => if x < 0 then f(x) else g(x), a)
This is a map across the input 'a'. You cannot directly execute this on a vector machine, because the function you're mapping with contains a branch, which is a no-no. Instead, you must decompose like this:

code:
let conds = map(fn (x) => x < 0, a)
let fs = map(fn (x) => f(x), a)
let gs = map(fn (x) => g(x), a)
let result = map(fn (cond, v1, v2) => cond * v1 + !cond * v2, conds, fs, gs)
(Actually, you must decompose the final map a bit more, since many systems will not have vector instructions that accept more than two input arrays.)

Interestingly, this is pretty much how GPUs work (in that both branches of a conditional are always taken), although you don't have to perform the above decomposition yourself.

tef
May 30, 2004

-> some l-system crap ->

Amarkov posted:

It also allows for people not to explain those tricks, which causes everyone who learns Python first to try

code:
if check == (value1 or value2):

this would work in Icon :colbert:

mjau
Aug 8, 2008

nielsm posted:

In languages that lack an explicit expression-if construction, having "and" and "or" return its arguments is a massive help. For instance in Lua, foo and bar or baz, equivalent to C ternary foo?bar:baz.
Be careful with that though, it's not exactly the same. If bar evaluates to false, the Lua code will always give you baz.

Dren
Jan 5, 2001

Pillbug

QuarkJets posted:

That's because you don't actually understand what 'and' does:

I understand. And I know about Python's ghetto ternary operator. The sticking point for me was that the examples listed show what appears to be incongruous behavior, though I suppose they're really about both the behavior of and as well as the overloaded behavior of *:
code:
>>> (True and True) * 100
100
>>> (True and 2) * 100
200
>>> (True and "a") * 100
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
There are at least two common languages (C and Python) with very different concepts of what the boolean operator should return:

seiken posted:

(C99, 6.5.13p3) "The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0."

python.org posted:

x and y: if x is false, then x, else y (2)

I find the idea that boolean operators should return 1 or 0 more intuitive but it seems like people from functional programming backgrounds prefer the other way since I think I've seen some arguments here about the utility of boolean operators returning the truthy argument in functional languages.

One Eye Open
Sep 19, 2006
Am I awake?

Athas posted:


Interestingly, this is pretty much how GPUs work (in that both branches of a conditional are always taken), although you don't have to perform the above decomposition yourself.

Not quite, most modern GPUs have MIMD branching, but for if..then..else blocks, every path conditional is evaluated. (Here it's explained for GPGPU stuff)

necrotic
Aug 2, 2005
I owe my brother big time for this!
So we've got this database with millions of records that is constantly read and written to. There's a nightly job that pulls in data from an external source that's encoded as UTF-8. Our database is configured with a UTF-8 charset:

code:
CREATE TABLE `whatever` (
  `id` blah blah blah
) CHARSET=utf8;
That's great. How everything should be.

code:
production:
  adapter: mysql2
  encoding: latin1
Oh, poo poo. It's been this way for 4+ years, and as we go global we're starting to see a bunch of invalid bytesequence errors. The good news is that only around ~50k codepoints are problematic, including most of the middle eastern characters and all characters between \u0080 and \u00FF which includes some very basic accented characters.

:smithicide:

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice
How anything can default to ANSI code pages in this day and age is beyond me.

necrotic
Aug 2, 2005
I owe my brother big time for this!

Ender.uNF posted:

How anything can default to ANSI code pages in this day and age is beyond me.

Also neat: this is mysql (huzzah), so latin1 is an extend ISO-8559-1 charset used by Microsoft called cp1252.

quote:

latin1 is the default character set. MySQL's latin1 is the same as the Windows cp1252 character set. This means it is the same as the official ISO 8859-1 or IANA (Internet Assigned Numbers Authority) latin1, except that IANA latin1 treats the code points between 0x80 and 0x9f as “undefined,” whereas cp1252, and therefore MySQL's latin1, assign characters for those positions. For example, 0x80 is the Euro sign. For the “undefined” entries in cp1252, MySQL translates 0x81 to Unicode 0x0081, 0x8d to 0x008d, 0x8f to 0x008f, 0x90 to 0x0090, and 0x9d to 0x009d.

I can kind of understand the decision, but making it the default over utf8 is aggravating if someone inexperienced with character sets gets his hands on it first.

nielsm
Jun 1, 2009



mjau posted:

Be careful with that though, it's not exactly the same. If bar evaluates to false, the Lua code will always give you baz.

True, luckily Lua only has two falsey values, "false" and "nil", so it's generally easy to figure out if the bar expression can take those values.

zeekner
Jul 14, 2007

necrotic posted:

Also neat: this is mysql (huzzah), so latin1 is an extend ISO-8559-1 charset used by Microsoft called cp1252.


I can kind of understand the decision, but making it the default over utf8 is aggravating if someone inexperienced with character sets gets his hands on it first.

These forums return Content-Type:text/html; charset=iso-8859-1

But it's really cp1252. That was a fun thing to find out when certain characters were being parsed incorrectly in the awful app.

Freakus
Oct 21, 2000

necrotic posted:

I can kind of understand the decision, but making it the default over utf8 is aggravating if someone inexperienced with character sets gets his hands on it first.
Note that mysql's utf8 doesn't support 4 byte characters. If you want that you need to use utf8mb4.

necrotic
Aug 2, 2005
I owe my brother big time for this!

Freakus posted:

Note that mysql's utf8 doesn't support 4 byte characters. If you want that you need to use utf8mb4.

Oh really? This is handy information, thanks!

EntranceJew
Nov 5, 2009

Inspired by the post about ebay's "eval" usage earlier, I decided to dive into the php codebase I support.
1900+ instances, most of which inside JS files, but here are some select PHP ones:
php:
<?
//if (substr(trim($what), -1) != ';') {
//    $what .= ';';
//}
eval('echo ' . $what . ';');
?>
You and me both, little buddy.
php:
<?php
class Clense_SQL__Controller {
    /**
     * Clense table data
     * @param string $table_name
     * @param array $data_array
     * @static
     * @return void
     * @author [redacted]
     * @since 2009.08.04
     */
    static function clenseDataTable($table_name, &$data_array) {
        require_once('really/long/redacted/path/Clense_SQL_'.$table_name.'.class.inc');
        eval('Clense_SQL_'.$table_name.'::clenseData($data_array);');

    }
}
?>

Oh.
security/db/security.class
php:
<?
eval($str_to_eval);
?>
Hmm.

EntranceJew fucked around with this message at 19:43 on Dec 20, 2013

xtal
Jan 9, 2011

by Fluffdaddy

Dren posted:

A list of examples of operators behaving in incongruous ways depending on their arguments is php-like.

I find
Python code:
(True and 'a')
returning 'a' to be kind of funky.

Ruby code:
$ irb
>> true and 'a'
=> "a"
Lua code:
$ lua
> print(true and 'a')
a
I think it's the same in Perl, JavaScript and virtually every other language in use. Ironically, PHP is the only example I could find that doesn't behave that way.

xtal fucked around with this message at 01:50 on Dec 21, 2013

Hughlander
May 11, 2005

Freakus posted:

Note that mysql's utf8 doesn't support 4 byte characters. If you want that you need to use utf8mb4.

MySQL is the php of the database world in all possible meanings.

necrotic
Aug 2, 2005
I owe my brother big time for this!

Hughlander posted:

MySQL is the php of the database world in all possible meanings.

Yup. We want to switch to Postgres but it will be a rather large project given how much data needs to move.

Steve French
Sep 8, 2003

xtal posted:

Ruby code:
$ irb
>> true and 'a'
=> "a"

Lua code:
$ lua
> print(true and 'a')
a

I think it's the same in Perl, JavaScript and virtually every other language in use. Ironically, PHP is the only example I could find that doesn't behave that way.

You're not trying hard enough

gonadic io
Feb 16, 2011

>>=
This all seems like stockholm syndrome for languages where anything could implicitly and silently be null.

Color Gray
Oct 10, 2005
BARF!
I maintain a project that was full of plenty of horrors (the original author was...uh...), although fortunately most of them have been removed over time by either myself or our other developers. My favorite had to be this "pattern" (yes, it was used more than once) for processing rows:

code:
int i = 0;

foreach (var row in rows)
{
    ProcessRow(rows[i]);

    i++;
}
How should I loop over the rows...with a foreach loop? With a for loop and an index? Nah...I'll use both! :shepicide:

I mean, it's incredibly stupid, but at least it will still process the correct rows, right? Well, one time it looked like this:

code:
int i = 0;

foreach (var row in rows)
{
    if (/* some condition */)
    {
        continue;
    }

    ProcessRow(rows[i]);

    i++;
}
:cripes:

Opinion Haver
Apr 9, 2007

xtal posted:

I think it's the same in Perl, JavaScript and virtually every other language in use.

Except for all those languages with strong static type checking, but who uses those.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Color Gray posted:

I maintain a project that was full of plenty of horrors (the original author was...uh...), although fortunately most of them have been removed over time by either myself or our other developers. My favorite had to be this "pattern" (yes, it was used more than once) for processing rows:

code:
int i = 0;

foreach (var row in rows)
{
    ProcessRow(rows[i]);

    i++;
}
How should I loop over the rows...with a foreach loop? With a for loop and an index? Nah...I'll use both! :shepicide:

I mean, it's incredibly stupid, but at least it will still process the correct rows, right? Well, one time it looked like this:

code:
int i = 0;

foreach (var row in rows)
{
    if (/* some condition */)
    {
        continue;
    }

    ProcessRow(rows[i]);

    i++;
}
:cripes:

To be fair, I do kind of wish that there was a way to elegantly combine the for loop with the foreach loop. If I use a for loop with an iterator or a get method, it's still an extra method call. I kind of wish there was something like:

code:
foreach (var row in rows with index i) {
  ...
}

Jewel
May 2, 2009

Volmarias posted:

To be fair, I do kind of wish that there was a way to elegantly combine the for loop with the foreach loop. If I use a for loop with an iterator or a get method, it's still an extra method call. I kind of wish there was something like:

code:
foreach (var row in rows with index i) {
  ...
}

Python has that :getin:

Python code:
for row in rows: #normal method
	...
	
for index, row in enumerate(rows):
	...
But I agree. More languages need that functionality, I find myself needing the index in a foreach style loop a lot of the time.

Although, the enumerate method is outlined as being this:

Python code:
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
So technically it's exactly the same as what you do in every other language. What other languages have a feature like "yield", also? I never really noticed it in anything else, or if I did I forgot.

Jewel fucked around with this message at 05:03 on Dec 21, 2013

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Jewel posted:

Python has that :getin:

Python code:
for row in rows: #normal method
	...
	
for index, row in enumerate(rows):
	...

You win again, python! :argh:

Opinion Haver
Apr 9, 2007

What are these strange 'for loops' of which you speak?

code:
> mapM_ (\(i, word) -> putStrLn (show i ++ " " ++ word)) (zip [0..] ["foo", "bar", "baz"])
0 foo
1 bar
2 baz

Adbot
ADBOT LOVES YOU

FrantzX
Jan 28, 2007
C# code:
public struct Indexed<T>
{
	public Indexed(Int32 index, T item)
	{
		Index = index;
		Item = item;
	}

	public readonly Int32 Index;

	public readonly T Item;
}

public static class Extensions
{
	static IEnumerable<Indexed<T>> Indexed<T>(this IEnumerable<T> sequence, Int32 startindex = 0)
	{
		Int32 index = startindex;
		foreach (T obj in sequence) yield return new Indexed<T>(index++, obj);
	}
}
LINQ & extensions method are some the best features I have ever seen in a language.

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