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
NtotheTC
Dec 31, 2007


Linear Zoetrope posted:

code:

bool TRUE = true
bool FALSE = false

bool x_is_not_y = x != y;

if (x_is_not_y == TRUE) {
    return FALSE;
} else if (x_is_not_y == FALSE) {
    return TRUE;
} else {
    throw "Boolean value is not true or false!";
}

Requirements change in the real world guys jesus

Adbot
ADBOT LOVES YOU

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison
the last page has demonstrated once again why composition > inheritance

Beef
Jul 26, 2004

Cuntpunch posted:

code:
return x != y ? false : true;
Reduced LOC

code:
return __builtin_expect((x!=y),0) ? false : true;
Fixed for continued bandwagon jumping.

CPColin
Sep 9, 2003

Big ol' smile.

uncurable mlady posted:

the last page has demonstrated once again why composition > inheritance

I can't tell if this is calling me discomposed or inbred, but, either way, shots fired!

NihilCredo
Jun 6, 2011

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

There is a particular kind of coder who would write it like:

code:
if (x.ToString().ToUpper() == y.ToString().ToUpper()) { return true; } else { return false;}
and then blame the runtime when it doesn't work as they magically expected.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Opferwurst posted:

Aren't Lists internally implemented as dynamic arrays in .NET? Why do I see people converting them to static arrays? What's the advantage?

If performance is critical and you have a data set of a static size, you can avoid the list having to resize the internal array. It's not a huge cost, but it's certainly not free. After I typed that, I remembered that List also has a constructor that sets the initial capacity, so... maybe not so much.

Also for interop with PowerShell or other .NET languages where it's easier to use primitive types.

Cuntpunch
Oct 3, 2003

A monkey in a long line of kings

New Yorp New Yorp posted:

If performance is critical and you have a data set of a static size, you can avoid the list having to resize the internal array. It's not a huge cost, but it's certainly not free. After I typed that, I remembered that List also has a constructor that sets the initial capacity, so... maybe not so much.

Also for interop with PowerShell or other .NET languages where it's easier to use primitive types.

Yeah, if you know a reasonable upper-limit size for your list, initializing it with that size will allocate once at the start. And if for some reason you've underguessed, then internally the capacity is just going to get doubled when you overrun.

GABA ghoul
Oct 29, 2011

qsvui posted:

C++ code:
bool _returnValue =false;

if (x == y)
{
  _returnValue =true;
}
else
{
  _returnVaalue = false;
}

return _returnValue;

this is really good code, but I made a small improvement to make it more flexible

CPColin
Sep 9, 2003

Big ol' smile.

NihilCredo posted:

There is a particular kind of coder who would write it like:

code:
if (x.ToString().ToUpper() == y.ToString().ToUpper()) { return true; } else { return false;}
and then blame the runtime when it doesn't work as they magically expected.

You gotta make it null-safe, like this:

code:
x?.ToString().ToUpper()
Whew, that's better!

dick traceroute
Feb 24, 2010

Open the pod bay doors, Hal.
Grimey Drawer
Which languages have null != null?

JawnV6
Jul 4, 2004

So hot ...
IEEE 754

Doom Mathematic
Sep 2, 2008
null isn't the same as NaN.

Linear Zoetrope posted:

code:
bool x_is_not_y = x != y;

if (x_is_not_y == true) {
    return false;
} else if (x_is_not_y == false) {
    return true;
} else {
    throw "Boolean value is not true or false!";
}

Want to see how they got to 100% coverage on that one.

taqueso
Mar 8, 2004


:911:
:wookie: :thermidor: :wookie:
:dehumanize:

:pirate::hf::tinfoil:

I'm a little worried about the return value, we should make sure it is in range:
pre:
assert(_returnValue == true || _returnValue == false, "Invalid return bool!!");
(These are in Java right? I don't know how to write this in java, probably something with an assertFactory)

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

dick traceroute posted:

Which languages have null != null?

SQL

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe

Woudln't that be null <> null? (I still get a headache when I run into that operator, it's like whoever did it just made it different to be contrarian)

SupSuper
Apr 8, 2009

At the Heart of the city is an Alien horror, so vile and so powerful that not even death can claim it.
You're overcomplicating it.
code:
return !(x - y);

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Joda posted:

Woudln't that be null <> null? (I still get a headache when I run into that operator, it's like whoever did it just made it different to be contrarian)

Depends on the platform. MSSQL has the IS/IS NOT NULL keyword which gets hinky with empty string depending on the data type

