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
qntm
Jun 17, 2009

fritz posted:

I found a line in some academic C code just now that has 5 "="s, two arithmetic operators, and a [].

Sounds like another victim of K&R.

Adbot
ADBOT LOVES YOU

canis minor
May 4, 2011

fritz posted:

I found a line in some academic C code just now that has 5 "="s, two arithmetic operators, and a [].

a += b[0] == c + d == f

What do I win?

dealmaster
Dec 24, 2005

Somebody call for an exterminator?

eithedog posted:

a += b[0] == c + d == f

What do I win?

Mother of God....

Newlines, motherfucker, do you speak it? Unless you're just trying to be cute, there's no reason to put all that crap on a single line.

dealmaster fucked around with this message at 14:09 on Jul 16, 2014

Volmarias
Dec 31, 2002

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

eithedog posted:

a += b[0] == c + d == f

What do I win?

The shame of having written that.

dealmaster
Dec 24, 2005

Somebody call for an exterminator?

Edison was a dick posted:

i = i++ is undefined behaviour in C, so Java could have inherited that feature.

No, it's perfectly defined, it just doesn't increment the variable. The guy who wrote that was a SQL developer that got assigned some Java development, so he didn't realize all you needed to do was ++i; and be done with it.

EAT THE EGGS RICOLA posted:

Someone just handed me a ~7000 line javascript thing to look at and make major changes to by tomorrow at the latest. Every variable and method has a two or three character random name. For example something I might call getElementId is called sn.

I'm legitimately thinking of just pretending this conversation never happened and/or just killing myself so I don't have to deal with this.

This type of poo poo is the worst. I remember getting added to a code review that included over 90 files after the project was completely done and this guy worked on it all by himself. Writing to the console everywhere, lovely design, bad variable names, Pokemon exception handling, etc. But the code was already being signed off by the business, so it was too late to go back and change everything. Why do we get added to reviews like this at the very last second when nothing can realistically be done???

dealmaster fucked around with this message at 14:14 on Jul 16, 2014

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

dealmaster posted:

Why do we get added to reviews like this at the very last second when nothing can realistically be done???

So that the person who's actually responsible for that situation has someone to pass the buck to when it all comes crashing down.

If you're not able to actually veto the change, it's not a real code review, and you should probably make sure it's not your responsibility by stating that you have reservations and <person who punted the review to you> can sign off on it if they think it's good.

Space Kablooey
May 6, 2009



why is this so funny?

dealmaster
Dec 24, 2005

Somebody call for an exterminator?

Jabor posted:

So that the person who's actually responsible for that situation has someone to pass the buck to when it all comes crashing down.

If you're not able to actually veto the change, it's not a real code review, and you should probably make sure it's not your responsibility by stating that you have reservations and <person who punted the review to you> can sign off on it if they think it's good.

Yeah I knew it was purely a cover-your-rear end thing, my question was more me raging at stupid coworkers. I was able to make him implement many of my changes including some changes in design, but not everything was done, unfortunately. Gotta meet that arbitrary release date, yo. You know, the date that someone promised someone higher up months and months ago without consulting the developers at all. My coworkers and I like to call those rear end-dates. There are also rear end-metrics, rear end-numbers, and rear end-requirements that all originate from someone's rear end and never went through any formal process at all.

Coffee Mugshot
Jun 26, 2010

by Lowtax

eithedog posted:

a += b[0] == c + d == f

What do I win?

How about,

a ^= (b[0] == c) ^ (d == f);

which doesn't seem unreasonable at all?

Carthag Tuek
Oct 15, 2005

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



There's no benefit to writing it like that. Use intermediate variables with clear names and let the compiler figure out how to optimize stuff.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Snapchat A Titty posted:

There's no benefit to writing it like that. Use intermediate variables with clear names and let the compiler figure out how to optimize stuff.

Even if the compiler doesn't optimize it as well as you do, I'd rather have readable code that takes a few nanoseconds longer to execute.

raminasi
Jan 25, 2005

a last drink with no ice
I guess I'm the horror because I find that readable :(

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Voted Worst Mom posted:

How about,

a ^= (b[0] == c) ^ (d == f);

which doesn't seem unreasonable at all?

But also doesn't do the same thing?

shrughes
Oct 11, 2008

(call/cc call/cc)
If you knew what b, c, d, f, and a were, that would be perfectly fine.

Flagrama
Jun 19, 2010

Lipstick Apathy

Subjunctive posted:

But also doesn't do the same thing?

There wasn't a code sample posted. They are both guessing what it could have been based off the description.

JawnV6
Jul 4, 2004

