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
Bhaal
Jul 13, 2001
I ain't going down alone
Dr. Infant, MD

Bhaal posted:

This isn't code (yet) but our recent new hire recently asked me "what does break do?". Then, after explaining, he followed-up with asking a clarification of how continue works differently than break. I was 4 timezones away on vacation when the interview sprung up, so instead of inconveniencing me for a technical interview while I was away the CTO handled the whole thing.

I guess I'd rather him asking questions like that instead of carrying on without that understanding, but he has a masters in CS :psyduck:
So as an update he just double checked again today to cofirm that break would infact break him out of a loop (I think he was unclear on whether it would continue execution or act like return or something), with a followup question about what if you're in TWO loops? Masters in CS with emphasis on java/c#/php. How. HOW?

He's already been checking in code on little starter tasks that were just random cleanup bits I haven't gotten around to, and at least from what I've seen it's all pretty much okay, but I just... I just ... :negative:

Am I just being too inconsiderate or condescending? I've got my share of gaps, too, and certainly had plenty more when I was starting out. Like, I wasn't familiar with what a closure was until I blundered into it about 3 months into my first job doing web development, thinking I found a js bug or something. But this was not having any solid prior experience in js, not, you know, coming out of a ~6 year academic career that focused on languages with closures.

And, I mean, BREAK? It just seems so fundamental.

Adbot
ADBOT LOVES YOU

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Some professors really rail on break and continue being evil in the same sense that some people are obsessed with Single Point of Return. It is possible, although unlikely, that he was merely trained around the use of those facilities.

WTF is a "Masters in CS with emphasis on java/C#/php", though? If a CS program is defined by the languages you use during coursework you may have a problem. Where I come from, Computer Science is a branch of applied mathematics.

Bhaal
Jul 13, 2001
I ain't going down alone
Dr. Infant, MD

Internet Janitor posted:

Some professors really rail on break and continue being evil in the same sense that some people are obsessed with Single Point of Return. It is possible, although unlikely, that he was merely trained around the use of those facilities.

WTF is a "Masters in CS with emphasis on java/C#/php", though? If a CS program is defined by the languages you use during coursework you may have a problem. Where I come from, Computer Science is a branch of applied mathematics.
Sorry, that was my bad shorthand way of saying in addition to school he's held a number of student jobs and had a hand in several remunerated projects that used those languages. Plus the first 2 or so years of most programs tend to focus on a particular language (usually procedural and OO-friendly), in his case (and mine) it was java.

I guess I can see how he might not be that familiar with break/continue due to the philosophy of his profs, but I mean it would seem pretty bad for someone teaching around a language like java to avoid explaining some pretty important keywords even if they want to attach a rider saying to avoid using them.

That, and if were me I'd at least give google a quick spin before asking around. I guess it's a mixture of personality differences. I'll only ask someone after I give a 10-30 minute attempt (depending on what it is) to figure it out on my own, he seems to just spout out questions as soon as he runs into the problem and/or if it just piques his curiosity. He's asked a lot of very strange, random questions, most not dealing with programming stuff but bizarre nonetheless (asked if I own a farm, we live in a huge city). One of my friends is starting to think he's actually this master of deadpan trolling or something.

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

pseudorandom name posted:

Don't forget that string literals used to be writable.

Was this pre-C89? I was under the impression that a compiler has always been free in a char *foo = "bar"; situation to put foo in the rodata segment or whatever, whereas char foo[] = "bar"; has always required that foo be on the stack.

Optimus Prime Ribs
Jul 25, 2007

BP posted:

It would allow you to modify the memory storing the object, as long as you can reverse the changes, rather than making a copy of the memory and mutating the copy. This could lead to some memory savings in some cases.

Makes sense and all but from a practical stand-point I just don't get why someone would want to write their code that way.
Whenever I use const it's for things which I know will never change (e.g. if I wanted to store pi in a variable for some reason).

Oh well. People will always be doin' dumb things. v:shobon:v

pseudorandom name
May 6, 2007

Otto Skorzeny posted:

Was this pre-C89? I was under the impression that a compiler has always been free in a char *foo = "bar"; situation to put foo in the rodata segment or whatever, whereas char foo[] = "bar"; has always required that foo be on the stack.

Modification of string literals is still undefined in the current spec. Older compilers used to stick them in the .data section.

SlightlyMadman
Jan 14, 2005

BP posted:

It would allow you to modify the memory storing the object, as long as you can reverse the changes, rather than making a copy of the memory and mutating the copy. This could lead to some memory savings in some cases.

Of course, this isn't the meaning of the const keyword and using it as such could lead to problems in certain situations (multithreaded/read-only memory).

But clearly, if those circumstances aren't present at the exact moment that I'm writing some code, they're not worth taking into consideration.

