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
Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

Harik posted:

I'm well aware. It's only c++11 in that they're abusing the new features 11 brought. Are there good examples of operator "", or is it just there to be a lurking horror?

Well, a trivial example would be your own bespoke artisanal string class. Saves on the macros.

The builder pattern isn't too bad to use, but it's a huge pain in the rear end to implement.

Adbot
ADBOT LOVES YOU

Space Kablooey
May 6, 2009


Harik posted:

I ran across this when looking up the named arguments pattern in C++, this is the C++11 way:

*snip*

code:
Label DefaultLabel=Label()
    .Name("test")
    .Size(14)
    .Style(STYLE_BOLD);
In the end I decided against any of them, it either scans like someone forgot a trailing semicolon and throws you off, or leads to overly long lines that do too many different things.

I can't really make out what's the boilerplate from the implementation of the the actual class, but if that does what I think it is doing (it autogenerates setter methods that return the object based on the class members), that snippet is pretty cool, IMO.

Besides, I don't think the Builder pattern is that bad since the successive methods are should be indented away, like it is on your label example.

This thing is at least better than doing the pattern in Java.

Plorkyeran
Mar 22, 2007

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

Dessert Rose posted:

Well, a trivial example would be your own bespoke artisanal string class. Saves on the macros.
Or just your non-bespoke std::string.

Other than strings the motivating use-case is for numeric units. With C++14 you can do things like 5s + 200ms and get the expected result.

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

LeftistMuslimObama posted:

One of our internal tools is basically a wrapper around a keypass database to launch sessions in all our internal unix environments. I just discovered that it can't handle passwords containing spaces unless you surround your password with quotes. So, if my password is Pass Word, I have to enter "Pass Word" in the application to be able to connect to my environment. The developer has chosen not to put the code into any of the SVN repositories (a horror in itself), so I can't find the offending code, but I can only assume that he's not escaping the password before passing it to PuTTY as an argument, because the bug doesn't happen with another terminal emulator that is launched via RPC rather than invoked via the system.