So hot ...
And everyone's cheating the "5 =" by assuming a single assignment and two comparisons.

a = (b=4)+(c=6) +(d=e=0);

fritz
Jul 26, 2003

You asked for it.
code:
umax=(vmin=input[kminus=k=k0])+(umin=lambda)-vmax;

b0lt
Apr 29, 2005

fritz posted:

You asked for it.
code:
umax=(vmin=input[kminus=k=k0])+(umin=lambda)-vmax;

:stare:

hobbesmaster
Jan 28, 2008

fritz posted:

You asked for it.
code:
umax=(vmin=input[kminus=k=k0])+(umin=lambda)-vmax;

So he intended to write:
vmin = input[k0];
k = k0;
kminus = k;
umin = lambda
umax=vmin+umin-vmax;

Coylter
Aug 3, 2009
code:
Private Function IsZipCodeValid(ByRef strZipCode As String) As Boolean
		IsZipCodeValid = False
		strZipCode = strZipCode.Replace(" ", "")
        If strZipCode.Length = 6 Then
            strZipCode = strZipCode.ToLower
            If IsString(strZipCode.Substring(0, 1)) Then
                If IsNumber(strZipCode.Substring(1, 1)) Then
                    If IsString(strZipCode.Substring(2, 1)) Then
                        If IsNumber(strZipCode.Substring(3, 1)) Then
                            If IsString(strZipCode.Substring(4, 1)) Then
                                If IsNumber(strZipCode.Substring(5, 1)) Then
                                    IsZipCodeValid = True
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
	End Function

	Public Function IsNumber(ByVal chrNombre As Char) As Boolean
		If chrNombre >= "0" And chrNombre <= "9" Then
			IsNumber = True
		Else
			IsNumber = False
		End If
	End Function

	Private Function IsString(ByVal chrString As Char) As Boolean
		If chrString >= "a" And chrString <= "z" Then
			IsString = True
		Else
			IsString = False
		End If
	End Function
Well, let's just say this made my day. This gentlemens, is part of the server code of something important.

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer

JawnV6 posted:

And everyone's cheating the "5 =" by assuming a single assignment and two comparisons.

a = (b=4)+(c=6) +(d=e=0);

Seriously stop people. This is like the code golf of horrors.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Flagrama posted:

There wasn't a code sample posted. They are both guessing what it could have been based off the description.

Ah, my mistake.

Polio Vax Scene
Apr 5, 2009



Coylter posted:

Well, let's just say this made my day. This gentlemens, is part of the server code of something important.

I enjoyed this code a lot, because first you do a quick scan and see the giant if pyramid, then you read it again and realize what it's actually for, then you read it again and see how it's doing it. And each time you lose more faith in humanity.

Also, chrNombre.

dealmaster
Dec 24, 2005

Somebody call for an exterminator?

Coylter posted:

code:
If pyramid poo poo code
Well, let's just say this made my day. This gentlemens, is part of the server code of something important.

Has the writer of this never heard of a regular expression? Jesus Christ.

xtal
Jan 9, 2011

by Fluffdaddy

dealmaster posted:

Has the writer of this never heard of a regular expression? Jesus Christ.

In fairness, regular expressions are pretty horrific themselves.

Workaday Wizard
Oct 23, 2009

by Pragmatica

xtal posted:

In fairness, regular expressions are pretty horrific themselves.

No they are not. The "...now you have two problems :v:" meme is garbage.

A regex is basically a deterministic finite automaton (DFA) and your string is the tape it runs on. As simple as that.

Most people who struggle with regex do because they don't bother testing their regex in Notepad++ or Sublime or what have you.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

xtal posted:

In fairness, regular expressions are pretty horrific themselves.

Yeah, "now you have two problems."

That code's not horrible to read once you squint past the boilerplate. If it were chained with && it would be pretty decent. Maybe better with the equivalent of a string stream or sscanf?

^^^

Regular expressions are a classic example of write-only code, especially without Perl's /x readability help. This case is simple enough that it could be OK, but it's a dangerous path. I've worked on regex engines (JS's replace-with-function-call is my fault), and I have to think pretty carefully to make sure I'm thinking about greed and backtracking correctly with all but the simplest stuff.

(Most regex engines are not actually regular expressions, and they all vary a bit in syntax or semantics, which also hurts understanding after the fact.)

Edit: testing in Sublime is great for the cases you think of, but exploring that space effectively is hard.

Subjunctive fucked around with this message at 02:23 on Jul 17, 2014

Workaday Wizard
Oct 23, 2009

by Pragmatica

Subjunctive posted:

Yeah, "now you have two problems."