PhonyMcRingRing
Jun 6, 2002

Bhaal posted:

So as an update he just double checked again today to cofirm that break would infact break him out of a loop (I think he was unclear on whether it would continue execution or act like return or something), with a followup question about what if you're in TWO loops? Masters in CS with emphasis on java/c#/php. How. HOW?

I don't understand why he didn't just run the code and see what it did instead of playing 20 questions with you.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug
Can we say with confidence that the real horror is the usage of notoriously tricky and subtle languages like C and C++ in 2011?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

SlightlyMadman posted:

But clearly, if those circumstances aren't present at the exact moment that I'm writing some code, they're not worth taking into consideration.

There is, in fact, plenty of code which manipulates objects which are guaranteed to be (1) dynamically allocated and (2) accessed only by a single thread. Often both of those constraints are close to inherent to the domain.

Again, I think this sort of code is very dangerous, particularly with string data, and it's poor style in the sense that it's a potentially-unexpected constraint on the calling code. But if you accept that the caller and callee are already tightly coupled, and your domain has made the above constraints tenable, then I would argue it's not a completely unreasonable interpretation of the const contract.

Zombywuf
Mar 29, 2008

BonzoESC posted:

Can we say with confidence that the real horror is the usage of notoriously tricky and subtle languages like C and C++ in 2011?

Still waiting on that blockbuster game written in Haskell.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

BonzoESC posted:

Can we say with confidence that the real horror is the usage of notoriously tricky and subtle languages like C and C++ in 2011?

There are analogies in any language with mutable state. I can certainly have (say) a List in Java which I pass down the stack with the informal understanding that the callee won't add anything to it. Under such an understanding, it's a point of interpretation whether that means "don't modify it at all" or "you can modify it as long as you undo those changes before you return", and there can be good reasons to do either.

The biggest difference is that C and C++ actually let you formalize (and therefore type-check) the mutability contract, which makes this point of interpretation kindof a question about the language.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
const does not in any way imply thread-safety. Even with the "const means don't mutate the data in any way" interpretation, a const method could still do something like call strtok. In some cases the restore-to-original view of const is even required for thread safety, as it's rather hard to lock a mutex owned by an object without any mutation.

TasteMyHouse
Dec 21, 2006

Zombywuf posted:

Still waiting on that blockbuster game written in Haskell.

or h.264 encoder (where this code was taken from)

Zombywuf
Mar 29, 2008

Plorkyeran posted:

const does not in any way imply thread-safety. Even with the "const means don't mutate the data in any way" interpretation, a const method could still do something like call strtok. In some cases the restore-to-original view of const is even required for thread safety, as it's rather hard to lock a mutex owned by an object without any mutation.

Well you could use mutable or you could use volatile http://drdobbs.com/184403766.

MononcQc
May 29, 2007

Spotted in some high-priced “expert”‘s code by a guy I talk to on IRC:

