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
GABA ghoul
Oct 29, 2011

Cuntpunch posted:

code:
//C#
public class Foo
{
    private string _text;
   
    public Foo(string text)
    {
        if(text != null)
        {
            _text = text;
        }
    }
}
This 'pattern' was *rampant* at work and every time I asked the people doing it *why* they did it, they cited some sense of 'because we have always done it this way'. Some of the more junior folks, when asked *what* it does spent a lot of time and effort talking themselves in circles about how it was preventing null references.

Someone who didn't know that strings have null as their default value wrote this ten years ago and then it cargoculted from there.

Also, I'm new to C# and serious coding so I'm really looking forward to see my code in here very soon.

Adbot
ADBOT LOVES YOU

GABA ghoul
Oct 29, 2011

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

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

GABA ghoul
Oct 29, 2011

Saw this gem today

code:

if(someList.Count <= 0)

:wtc:

GABA ghoul
Oct 29, 2011

Volguus posted:

Personally I believe that Empty() would read better than Any, but that's neither here or there. Checking for "Count <= 0", especially when said Count returns a signed value just screams of defensive programming. You never know when the value could potentially overflow, when a bug in that list implementation may return -1, when radiation from the sun will flip a bit. Since I have a hard time believing that "== 0" is more efficient than "<= 0" I would almost always prefer the "<= 0" if something like "Empty" is not available.

:ms:

I asked about it and apparently it's what happens when you use some ReSharper auto refactoring functionality. It inverts a ">0" statement, but isn't smart enough to know that arrays can't have a negative size.

Also, gotta agree that if the fabric of space and time broke down and System.Collections started producing negative sized arrays I would want my program to drop everything and tell me ASAP.

GABA ghoul
Oct 29, 2011

Linear Zoetrope posted:

Out of wonder, which languages? The only one I know of is C strings, I wasn't aware that some (modern) languages didn't just keep an associated count variable.

LINQ extends everything enumerable with a Count() method. Mixing up list.Count and list.Count() can have huge performance implications in C#.

GABA ghoul
Oct 29, 2011


Hi badidea, you can just type -75000 or if you REALLY want to specify a range for some reason you can also type -65000.00057. As it's currently working and usable, it's not a high priority fix for now. We did put it on our to-do list though!

GABA ghoul
Oct 29, 2011

CPColin posted:

Yeah, I've seen if (map.contains(key)) { return map.get(key) } else { return null } plenty of times too

I'm not a coding horror perpetrator, BUT ...

IIRC the .NET hashmap actually throws an exception when a key is not present. So for someone coming from that neck of the woods that would be good code.

There is a TryGet method too, so you don't actually have to write the above every time. But I don't know how long it's been there

GABA ghoul
Oct 29, 2011

raminasi posted:

I just looked this up because I was curious and it was introduced in .NET Framework 2.0, which was 2005.

That's when they introduced generics, so I guess it has always been there.

Now that I think about it, their approach to throwing an exception in case of a missing keys actually makes a lot of sense. For value types anything a GetKey method could return for a missing key could be confused for an actual value, i.e. am I getting back a 0 because the inventory for this productID is 0 or because the productID is not in the hashmap at all?

GABA ghoul
Oct 29, 2011


Htbh, but I would not approve this in a pull request because it clearly doesn't work if you pass a 0(which is even). AI still has a long way to go before it can replace a real developer

GABA ghoul
Oct 29, 2011

leper khan posted:

or any negative numbers, despite signed input

I think there is a very clever solution for this that could save a lot of work

code:

if (number < 0)
 number = int.Parse(number.ToString().Replace('-', ''))

GABA ghoul
Oct 29, 2011

Qwertycoatl posted:

I have literally seen
code:
is_odd = (number / 2).ToString().Contains('.')

:love:

I love this so much, especially because it doesn't even work if you execute it on a German Windows

So here is the correct version that is actually safe to deploy internationally

code:


string decimalSeparator = Directory.Exists("C:\\Programme") ? "," : ".";

is_odd = (number / 2).ToString().Contains(decimalSeparator);

GABA ghoul
Oct 29, 2011

leper khan posted:

fail PR. use "." and the invariant cultureinfo

whoa, buddy, what kind of thread do you think this is? We don't look kindly on that kind of code in here :toughguy:

GABA ghoul
Oct 29, 2011

Jen heir rick posted:

You can also set the current culture to en-us:
code:
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("EN-US");
is_odd = (number / 2).ToString().Contains(decimalSeparator);
I see no downsides.

Totally agree. Just think of all the types of exciting heisenbugs you can generate when one or two out of 30+ threads in the System.Threading thread pool suddenly have a different culture from the rest(but only occasionally, because of the random life time of a pool thread of anything between milliseconds and forever). It could drive developers insane for months trying to diagnose this

GABA ghoul
Oct 29, 2011

QuarkJets posted:

Python code:


This good solid code, but personally I would go with a more modern functional programming approach


Python code:


class Monad(Enum):
    NO = 1
    YES = 2
    MAYBE = 3

def is_even(x):
    return Monad.MAYBE

Adbot
ADBOT LOVES YOU

GABA ghoul
Oct 29, 2011

Bruegels Fuckbooks posted:

I thought about it a bit and I think we can use tensorflow for this application. This may or may not work depending upon your CPU type and CUDA version present on your system - when you generate the model, it's important to take a vm of your working environment so you can ensure consistent results.

code:
import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Dense(128, activation='relu', input_shape=(1,)),
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(1024, activation='relu'),
    tf.keras.layers.Dense(2048, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

X_train = tf.range(0, 1000000, dtype=tf.float32)
y_train = tf.cast(X_train % 2 == 0, tf.float32)

model.fit(X_train, y_train, epochs=10, batch_size=32)

def is_even(number):
    number = tf.cast(number, tf.float32)
    prediction = model.predict([[number]])
    return tf.round(prediction) == 1

print(is_even(2))  # True
print(is_even(3))  # False
print(is_even(42))  # True
print(is_even(777))  # False

lmao

It should throw an UnsafeNumberContentDetected exception if you pass "80085" to it. Children might be using it

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