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
Lexical Unit
Sep 16, 2003

My co-worker came to me today asking how he could make it a compile time error to access an element in a map via operator[]() for when that element didn't exist in the map.

He explained the bigger problem to me like this: "I want to be able to be like map[blah] or whatever but the problem is that if there's no entry for blah then it will insert an invalid pointer into my map!"

I told him to check if blah exists in the map before trying to access map[blah]. He actually laughed at me and told me that would be ludicrous.

He is currently trying to figure out how to write a "smart" pointer that will throw an exception when default constructed as a replacement for putting bare pointers in a map.

Adbot
ADBOT LOVES YOU

Crazy RRRussian
Mar 5, 2010

by Fistgrrl

Lexical Unit posted:

My co-worker came to me today asking how he could make it a compile time error to access an element in a map via operator[]() for when that element didn't exist in the map.

He explained the bigger problem to me like this: "I want to be able to be like map[blah] or whatever but the problem is that if there's no entry for blah then it will insert an invalid pointer into my map!"

I told him to check if blah exists in the map before trying to access map[blah]. He actually laughed at me and told me that would be ludicrous.

He is currently trying to figure out how to write a "smart" pointer that will throw an exception when default constructed as a replacement for putting bare pointers in a map.

Where do you work at?

Lexical Unit
Sep 16, 2003