Hammerite
Mar 9, 2007

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

:wrong:

http://sqlfiddle.com/#!17/9eecb/26296

JawnV6
Jul 4, 2004

So hot ...

Doom Mathematic posted:

null isn't the same as NaN.
so? 0x00000000 != 0x80000000

Jazerus
May 24, 2011


what we really need here is a BoolFactoryBeanFactory

Doom Mathematic
Sep 2, 2008

JawnV6 posted:

so? 0x00000000 != 0x80000000

So, null isn't part of IEEE 754.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

dick traceroute posted:

Which languages have null != null?

Unity overwrites ==

This means that == and != will ‘work’ but ?. will not for anything that inherits UnityEngine.Object

Tann
Apr 1, 2009

I thought I'd share a few of the horrors from my current job. Standard Java enterprise junk, there's a lot of code from about 5 years ago that's very suspect. I've cleaned up a lot of it but some sticks in my mind.

Inside Constants.java:
code:
public static final Integer ZERO = 0;
public static final Integer ONE = 1;
public static final Integer TWO = 2;
public static final Integer THREE = 3;
...
// some random other constants inbetween, some of which references these numbers!
...
public static final Integer EIGHT = 8;
....
// some more random constants
...
public static final Integer TEN = 10;
Inside some entity that's mapped to a DB table. I think it was coming in as ints from a frontend thing so they decided to store it as both???
code:
private Boolean onMonday;

private Integer onMondayValue;

private Boolean onTuesday;

private Integer onTuesdayValue;

// all the way to Sunday!
We also had a way of crashing the server by adding 10 rows and removing 10 rows from this customisable form on the frontend. It was calling the server to find the html it should display each time you edited it and there was a bug which caused some list to multiply in size each time.

Plus someone dropped a production schema last year! I'm very glad it happened to someone else and I consider it an incredible learning experience that I will take with me for the rest of my life.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Jazerus posted:

what we really need here is a BoolFactoryBeanFactory

You fool, how are we supposed to inject implementations of those factories? And we don't want to maintain more than one instance, so we'll need a convenient superclass for creating singletons of it.

AbstractSingltonBoolFactoryBeanAbstractFactory

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

I don't know if it's a Coding Horror or a Coding Awesome, but I was doing something else and came across this simple convert units call:
code:
private decimal GetCentimerte(blah)
And it immediately spawned in my head... THE HUNDRED DEATHS - CENTIMEURTE

hackbunny
Jul 22, 2007

I haven't been on SA for years but the person who gave me my previous av as a joke felt guilty for doing so and decided to get me a non-shitty av

Opferwurst posted:

Aren't Lists internally implemented as dynamic arrays in .NET? Why do I see people converting them to static arrays? What's the advantage?

No advantage. In fact, converting to a static array takes extra memory (the new array object) and extra execution time (copying items from the list to the array). Behind a List/IList you can hide all sorts of optimizations, like preallocating larger arrays to allow for expansion, or using multiple arrays for storage. A .NET array, on the other hand, is extremely inflexible: it has a fixed size and all items are world-accessible (and world-writable!). You can't efficiently build an array item by item: you have to keep the list of elements somewhere else (... like a List), allocate an array of the right size, and then copy the elements to it. As a general rule, never use arrays unless you have a really good reason

Night Shade
Jan 13, 2013

Old School

hackbunny posted:

No advantage. In fact, converting to a static array takes extra memory (the new array object) and extra execution time (copying items from the list to the array). Behind a List/IList you can hide all sorts of optimizations, like preallocating larger arrays to allow for expansion, or using multiple arrays for storage. A .NET array, on the other hand, is extremely inflexible: it has a fixed size and all items are world-accessible (and world-writable!). You can't efficiently build an array item by item: you have to keep the list of elements somewhere else (... like a List), allocate an array of the right size, and then copy the elements to it. As a general rule, never use arrays unless you have a really good reason

hey neat, now i have a new weekend project, an ArrayPool<> and Span<> backed IList implementation

Cuntpunch
Oct 3, 2003

A monkey in a long line of kings
code:
//C#

public bool ValidationThing()
{
	var validations = new Dictionary<string,bool>
	{
		{"validationA", validateA()},
		{"validationB", validateB()},
		//etc etc.
	}

	return validations.All(validation => validation.Value);
}

Hammerite
Mar 9, 2007

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

Cuntpunch posted:

code:
//C#

public bool ValidationThing()
{
	var validations = new Dictionary<string,bool>
	{
		{"validationA", validateA()},
		{"validationB", validateB()},
		//etc etc.
	}

	return validations.All(validation => validation.Value);
}