Managed to track down the source for this. Sure enough, the code was (slightly psuedo'd anonymized):
C# code:
sUnixPass = CredentialsClass.upass; //yes, that's systems hungarian notation
...
//in another method that receives the unix pass
args = somePuTTYarg + " " + anotherarg + " " + sUnixPass;
Process.Start(<puttypath>,args);
Unsurprisingly, a quote inside a password makes the process think the arg list is over. A space makes it think it has more args than there are. I fixed it by calling uri.escape on the password first.

Always escape your strings kids! It's fortunate that there's not really any putty arguments that could be destructive if your password had a space with a putty argument after in it.

shodanjr_gr
Nov 20, 2007

LeftistMuslimObama posted:

Managed to track down the source for this. Sure enough, the code was (slightly psuedo'd anonymized):
C# code:
sUnixPass = CredentialsClass.upass; //yes, that's systems hungarian notation
...
//in another method that receives the unix pass
args = somePuTTYarg + " " + anotherarg + " " + sUnixPass;
Process.Start(<puttypath>,args);
Unsurprisingly, a quote inside a password makes the process think the arg list is over. A space makes it think it has more args than there are. I fixed it by calling uri.escape on the password first.

Always escape your strings kids! It's fortunate that there's not really any putty arguments that could be destructive if your password had a space with a putty argument after in it.

Is there something particularly wrong with using systems hungarian in this context? (I get that it is C# code but still...)

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

shodanjr_gr posted:

Is there something particularly wrong with using systems hungarian in this context? (I get that it is C# code but still...)

This isn't a bad example of it, but it does result in us having stuff like integers named i_m_oNumThings (to indicate that it's an int32 global object member), which is dumb-looking, and becomes dumber when someone turns it into a long to fix an overflow because then it should be l_m_oNumThings so now your "self-documenting" code is actually deceptive. We have a lot of floats/doubles out there with int prefixes too.

I'm also of the opinion that given Visual Studio's wonderful intellisense can give you all of the information that hungarian tries to convey, that adding extra characters to the name of the variable that become annoying to refactor when you make changes is silly. I realize that variable naming conventions are one of those Internet Strong Opinions things though.

shodanjr_gr
Nov 20, 2007

LeftistMuslimObama posted:

This isn't a bad example of it, but it does result in us having stuff like integers named i_m_oNumThings (to indicate that it's an int32 global object member), which is dumb-looking, and becomes dumber when someone turns it into a long to fix an overflow because then it should be l_m_oNumThings so now your "self-documenting" code is actually deceptive. We have a lot of floats/doubles out there with int prefixes too.

Yeah but that's pretty standard Hungarian notation criticism right?

quote:

I'm also of the opinion that given Visual Studio's wonderful intellisense can give you all of the information that hungarian tries to convey, that adding extra characters to the name of the variable that become annoying to refactor when you make changes is silly. I realize that variable naming conventions are one of those Internet Strong Opinions things though.

I've seen Hungarian used in very large systems projects. Part of it was probably inertia (the software that I worked on started development in the 90s). I'm pretty sure the other part of it was that there was no decent code-completion/code-sense software that could properly parse the source tree without exploding (circa 2012 at least). Honestly, if you have a decent code review/code standards culture, stale hungarian names are not a problem imo.

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

shodanjr_gr posted:

Yeah but that's pretty standard Hungarian notation criticism right?


I've seen Hungarian used in very large systems projects. Part of it was probably inertia (the software that I worked on started development in the 90s). I'm pretty sure the other part of it was that there was no decent code-completion/code-sense software that could properly parse the source tree without exploding (circa 2012 at least). Honestly, if you have a decent code review/code standards culture, stale hungarian names are not a problem imo.

Yeah, all of our code goes through two rounds of review if it's code we're releasing. In that sense, Hungarian is actually the bane of my existence because 90% of the time my code gets bounced back to me out of code review it's because I forgot to put hungarian prefixes on my variables instead of any functional problems. To me, it's a distraction to be coding and then need to pause and think "what's the right prefix for this?", but I recognize that other people find it useful and for all intents and purposes I'm outvoted on this one.

We don't use hungarian on our MUMPS code (because MUMPS has no types). But we do use it on our JavaScript for some reason? I guess to indicate how you're expecting the variable to be used.

VikingofRock
Aug 24, 2008




i_m_o it might make sense to use Hungarian notation in a duck-typed language, just to make it clear whether your number parser returns 2 or '2'.

That said, I've never used it for that.

Suspicious Dish
Sep 24, 2011

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

xzzy posted:

X window managers are a coding horror in a category of their own, there's been more from scratch "minimalist" implementations that slowly turn into bloated monsters than any other software type.

I don't think you understand how difficult ICCCM compliance is in practice.

xzzy
Mar 5, 2009

It's not the compliance that's a coding horror. It's the legions of wet behind the ears programmers firing up their own project with broad proclamations how THIS TIME things will be different, I mean it guys!

Before Linux settled on Gnome and KDE window managers were popping up like weeds.

Zopotantor
Feb 24, 2013

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

Suspicious Dish posted:

I don't think you understand how difficult what a piece of poo poo ICCCM compliance is in practice.

quote:

In summary, ICCCM is a technological disaster: a toxic waste dump of broken protocols, backward compatibility nightmares, complex nonsolutions to obsolete nonproblems, a twisted mass of scabs and scar tissue intended to cover up the moral and intellectual depravity of the industry's standard naked emperor.

Pavlov
Oct 21, 2012

I've long been fascinated with how the alt-right develops elaborate and obscure dog whistles to try to communicate their meaning without having to say it out loud
Stepan Andreyevich Bandera being the most prominent example of that
Not too long ago I was trying to see if I could change the default keybinds for switching from X to a tty session from Ctrl-Alt-F# to ALT-F#. After two or three days of xmodmap and xorg.conf and XKB the answer was "I don't know but gently caress X."

qntm
Jun 17, 2009
Might as well link the whole thing

sarehu
Apr 20, 2007

(call/cc call/cc)
To be fair it was the 80's or maybe even the 70's and we still had leaded gasoline and lead paint back then.

Zibidibodel
Jan 10, 2012

Here's a little good ol' ruby.

The question posed was:

quote:

Question: We have 4 bottles of 1 ltr, 5 ltr, 7 ltr and 10 ltr. And we can use them with their full quantity only.

What is the number of minimum bottles required to get the desired output.

The reply was:

code:
results = {}

[4,3,2,1].each { |x|
  b10 = b7 = b5 = b1 = 0
  ask = 122


  if x == 4
    b10 = ask/10
    ask = ask - (10*b10)
  end


  if x >= 3
    b7 = ask/7
    ask = ask - (7*b7)
  end


  if x >= 2
    b5 = ask/5
    ask = ask - (5*b5)
  end

  b1 = ask/1


  total_b = b1 + b5 + b7 + b10

  results[total_b] = "\n You need \n#{b10} x10lt \n#{b7} x7lt \n#{b5} x5lt \n#{b1} x1lt\n\n"
}


results.each { |k, v|
  puts v
  puts "Total no of bottles #{k}"
}

puts "Best: total #{results.min.first} bottles"
puts results.min.last 
I found the worst part to be that array of [4,3,2,1] as the pure definition of unreadable code. Also it doesn't answer the question.

Suspicious Dish
Sep 24, 2011

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

Pavlov posted:

Not too long ago I was trying to see if I could change the default keybinds for switching from X to a tty session from Ctrl-Alt-F# to ALT-F#. After two or three days of xmodmap and xorg.conf and XKB the answer was "I don't know but gently caress X."

If you're still curious, edit /usr/share/X11/xkb/symbols/srvr_ctrl and change type="CTRL+ALT", to type="CTRL", for all of the VT bindings.

Suspicious Dish
Sep 24, 2011

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

For however bad X11 is, lmao at anybody thinking NeWS is any better. NeWS involved people writing custom PostScript scripts to shove inside a giant rear end display server, get access to the entire global namespace, even do filesystem and network operations. Not to mention that they added a way to load NeWS at runtime from an untrusted source. So you could just randomly pwn some machines on your network by sending them malicious PostScript scripts that were self-replicating and inserted them into your system at boot.

Pavlov
Oct 21, 2012

I've long been fascinated with how the alt-right develops elaborate and obscure dog whistles to try to communicate their meaning without having to say it out loud
Stepan Andreyevich Bandera being the most prominent example of that

Suspicious Dish posted:

If you're still curious, edit /usr/share/X11/xkb/symbols/srvr_ctrl and change type="CTRL+ALT", to type="CTRL", for all of the VT bindings.

drat, is that where it is? Then why do the xmodmap entries for the function keys have a column that specifically throws a VT keysym? I'll be honest the documentation for some of this X11 stuff is all over the loving place.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
xmodmap modifies legacy X11 keymaps through the ChangeKeyboardMapping protocol request. Those keymaps are legacy and were eventually replaced with XKB over time.

raminasi
Jan 25, 2005

a last drink with no ice

Zibidibodel posted:

Here's a little good ol' ruby.

The question posed was:


The reply was:

code:
results = {}

[4,3,2,1].each { |x|
  b10 = b7 = b5 = b1 = 0
  ask = 122


  if x == 4
    b10 = ask/10
    ask = ask - (10*b10)
  end


  if x >= 3
    b7 = ask/7
    ask = ask - (7*b7)
  end


  if x >= 2
    b5 = ask/5
    ask = ask - (5*b5)
  end

  b1 = ask/1


  total_b = b1 + b5 + b7 + b10

  results[total_b] = "\n You need \n#{b10} x10lt \n#{b7} x7lt \n#{b5} x5lt \n#{b1} x1lt\n\n"
}


results.each { |k, v|
  puts v
  puts "Total no of bottles #{k}"
}

puts "Best: total #{results.min.first} bottles"
puts results.min.last 
I found the worst part to be that array of [4,3,2,1] as the pure definition of unreadable code. Also it doesn't answer the question.

I've been looking at this for five minutes and I still can't figure out what's supposed to be horrific about it.

Harik
Sep 9, 2001

From the hard streets of Moscow
First dog to touch the stars


Plaster Town Cop

sarehu posted:

To be fair it was the 80's or maybe even the 70's and we still had leaded gasoline and lead paint back then.

That's actually a really good explanation for what happened to X.

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

GrumpyDoctor posted:

I've been looking at this for five minutes and I still can't figure out what's supposed to be horrific about it.

Style and structure aside, try that algorithm on 29.

Sebbe
Feb 29, 2004

NihilCredo posted:

Style and structure aside, try that algorithm on 29.

Ah, lookie there. The good old change-making problem.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
I mean, the algorithm is wrong, but that's not a completely ridiculous implementation of the algorithm. If it used a foreach loop instead of the each method, you probably wouldn't think twice about it.

feedmegin
Jul 30, 2008

Suspicious Dish posted:

For however bad X11 is, lmao at anybody thinking NeWS is any better. NeWS involved people writing custom PostScript scripts to shove inside a giant rear end display server, get access to the entire global namespace, even do filesystem and network operations. Not to mention that they added a way to load NeWS at runtime from an untrusted source. So you could just randomly pwn some machines on your network by sending them malicious PostScript scripts that were self-replicating and inserted them into your system at boot.

That was a different time though, back in the mid-80s when telnet and rsh were considered just fine for example despite sending passwords in cleartext over the wire, everyone ran fingerd, etc, etc. Back in the days before ubiquitous internet access to everything people didn't generally worry so much about security.

Pavlov
Oct 21, 2012

I've long been fascinated with how the alt-right develops elaborate and obscure dog whistles to try to communicate their meaning without having to say it out loud
Stepan Andreyevich Bandera being the most prominent example of that

Suspicious Dish posted:

If you're still curious, edit /usr/share/X11/xkb/symbols/srvr_ctrl and change type="CTRL+ALT", to type="CTRL", for all of the VT bindings.

Ok I actually got around to trying this, and this is wrong. CTRL doesn't look to be a pre-defined type (neither is ALT). I fiddled around a bit, and it seems to do what I want if I instead use type="PC_ALT_LEVEL2" and the form [ F1, XF86_Switch_VT_1 ] for my symbols. Thanks for pointing me in the right direction though.

loinburger
Jul 10, 2004
Sweet Sauce Jones
Co-worker: I want to generate a bunch of unique ids, but I want them to appear to be random. I was thinking of using a UUID
loinburger: How many ids do you want to generate?
Co-worker: At most a hundred million
loinburger: Okay, that seems reasonable, you're not likely to get a collision if you use a UUID with that many ids
Co-worker: The problem is that the UUIDs take up 128 bits, so I was going to truncate them down to 32 bits in order to save memory
loinburger: Don't do that, you're pretty much guaranteed to get collisions with a hundred million ids and a 32-bit hash
Co-worker: But the hash is 128 bits
loinburger: No, the hash is 32 bits - you're truncating the UUID down to 32 bits, so you have at most 2^32 bit permutations
Co-worker: But... the hash is 128 bits

After a few more minutes of this he agreed that his 32-bit hash was a 32-bit hash, so crisis averted (I don't know what he's using these ids for, but I'm assuming that it would be a Bad Thing if he wound up with a shitton of repeats).

I suggested that he look into non-repeating random number generators if he wanted to save 96 bits per id / eliminate the unlikely chance that he'd generate two identical UUIDs out of a hundred million ids

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

loinburger posted:

eliminate the unlikely chance that he'd generate two identical UUIDs out of a hundred million ids

Heat death of the universe.

ExcessBLarg!
Sep 1, 2001
Why is 128 bits a problem but 32 bits is not? Is the 4x space factor that serious of a limitation?

I'm guessing 128 bits just "seems too big."

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

ExcessBLarg! posted:

Why is 128 bits a problem but 32 bits is not? Is the 4x space factor that serious of a limitation?

I'm guessing 128 bits just "seems too big."

Well, if you're generating 1e8 of them then it's a third of a gigabyte difference, which could be a meaningful thing for some applications.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

ExcessBLarg! posted:

Why is 128 bits a problem but 32 bits is not? Is the 4x space factor that serious of a limitation?

I'm guessing 128 bits just "seems too big."

Well a hundred million 128bit keys is about 1.5 gigabytes. That's not insignificant if you can avoid it.

HappyHippo fucked around with this message at 22:03 on May 21, 2015

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

One of us has the math off by a factor.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Subjunctive posted:

One of us has the math off by a factor.

I think it might be both of us? I corrected mine, hopefully.

loinburger
Jul 10, 2004
Sweet Sauce Jones
He's probably using them as keys in Redis, in which case the memory savings would be significant. I have no idea why he can't just use a counter i.e. why the keys have to be random-looking (he indicated that he didn't want random keys, he just wanted them to "appear" to be random - otherwise I wouldn't have directed him to the article on non-repeating random number generators, because they're probably pretty lovely in terms of randomness)

Tank Boy Ken
Aug 24, 2012
J4G for life
Fallen Rib
Well if each 128bit code = 128 bits of memory = 100million = 1.5 gigabyte, the 32 bit code is roughly a third of a gigabyte. The difference would be ~1.125 gigabyte. One of you was talking about total size, the other about the difference.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

HappyHippo posted:

I think it might be both of us? I corrected mine, hopefully.

Yes, quite. I'll leave mine for future shame.

raminasi
Jan 25, 2005

a last drink with no ice

rjmccall posted:

I mean, the algorithm is wrong, but that's not a completely ridiculous implementation of the algorithm. If it used a foreach loop instead of the each method, you probably wouldn't think twice about it.

Yeah this is what I meant. Wrong <> a horror, in my book. And is that unidiomatic for Ruby? (Honestly asking, I'm not a Ruby dev. It didn't jump out as weird to me.)

ExcessBLarg!
Sep 1, 2001
I'm not sure there's a truly idiomatic way to write that loop in Ruby, although this would be familiar to most:
code:
4.downto(1).each do |x|
    ...
end
In general, the "each" method is preferred over C-style for loops, and is Ruby's "foreach" construct. Braces are usually used for single-line blocks, while do/end are used for multiline ones. Although I wouldn't have thought much about the way it's presently written.

Adbot
ADBOT LOVES YOU

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
How would you duplicate the functionality of JavaScript's escape function in Objective-C?

If your answer involves finding out what the escape function does, and writing the ~10 lines to do that in Objective-C, then you're a fool. The correct answer is: transform the string to a JS string literal, then create a webview and use it to run a script that calls the escape function on that literal.

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