Your tax dollars at work folks (if you're American that is).

lumberjack4
May 14, 2010

Lexical Unit posted:

My co-worker came to me today asking how he could make it a compile time error to access an element in a map via operator[]() for when that element didn't exist in the map.

He explained the bigger problem to me like this: "I want to be able to be like map[blah] or whatever but the problem is that if there's no entry for blah then it will insert an invalid pointer into my map!"

I told him to check if blah exists in the map before trying to access map[blah]. He actually laughed at me and told me that would be ludicrous.

He is currently trying to figure out how to write a "smart" pointer that will throw an exception when default constructed as a replacement for putting bare pointers in a map.

Has he ever written error handling in any of his code?

Lexical Unit
Sep 16, 2003

lumberjack4 posted:

Has he ever written error handling in any of his code?
No of course not that would be ludicrous :v:

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Lexical Unit posted:

Your tax dollars at work folks (if you're American that is).

Can I work there? I promise I won't ask retarded questions about hash maps.

Lexical Unit
Sep 16, 2003

No sorry there's a currently a hiring freeze in effect. I'm afraid you'll have to go ask non-retarded questions elsewhere for now.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
Ah well, worth a shot. I'm the only one left after the last round of layoffs so there's no one left to ask non-retarded questions.

BigRedDot
Mar 6, 2008

Lexical Unit posted:

hiring freeze
Not true! There's at least some openings for "Tactical System Administrator" and "R&D Sensor Prediction Software Developer" still open.

BigRedDot fucked around with this message at 23:34 on May 21, 2010

Lexical Unit
Sep 16, 2003

Lexical Unit fucked around with this message at 23:16 on Mar 4, 2020

Captain Capacitor
Jan 21, 2008

The code you say?

Lexical Unit posted:

Run-time map nonsense

I probably wouldn't be able to do the same amount of work, but certainly no less. I take up less room and ask 94% less stupid questions.

Please consider my CV attached.

==========
Resume.wpd

Big Nubbins
Jun 1, 2004
I recently got the opportunity to refactor horrible Javascript I wrote right after I was hired. The code was for a website whose layout was to change dynamically between 4 different standard layout templates (home, an information page, an image gallery, and a horizontal accordion-style image gallery with categories). We used Prototype and scriptaculous for animating the layout transitions. I'd also like to mention that I had only a general knowledge of Javascript but wrote maybe 300 lines of JS prior to being hired, and this was my first project.

The transition function body was something like this:
code:
if (oldPageType == 'home' && newPageType == 'info') { ... } // animate from the home to info page
else if (oldPageType == 'home' && newPageType == 'gallery') { ... }
else if (oldPageType == 'home' && newPageType == 'accordiongallery') { ... }
else if (oldPageType == 'info' && newPageType == 'home') { ... }
...
You get the picture. This worked fine even though there was a chunk of 16 if..else statements in the function. The page worked and the client was happy. I think it ended up being something like 400 lines of Javascript. Now the client wants another gallery and I'm told by the boss to "just get it done" since we were already blowing the budget. The implementation of the initial gallery was a number of functions, mainly to handle animating it and displaying the captions, but it was highly intertwined with specific DOM elements. So instead of reworking my gallery code to allow it to be ignorant of the specific DOM elements and just work with whatever gallery was the "current" one, I simply copy-pasted all my gallery code, renaming the functions and state vars to work with different elements. Now my transition functions involving galleries looked like this:
code:
else if (oldPageType == 'home' && newPageType == 'gallery') {
    if (gallery == 'staff') { ... }
    else if (gallery == 'other') { ... }
}
Then they added another layout type, so it grew essentially from 16 if..else statements to 32. It was the quickest way to get the job done, but was also nearly impossible to debug, as I had to monitor a retarded amount of state variables. It ended up being 1700 lines of Javascript when the client was finished adding new galleries and other junk.

In the refactor, I wrote a nice, clean gallery class that handles everything and even though it added about 400 lines to the project, it replaced 1400 lines of horrifying code just to support the different galleries. It never felt so good to hit delete on huge swaths of lovely, confusing code that I wrote myself years prior.

Big Nubbins fucked around with this message at 16:11 on May 25, 2010

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Baby-sized horror that popped up today:
code:
char name[MAX_SIZE];
memset(name, 0, MAX_SIZE);
strcat(name, "Default");

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Mustach posted:

Baby-sized horror that popped up today:
code:
char name[MAX_SIZE];
memset(name, 0, MAX_SIZE);
strcat(name, "Default");

I don't get it.

Vanadium
Jan 8, 2005

Should be char name[MAX_SIZE] = "Default";, I guess.

b0lt
Apr 29, 2005

Flobbster posted:

That one's easy because there's only one correct answer: use the same conventions as the language and standard library that you're using. Some examples:

C++ with STL: underscores
C++ with MFC: slit wrists camel case
Ruby: underscores
Java: camel case with initial minuscule
Objective-C: camel case with initial minuscule
C#: camel case with initial majuscule

In the event that you're using a combination of a language and library where the conventions differ, (1) the library sucks for doing that, and (2) I don't know, pick one and stick with it.

PHP: :confused: :suicide:

ninjeff
Jan 19, 2004

code:
Private Sub ReleaseObject(obj)
    If Not (obj Is Nothing) Then
        Set obj = Nothing
    End If
End Sub
This function is like an onion; it stinks and makes you cry.

The real horror is that I didn't notice it until today after a few weeks of working on this horrible program.

manero
Jan 30, 2006

code:
  function replaceAll( str, searchTerm, replaceWith, ignoreCase )
  {
    var regex = "/"+searchTerm+"/g";
    if( ignoreCase ) regex += "i"; 
    return str.replace( eval(regex), replaceWith );
  }
This function is used in exactly three places directly above the function definition, none of which actually change the ignoreCase parameter.

Rohaq
Aug 11, 2006

manero posted:

code:
  function replaceAll( str, searchTerm, replaceWith, ignoreCase )
  {
    var regex = "/"+searchTerm+"/g";
    if( ignoreCase ) regex += "i"; 
    return str.replace( eval(regex), replaceWith );
  }
This function is used in exactly three places directly above the function definition, none of which actually change the ignoreCase parameter.
They wrote a function that essentially has an extra argument that adds an 'i' to the end of the regex?

Jesus :psyduck:

jandrese
Apr 3, 2007

by Tiny Fistpump

Vanadium posted:

Should be char name[MAX_SIZE] = "Default";, I guess.

Is that guaranteed to zero out the rest of the string? If I was paranoid enough about the rest of the string, I might go and zero it out by hand first like that too. FWIW, I just tested it with gcc and it did zero out the string, so it's probably alright.

Vanadium
Jan 8, 2005

Yeah, it is, if you initialise an array but do not give values for all the elements, the remaining ones get set to zero.

Parantumaton
Jan 29, 2009


The OnLy ThInG
i LoVe MoRe
ThAn ChUgGiNg SeMeN
iS gEtTiNg PaId To Be A
sOcIaL MeDiA sHiLl
FoR mIcRoSoFt
AnD nOkIa
Is it OK to post it as a pre-emptive coding horror what Sun/Oracle's Lambda implementation for JDK7 will look like? Because seriously, look at this (from here):
code:
public static <T, U> T exec(TU<T, U> lambda, U x) {
    return lambda.foo(x);
}
/* ... */
int x = LambdaCapture01.<Integer,Integer>exec(#(Integer x)(x + N), 3);
/* ... */
int x2 = LambdaCapture01.<Integer,Integer>exec(#(Integer x)(x + LambdaCapture01.this.n + N), 3);

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Well, with any luck lambdas won't make it into the Java7 release and we can breathe easy for a few more years.

VVV oh, jesus gently caress. :gonk:

Internet Janitor fucked around with this message at 19:31 on May 30, 2010

Parantumaton
Jan 29, 2009


The OnLy ThInG
i LoVe MoRe
ThAn ChUgGiNg SeMeN
iS gEtTiNg PaId To Be A
sOcIaL MeDiA sHiLl
FoR mIcRoSoFt
AnD nOkIa

Internet Janitor posted:

Well, with any luck lambdas won't make it into the Java7 release and we can breathe easy for a few more years.

Sorry, the link I provided above is a repo push report from the official JDK7 Mercurial repository. They're in. In that form.

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

Parantumaton posted:

Is it OK to post it as a pre-emptive coding horror what Sun/Oracle's Lambda implementation for JDK7 will look like? Because seriously, look at this (from here):
code:
public static <T, U> T exec(TU<T, U> lambda, U x) {
    return lambda.foo(x);
}
/* ... */
int x = LambdaCapture01.<Integer,Integer>exec(#(Integer x)(x + N), 3);
/* ... */
int x2 = LambdaCapture01.<Integer,Integer>exec(#(Integer x)(x + LambdaCapture01.this.n + N), 3);

Err, that's not so bad, I don't think? The actual lambda part is just #(Integer x)(x + N), so the syntax is basically #(parameter list)(body).

The rest of that junk looks like they're just explicitly qualifying their class when they call the exec helper function that they've written in their tests. The only that I'm not clear about is why the interface TU<T, U> that they've declared is necessary, or why they're invoking the lambda using a method on that interface instead of just doing something with the lambda itself.

Hopefully things will become clearer further into development, but given the history of Java taking good ideas for features and loving them up completely, who knows!

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
I'd argue that lambda expressions are not a good idea for Java. They don't improve the expressive power of the language- any sane implementation is just going to be sugar for anonymous classes and save us a few dozen characters, at the cost of adding new, irregular syntax.

Historically, Java maintainers have been very conservative about tossing in new features, but Java7 is shaping up to look like C# envy.

Opinion Haver
Apr 9, 2007

Internet Janitor posted:

I'd argue that lambda expressions are not a good idea for Java. They don't improve the expressive power of the language- any sane implementation is just going to be sugar for anonymous classes and save us a few dozen characters, at the cost of adding new, irregular syntax.

Historically, Java maintainers have been very conservative about tossing in new features, but Java7 is shaping up to look like C# envy.
Not to mention, what the gently caress is up with #?

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

Internet Janitor posted:

I'd argue that lambda expressions are not a good idea for Java. They don't improve the expressive power of the language- any sane implementation is just going to be sugar for anonymous classes and save us a few dozen characters, at the cost of adding new, irregular syntax.

I've wasted enough time writing anonymous class instantiations of one-method interfaces for things like event listeners to say that you're absolutely wrong for thinking this way :v: It's even worse if you're writing a class that exposes some kind of in listener functionality and you have to define a pointless interface on top of it.

There are a lot of places where I would like to write code more naturally using higher-order functions and that's impossible without declaring/instantiating a bunch of pointless interfaces. Wouldn't it be nice if they added nice collection manipulation functions that took lambdas as arguments, something that would be unwieldy with anonymous types? You really think there's nothing gained by changing something like this:

code:
List<T> newList = someList.map(new SomeStupidInterface<T>() {
    public T someStupidMethod(T elem)
    {
        return elem.someOperation();
    }
});
to something like this?

code:
List<T> newList = someList.map(#(T elem)(elem.someOperation()));
Java should have C# envy, because C# shows how to do a Java-like language that isn't complete poo poo.

Shumagorath
Jun 6, 2001
The last time I did any serious amount of Java coding our school was locked on 1.4.2 and the syntax seems like it's way worse now. Of course, the JVM also had bugs if you had your CWD on your PATH variable so I guess I should count my blessings.

ColdPie
Jun 9, 2006

Shumagorath posted:

The last time I did any serious amount of Java coding our school was locked on 1.4.2 and the syntax seems like it's way worse now. Of course, the JVM also had bugs if you had your CWD on your PATH variable so I guess I should count my blessings.

1.5 is imo the minimum "good" Java. It introduced automatic [un]boxing, generics (you can laugh, but the language is much worse without them), and the enhanced for loop, all of which I feel are essential Java features. I hate when I have to work in 1.4 or below environments because I rely on these features so much. But I don't have to use Java for my job anymore, so I haven't touched it in ages

I like to keep track of where it goes. Even if it's not always great to use, I think it's fun to watch the project evolve.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Internet Janitor posted:

I'd argue that lambda expressions are not a good idea for Java. They don't improve the expressive power of the language

Neither does any feature once you have Turing completeness.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Avenging Dentist posted:

Neither does any feature once you have Turing completeness.

This isn't strictly true, as there are programming language features which are not related to the primitive computational model, and thus aren't always reachable through pure Turing programming. For example, the SK combinator language is Turing-complete, but has no ability to perform IO with the outside world, so adding additional combinators which allow IO is a genuine increase in expressivity.

That said, lambdas are not such a feature for Java.

Crazy RRRussian
Mar 5, 2010

by Fistgrrl
What exactly do we mean by "expressivity" here?

npe
Oct 15, 2004
Found this bit of java code to left pad a number with zeros...

code:
String ss = "0000000000000000" + Integer.toString(suffix); 
sb.append(ss.substring(ss.length() - pad_size));

shrughes
Oct 11, 2008

(call/cc call/cc)

npe posted:

Found this bit of java code to left pad a number with zeros...

code:
String ss = "0000000000000000" + Integer.toString(suffix); 
sb.append(ss.substring(ss.length() - pad_size));

That's not a horror.

TagUrIt
Jan 24, 2007
Freaking Awesome

shrughes posted:

That's not a horror.

code:
sb.append(String.format("%10d", suffix));
Edit: Okay, not a horror. Just an odd piece of code.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

shrughes posted:

That's not a horror.

pad(10, -1)

pad(20, 0)

defmacro
Sep 27, 2005
cacio e ping pong

yaoi prophet posted:

Not to mention, what the gently caress is up with #?

At least one other language, Clojure, uses # as a reader macro for declaring anonymous functions, the following two expressions are equivalent:
code:
user=> (map (fn [x] (* x x)) [1 2 3 4])
(1 4 9 16)
user=> (map #(* % %) [1 2 3 4])
(1 4 9 16)
% is used for an argument if there is only one and %1, %2, ..., %n are used for multiple arguments. Quite succinct syntax... not so much for Java unfortunately.

OddObserver
Apr 3, 2009

manero posted:

code:
  function replaceAll( str, searchTerm, replaceWith, ignoreCase )
  {
    var regex = "/"+searchTerm+"/g";
    if( ignoreCase ) regex += "i"; 
    return str.replace( eval(regex), replaceWith );
  }
This function is used in exactly three places directly above the function definition, none of which actually change the ignoreCase parameter.

Bonus points for using eval.

Adbot
ADBOT LOVES YOU

Karanth
Dec 25, 2003
I need to finish Xenogears sometime, damn it.

defmacro posted:

At least one other language, Clojure, uses # as a reader macro for declaring anonymous functions, the following two expressions are equivalent:
code:
user=> (map (fn [x] (* x x)) [1 2 3 4])
(1 4 9 16)
user=> (map #(* % %) [1 2 3 4])
(1 4 9 16)
% is used for an argument if there is only one and %1, %2, ..., %n are used for multiple arguments. Quite succinct syntax... not so much for Java unfortunately.

# is also used in javadoc tags such as {@link ClassName#methodName()}

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