This is a bit gratuitous, and if you're going to go to the trouble of structuring things this much then it should probably be a class instead of a dictionary, but it's not really a horror. I can see how something would end up like this if the dev found themselves having to frequently debug through whatever this is. Or a piece of code that had more of a reason to use a dictionary could have ended up this way after a few changes without rationalizing things.

Also ValidationThing() is not a good name for this method but I assume that's just you anonymizing this code

Cuntpunch
Oct 3, 2003

A monkey in a long line of kings

Hammerite posted:

This is a bit gratuitous, and if you're going to go to the trouble of structuring things this much then it should probably be a class instead of a dictionary, but it's not really a horror. I can see how something would end up like this if the dev found themselves having to frequently debug through whatever this is. Or a piece of code that had more of a reason to use a dictionary could have ended up this way after a few changes without rationalizing things.

Also ValidationThing() is not a good name for this method but I assume that's just you anonymizing this code

The names are anonymized, but the structure is that. There's literally nothing trimmed out (except the 4 or 5 other validation methods). But a Dictionary where you never actually use the Keys?

On the second pass it turned into a List<bool>. All just to be able to use LINQ's All() method.

The problem there being is that unlike simply doing
code:
 return validateA() && validateB() //etc 
By pre-calling everything you don't get any valuable short-circuiting logic.

Hammerite
Mar 9, 2007

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

Cuntpunch posted:

The names are anonymized, but the structure is that. There's literally nothing trimmed out (except the 4 or 5 other validation methods). But a Dictionary where you never actually use the Keys?

That's why I speculated that they had had to use it a lot while debugging. It looks like something you might come up with if you wanted to look at the results of all those methods with the debugger attached.

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

Cuntpunch posted:

By pre-calling everything you don't get any valuable short-circuiting logic.

That could potentially be a feature, again if you're looking at this in the debugger

Carbon dioxide
Oct 9, 2012

Microsoft open-sourced their calc.exe source code.

https://github.com/Microsoft/calculator

It is full of coding horrors.

Also, it phones home with statistical usage data.

Also people are leaving funny PRs and stuff now. https://github.com/Microsoft/calculator/pull/158/files

raminasi
Jan 25, 2005

a last drink with no ice
New Microsoft definitely has some fun moments, gotta say.

Kazinsal
Dec 13, 2011


I do like that we're basically seeing from open sourced Windows components like this what everyone who thinks things through past "TELEMETRY EVIL NSA SPYWARE" expected: they're looking at poo poo like how many people actually use the memory and bitflip features in the calculator.

Munkeymon
Aug 14, 2003

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



Telyra posted:

I do like that we're basically seeing from open sourced Windows components like this what everyone who thinks things through past "TELEMETRY EVIL NSA SPYWARE" expected: they're looking at poo poo like how many people actually use the memory and bitflip features in the calculator.

Probably also crash reports, right?

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Telyra posted:

I do like that we're basically seeing from open sourced Windows components like this what everyone who thinks things through past "TELEMETRY EVIL NSA SPYWARE" expected: they're looking at poo poo like how many people actually use the memory and bitflip features in the calculator.

Plus the ever valuable Troll Deterrent:
https://github.com/Microsoft/calculator/pull/156

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
the only time i saw someone go OMG TERRIBLE CODE was for a 10-line class that wrapped std::vector to remove exceptions from the API. i mean it pulls in Windows.ApplicationModel.Calls.CallPhoneContract for seemingly no reason and takes 150ms to lay out the UI but thats just XAML being terrible not the calc app.

in theory you should be able to use /EHsc to disable exceptions, but then you cant use modern C++ with it https://developercommunity.visualstudio.com/content/problem/341669/c-include-has-a-warning-out-of-the-box-c4530-in-pp.html

Soricidus
Oct 21, 2010
freedom-hating statist shill
loving hell seeing that screenshot on the calc repo reminded me just how hideous "modern windows apps" are. vast and featureless rectangles of uniform gray, with randomly sized text scattered around in slightly different shades of gray. it's like someone looked at an x11 gui from 1985 and said "hmm, the only problem i can see with this is that there's just too much contrast"

bring back aero, or at least the concept of having ui designers who make a basic effort to make things pleasing to look at.

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

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

yeah, calc.exe is hilariously slow to open. I always laugh a little bit when I have to use it.

Good thing its basically the only "modern" app I use, because I'd just be laughing non stop.

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