That code's not horrible to read once you squint past the boilerplate. If it were chained with && it would be pretty decent. Maybe better with the equivalent of a string stream or sscanf?

^^^

Regular expressions are a classic example of write-only code, especially without Perl's /x readability help. This case is simple enough that it could be OK, but it's a dangerous path. I've worked on regex engines (JS's replace-with-function-call is my fault), and I have to think pretty carefully to make sure I'm thinking about greed and backtracking correctly with all but the simplest stuff.

(Most regex engines are not actually regular expressions, and they all vary a bit in syntax or semantics, which also hurts understanding after the fact.)

I disagree that regex is write-only.

A regex is literally the machine that will match your string, as in what you see is 100%, no doubts about it, what you get.

Not being able to figure out a regex instantly is not a failure of regex. A simple line of regex is the equivalent of pages of code in your language of choice. That's the point of using regex after all.

If there is a more readable regex equivalent please share. (Note: more characters to type != more readable)

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
There's two problems (heh) with regexes that lead to their unreadability when used as a parser for human input.

The first is that human input is messy. Phone numbers are going to be 123-456-7890. Or they might be (123) 456-7890. Or just 456-7890. Or maybe even +11234567890. And then you can easily replace spaces with dashes or parens or just nothing in any number of configurable ways to meet certain conventions. A regular expression that matches "phone numbers" has to take into account these different customs is going to be complex, because the original problem is really complex.

The second problem is that traditional regular expression languages don't have any inherent composability. There's no way to say delimiter = '-' | ' ' | ''; and then use (\d{3})? $delimiter (\d{3}) $delimiter (\d{4}). Because of this, you also see a lot of clever trickery in common regex syntax to group some parts together, and a lot of "code golf" that makes it dense and unreadable. Trying to understand a regular expression is like reverse engineering assembly code, almost.

The problem with regexes isn't that they're inexact. It's that the user experience of them is designed in such a way to lead to discourage breaking problems down and readability, and increase cleverness.

Workaday Wizard
Oct 23, 2009

by Pragmatica
I agree that composability would be a nice feature to have. But I don't agree with the rest.
If your matching rules are complex, why are expecting a simple nice to read solution?

E: the unreadability of the regex is not because it's regex. It's because your rules are complex. (Try matching with your own favorite language, heck try writing the matching rules in English and see).

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I'm saying that our current regular expression representations could go a long way towards encouraging more readability and composability, and right now they fail in those aspects, because every character matters.

Googling for "phone number regex", the first result I found is: ^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$

Given enough time, you can break apart what it's doing. It's almost like compiler output, and that it really has to be reverse-engineered. I won't clean up that, since this is a dumb forum post and I have work tomorrow in the morning. But imagine an alternate representation:

code:
digit_group(n) = \d{n};

delimiter = '-' | ' ' | '';

area_code = (
    # (123)
    ('(' + digit_group(3) + ')') |
    # 123
    digit_group(3)
);

global_extension = (
    '+' + number{0:4}
);

phone_number = (
    # +1 (123)-456-7890
    global_extension? + area_code + delimiter + digit_group(3) + delimiter + digit_group(4)
);
Yes, this is more like EBNF notation, I know, but it can still be represented with the same exact state machine as a regex. It is, for all intents and purposes, a regular expression.

Workaday Wizard
Oct 23, 2009

by Pragmatica
As I said

Shinku ABOOKEN posted:

I agree that composability would be a nice feature to have.

I don't want to continue the derail any longer, and I also don't want people to avoid the right tool for the job.
My point is if your problem is solvable by regex just use regex and call it a day. Don't be superstitious.

Plorkyeran
Mar 22, 2007

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

Suspicious Dish posted:

code:
digit_group(n) = \d{n};

delimiter = '-' | ' ' | '';

area_code = (
    # (123)
    ('(' + digit_group(3) + ')') |
    # 123
    digit_group(3)
);

global_extension = (
    '+' + number{0:4}
);

phone_number = (
    # +1 (123)-456-7890
    global_extension? + area_code + delimiter + digit_group(3) + delimiter + digit_group(4)
);
Yes, this is more like EBNF notation, I know, but it can still be represented with the same exact state machine as a regex. It is, for all intents and purposes, a regular expression.
And as it happens, that sort of thing does actually exist for most languages. For example, that using lpeg (chosen because I've used it most recently, not because it's unusually good for this specific example):
Lua code:
-- lpeg only has at least n and at most n, with no exactly n
function digit_group(n)
    pat = R'09'
    while n > 0 do
        pat *= R'09'
        n = n - 1
    end
    return pat
end