code:
switch ($retcode)
{
    case -1:
    case -3:
        if ($retcode==-1)
            log("SOME_CODE", "SOME MSG");
        else
            log("SOME_OTHER_CODE", "SOME OTHER MSG");
...

Lhet
Apr 2, 2008

bloop


Here's sample of some image processing code we have at work. Absolutely unmaintainable. I think this was basically copied from paper verbatim, converting things like H2 into hh. Luckily almost all these algorithms are static.

code:
int col = 0;
for (i=0; i<ind-1; i++) {
	int init = indic[i];
	int finn = indic[i+1];
	if (i == 0) init = 0;
	int ttd = 2*(I1.width + I1.height);
	for (int nnp=init; nnp<finn; nnp++) {
		int tts = points[2*nnp] + points[2*nnp+1];
		if (tts < ttd) {
			row = points[2*nnp+1];
			col = points[2*nnp];
			ttd = tts;
		}
	}
	if (Iinp[row+1][col+1] == 0) indic[i] *= -1;
}

for (i=0; i<nn; i++) {
	points[2*i]--;
	points[2*i+1]--;

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug
Saw something along these lines today:

code:
string s = string.Empty;

if (somebool)
{
    s = "foo";
}

{
    s = "bar";
}
I checked source control and it's been like that since 2008. I'm not putting the missing "else" in, that's for sure.

I'm not even going to bother mentioning it to anyone else on the team, or that we could catch poo poo like that really easily by unit testing. They've broken my spirit. I will be the sole person on the team who cares about writing testable, tested code. Then I will find a new job in 6 months.

New Yorp New Yorp fucked around with this message at 02:14 on Oct 22, 2011

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
The programmer just wanted to make sure that string literal fell out of scope after the assignment, that's all. :v:

Captain Cappy
Aug 7, 2008

Ithaqua posted:

Saw something along these lines today:

code:
string s = string.Empty;

if (somebool)
{
    s = "foo";
}

{
    s = "bar";
}
I checked source control and it's been like that since 2008. I'm not putting the missing "else" in, that's for sure.

I'm not even going to bother mentioning it to anyone else on the team, or that we could catch poo poo like that really easily by unit testing. They've broken my spirit. I will be the sole person on the team who cares about writing testable, tested code. Then I will find a new job in 6 months.

Seems like you caught that poo poo pretty easily by code reviewing. Why add the extra step of unit testing?

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Captain Cappy posted:

Seems like you caught that poo poo pretty easily by code reviewing. Why add the extra step of unit testing?

Because if the original developer had written unit tests, he would've known immediately that there was a problem and it wouldn't have required a second set of eyes to catch.

My viewpoint is that code reviews should be for sanity-checking business rules and architectural decisions, not finding bugs.

Captain Cappy
Aug 7, 2008

Ithaqua posted:

My viewpoint is that code reviews should be for sanity-checking business rules and architectural decisions, not finding bugs.

Why? It seemed to work pretty quickly to find that bug you just posted. Also are you sure he would have known the error immediately, considering he's the one who made it in the first place?

TasteMyHouse
Dec 21, 2006

Captain Cappy posted:

Why? It seemed to work pretty quickly to find that bug you just posted. Also are you sure he would have known the error immediately, considering he's the one who made it in the first place?

what are you saying? if he wrote proper unit tests he would know that his code wasn't functioning the way he meant it to be.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Captain Cappy posted:

Why? It seemed to work pretty quickly to find that bug you just posted. Also are you sure he would have known the error immediately, considering he's the one who made it in the first place?

I'm not saying you won't find bugs in a code review, but I never sit down to do a code review with the intent of finding bugs. I've found bad architectures, missing requirements, completely mis-implemented requirements, solutions that can't possibly scale, correct solutions implemented in mind-bogglingly stupid ways, solutions containing unnecessarily copy-and-pasted code, but very rarely any "i forgot an else statement :downs:" bugs.

A good unit test suite would catch something like that because there would be an explicit test case for both scenarios (e.g. "When_Somebool_Is_True_Then_S_Is_Foo" and "When_Somebool_Is_False_Then_S_Is_Bar" or "When_Somebool_Is_False_Then_S_Is_Not_Foo"). Of course, that's assuming the test is important enough to be written in the first place. In this case, it doesn't look like it was important.

raminasi
Jan 25, 2005

a last drink with no ice

TasteMyHouse posted:

what are you saying? if he wrote proper unit tests he would know that his code wasn't functioning the way he meant it to be.

How do you know he didn't mean to do it that way :colbert:

PrBacterio
Jul 19, 2000

rjmccall posted:

The strchr signature is a wart that is basically required by a lack of expressivity in the C type system.
If strchr had the seemingly more appropriate type signature returning a const pointer, wouldn't it still be possible to write (e.g.)
code:
    extern const char *strchr(const char *, int);
    foo[strchr(foo, bar)-foo]=baz;
    char *qux=&foo[strchr(foo, bar)-foo];
or something along those lines? Sure it's more wordy but it does show that this can, in fact, be expressed correctly even in C's limited type system.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
I meant that the return type really ought to be const-qualified if and only if the first argument is. Given that that's not expressible in the type system, I tend to side with the committee that it's better to lose const-safety than to make a *very* common task in C string processing this awkward.

ETA: but you're right that that's a legal workaround

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.
newlib is Satan's own library. I present as evidence that while sin() is in math.h, it is not in libm.a. :commissar:

Hammerite
Mar 9, 2007

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

MononcQc posted:

Spotted in some high-priced “expert”‘s code by a guy I talk to on IRC:

code:
switch ($retcode)
{
    case -1:
    case -3:
        if ($retcode==-1)
            log("SOME_CODE", "SOME MSG");
        else
            log("SOME_OTHER_CODE", "SOME OTHER MSG");
...

Whether this is bad or not would seem to depend on whether there are any other statements before the next case.

Ursine Catastrophe
Nov 9, 2009

It's a lovely morning in the void and you are a horrible lady-in-waiting.



don't ask how i know

Dinosaur Gum

Hammerite posted:

Whether this is bad or not would seem to depend on whether there are any other statements before the next case.

That and whether the language supports statements between fallthroughs. It's been awhile, but I seem to recall
code:
switch ( variable ){
  case 1:
    statement;
  case 2:
     ....
  break;
}
would throw an error in C#, but not
code:
switch ( variable ){
  case 1:
  case 2:
     ....
  break;
}
Disclaimer: It's been a long time, and while I definitely recall the stupidity of the inability to handle switches like that, I don't recall 100% if it was C# or not, and if it was, I don't know if it's been fixed at some point in the last 6 years.

plushpuffin
Jan 10, 2003

Fratercula arctica

Nap Ghost

OriginalPseudonym posted:

That and whether the language supports statements between fallthroughs. It's been awhile, but I seem to recall
code:
switch ( variable ){
  case 1:
    statement;
  case 2:
     ....
  break;
}
would throw an error in C#, but not
code:
switch ( variable ){
  case 1:
  case 2:
     ....
  break;
}
Disclaimer: It's been a long time, and while I definitely recall the stupidity of the inability to handle switches like that, I don't recall 100% if it was C# or not, and if it was, I don't know if it's been fixed at some point in the last 6 years.

It is C#, and it hasn't been fixed. However, you can accomplish the fall through by using a goto with the name of one of the other labels in the switch.

Zhentar
Sep 28, 2003

Brilliant Master Genius
Considering that the behavior was an intentional design decision, there's nothing to fix.

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

Zhentar posted:

Considering that the behavior was an intentional design decision, there's nothing to fix.

Sure there is, that thing is just a wart rather than a bug :)

Ursine Catastrophe
Nov 9, 2009

It's a lovely morning in the void and you are a horrible lady-in-waiting.



don't ask how i know

Dinosaur Gum

Zhentar posted:

Considering that the behavior was an intentional design decision, there's nothing to fix.

I admit to curiousity as to why that would be something to intentionally put in.


Otto Skorzeny posted:

Sure there is, that thing is just a wart rather than a bug :)

I believe the technical term is "feature" :v:

pseudorandom name
May 6, 2007

There no possibility that you'll forget a break and accidentally have a fallthrough in C#.

Yes, that's literally the reason.

Zhentar
Sep 28, 2003

Brilliant Master Genius
That's not the only scenario it's there to prevent, but yes, it's only done that way to avoid accidentally screwing up (and, with goto case, to make your intent explicit). The reasoning being that intentional fallthrough is the uncommon case.

As a side bonus, it prevents people who are cool with fallthrough but think goto case is an abomination because it starts with goto from trying to be too clever for their own good.

Ursine Catastrophe
Nov 9, 2009

It's a lovely morning in the void and you are a horrible lady-in-waiting.



don't ask how i know

Dinosaur Gum

Zhentar posted:

That's not the only scenario it's there to prevent, but yes, it's only done that way to avoid accidentally screwing up (and, with goto case, to make your intent explicit). The reasoning being that intentional fallthrough is the uncommon case.

As a side bonus, it prevents people who are cool with fallthrough but think goto case is an abomination because it starts with goto from trying to be too clever for their own good.

To be frank, C# was taught as my college's preferred programming language, and none of the professors taught, mentioned, or used goto. (Neither did the textbooks they used.) It wasn't until this thread, years after the fact, that I found out C# even has goto.

So yeah, I assume they all fell into the "goto is a four-letter word" category.

shrughes
Oct 11, 2008

(call/cc call/cc)

OriginalPseudonym posted:

To be frank, C# was taught as my college's preferred programming language, and none of the professors taught, mentioned, or used goto. (Neither did the textbooks they used.) It wasn't until this thread, years after the fact, that I found out C# even has goto.

So yeah, I assume they all fell into the "goto is a four-letter word" category.

It's more like they don't want all the stupid scrubs writing goto statements instead of learning to use while loops and for loops.

Ursine Catastrophe
Nov 9, 2009

It's a lovely morning in the void and you are a horrible lady-in-waiting.



don't ask how i know

Dinosaur Gum

shrughes posted:

It's more like they don't want all the stupid scrubs writing goto statements instead of learning to use while loops and for loops.

You would think about the time they're teaching the class about delegates, they might mention at some point "Oh yeah by the way..."

Adbot
ADBOT LOVES YOU

EpicCodeMonkey
Feb 19, 2011

Zhentar posted:

That's not the only scenario it's there to prevent, but yes, it's only done that way to avoid accidentally screwing up (and, with goto case, to make your intent explicit). The reasoning being that intentional fallthrough is the uncommon case.

As a side bonus, it prevents people who are cool with fallthrough but think goto case is an abomination because it starts with goto from trying to be too clever for their own good.

I never understood why the language (or other C-derived languages, for that matter) don't make fall-through legal in switch statements, but require that the last statement in each case be "break", "return", or "continue" - the latter specifying a fallthrough. If anything else is encountered it would be a syntax error. The only downside would be that:

code:
switch
{
     case 1:
     case 2:
          DoSomething();
}
Would be illegal both due to the missing break at the end, and the two case statements together with no code in between, but you could special-case that. This way there's no way a programmer can accidentally fallthrough, but it's still possible if that is desired.

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