delimiter = S'- '^-1
area_code = '(' * digit_group(3) * ')' + digit_group(3)
global_extension = '+' + R'09'^-4 * delimiter
phone_number = global_extension^0 * area_code * delimiter * digit_group(3) * delimiter * digit_group(4)
It has a similar learning curve to regular expressions in that there's some opaque syntax in the name of concision, but being an EDSL rather than a DSL in strings means you can use all of the host language features.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Regexes are fine and cool for the trivial case, and you should probably use them, with the major caveat that you should throw them away with extreme prejudice as soon as your case becomes non-trivial.

Pythagoras a trois
Feb 19, 2004

I have a lot of points to make and I will make them later.
Edit: Instead of contributing, I made a snide comment in favor of regexes. My two cents is use them, let the golfers play golf and then paste their string into your code. Portability in favor of readability, because I wouldn't read it anyway, I don't care enough about international numbers or trailing extensions, and would botch the implementation if I tried.

To contribute: The reason I'm in a snide mood is because yesterday my client came back to me with a recurring problem. We have a yellow rectangle on the main page, where there's a logo, and text to the right of it. Again she said that the way it renders in her browser, Firefox, is as a rectangle with a logo in it, and a scroll bar on its right hand side. If she scrolls down, there's the text, hidden away.

So I changed the settings and tested the site in Firefox and Chrome and Safari and IE and IE10 and IE9 and can't duplicate the issue, so every time I say "It looks fixed" I mean to say "It definitely isn't doing that under any normal circumstances, and I re-solved the problem from a different direction so maybe that helped your strange computer".

But yesterday, while on the phone talking about the website, she saw the front page and yelled "Oh, it's fixed!"
Then proceeded to say "But when I zoom in I still get the scroll bars".

Oh.

Pythagoras a trois fucked around with this message at 15:34 on Jul 17, 2014

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
code:
int sprintf(char *out, const char *format, ...)
{
        va_list args;
        
        va_start( args, format );
        return print( &out, format, args );
}


int snprintf( char *buf, unsigned int count, const char *format, ... )
{
        va_list args;
        
        ( void ) count;
        
        va_start( args, format );
        return print( &buf, format, args );
}
:smithicide:

BigRedDot
Mar 6, 2008

Subjunctive posted:

Yeah, "now you have two problems."
My favorite one of those was about Java... "now you have a problem factory"

Hughlander
May 11, 2005

Suspicious Dish posted:

I'm saying that our current regular expression representations could go a long way towards encouraging more readability and composability, and right now they fail in those aspects, because every character matters.

Googling for "phone number regex", the first result I found is: ^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$

Given enough time, you can break apart what it's doing. It's almost like compiler output, and that it really has to be reverse-engineered. I won't clean up that, since this is a dumb forum post and I have work tomorrow in the morning. But imagine an alternate representation:

code:
digit_group(n) = \d{n};

delimiter = '-' | ' ' | '';

area_code = (
    # (123)
    ('(' + digit_group(3) + ')') |
    # 123
    digit_group(3)
);

global_extension = (
    '+' + number{0:4}
);

phone_number = (
    # +1 (123)-456-7890
    global_extension? + area_code + delimiter + digit_group(3) + delimiter + digit_group(4)
);
Yes, this is more like EBNF notation, I know, but it can still be represented with the same exact state machine as a regex. It is, for all intents and purposes, a regular expression.

While I get your point, what you wrote doesn't match the same state machine as the regex, that's why the regex is so complex. Your test input isn't a valid phone number. Area code and exchanges can't begin with 1. The regex posted matches with or without an area code. Hell the *SPECIFIC* one you listed also allows for an extension at the end optionally. That's why they're useful.
Modify yours so that '555-0134 ext 1321' is allowed, but 123-456-7890 isn't for some fun.

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

Suspicious Dish posted:

The second problem is that traditional regular expression languages don't have any inherent composability. There's no way to say delimiter = '-' | ' ' | ''; and then use (\d{3})? $delimiter (\d{3}) $delimiter (\d{4}). Because of this, you also see a lot of clever trickery in common regex syntax to group some parts together, and a lot of "code golf" that makes it dense and unreadable. Trying to understand a regular expression is like reverse engineering assembly code, almost.

Lex is 39 years old. How much more traditional can you get?

Adbot
ADBOT LOVES YOU

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Suspicious Dish posted:

Googling for "phone number regex", the first result I found is: ^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$

That regular expression doesn't even match my phone number, anyway, so for all that complexity it still isn't good enough. It doesn't even match it if I "internationalise" the number by deleting the leading zero and adding +44.

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