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
Progressive JPEG
Feb 19, 2003

Crazy Mike posted:

As my office switches to .NET, eventually we are going to have a talk about new naming conventions. Our current variable naming convention is <scope><datatype>_variablename like string ls_user_name or long ll select_count or boolean ib_read_only. What is the argument against having scope and datatype in your variable names?

It's arguable that if you have so many variables/members that you need to track their types that way, then it's a sign you need to refactor. If you aren't able to read a variable or member name and immediately tell what its doing (either due to poor naming or an abundance of orthogonal purposes in the same component), then it's likely time to think about breaking things down into functional subcomponents.

Adbot
ADBOT LOVES YOU

mobby_6kl
Aug 9, 2009

by Fluffdaddy

Titan Coeus posted:

This is the case. Page 165.

And if we're :spergin: about C, main returns 0 implicitly as of C99, and functions used to be implicitly int up until C99 (at least, I think that's when both changes were made), so you can usually get away with this in a single program:
C++ code:
main() {
    printf("Hello, world!");
}

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

Aleksei Vasiliev posted:

There are Java implementations that require you to import anything in java.lang?

e: import java.lang.System.out; isn't even a valid import

It used to be the case (Java 1.0 - 1.1 era IIRC) that you had to import from java.lang. I specifically remember having to import java.lang.String during CS class in uni. I hated java for years due to 1.1.

hobbesmaster
Jan 28, 2008

mobby_6kl posted:

And if we're :spergin: about C, main returns 0 implicitly as of C99, and functions used to be implicitly int up until C99 (at least, I think that's when both changes were made), so you can usually get away with this in a single program:
C++ code:
main() {
    printf("Hello, world!");
}

Ironic that the "cpp" code flag is there because that isn't valid C++. ;)

NtotheTC
Dec 31, 2007


Otto Skorzeny posted:

I need* to...

I hate when people do this and forget to add the other *. I spend hours of my life searching for it.

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

NtotheTC posted:

I hate when people do this and forget to add the other *. I spend hours of my life searching for it.

*Since the C compiler will assume that any function that lacks a prototype returns an integer and takes however many arguments it is called with, and the only function I'm calling is printf (and I haven't screwed up its invocation), and a standard version of printf is almost certainly in an object file that will get linked by default, it is very probably that a program that "forgot" to include stdio.h would still compile (with a warning, unless you have passed it flags to treat warnings as errors) and run correctly, even though the include ought to be there.

:)

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
A couple notes on that.

First, if your function is defined with a prototype that takes a float, calling it through an unprototyped function type will probably fail, because default argument promotion (the thing that happens when you pass a variadic or unprototyped argument) turns floats into doubles. This ridiculous special case is brought to you by DIGITAL, which did not want to support float natively and somehow felt that this terrible language hack would help.

Second, your specific example of printf is unportable. C does not guarantee that calling a variadic function through an unprototyped function type will succeed, and indeed, there are platforms which pass variadic arguments differently from how they pass non-variadic arguments; MIPS is the most prominent one. Usually what this means is that variadic arguments are always passed on the stack. In the abstract, that's much smarter than matching the non-variadic convention, because the non-variadic convention usually wants to pass different types of arguments in different registers, and piecing that all together in a way that matches the normal convention requires a gratuitous amount of code in variadic prologues and va_next. The problem is that it potentially breaks ancient code relying on unprototyped function calls, and platform ABI designers are often extremely conservative about that kind of thing.

Zamujasa
Oct 27, 2010



Bread Liar
We interrupt this C++ horror to present:

php:
<?
    $fd = fopen("/var/boss/serv_log.txt", "w");
    fwrite($fd, "log");
    fclose($fd);

    // one hundred lines later (it is a procedural file)
    $fd = fopen("/var/boss/serv_log.txt", "w");
    // no fclose, fwrite, anything.

?>
Boss never fails to impress. :golfclap:

Suspicious Dish
Sep 24, 2011

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

Ah, so that's why code in the X server relies on this.

Do you know why we couldn't get a "number of arguments passed to variadic functions" argument exposed? va_length? It has to be there for va_copy to work.

McGlockenshire
Dec 16, 2005

GOLLOCKS!

Zamujasa posted:

We interrupt this C++ horror to present:

php:
<?
    $fd = fopen("/var/boss/serv_log.txt", "w");
    fwrite($fd, "log");
    fclose($fd);

    // one hundred lines later (it is a procedural file)
    $fd = fopen("/var/boss/serv_log.txt", "w");
    // no fclose, fwrite, anything.

?>
Boss never fails to impress. :golfclap:

For anyone that doesn't get the horror, the w flag is "open this and truncate it before getting a lock," aka "I have no idea how to correctly perform file I/O" and/or "somebody kill the idiot that decided that flag was a good idea."

CCrew
Nov 5, 2007

I ran into this today while trying to work on a recently fired programmers only project.

code:
for(int i = 0; i<n; i++)
{
    case 0:
       stuff
    case 1:
       stuff
    [...]
    case n:
       stuff
}
He was essentially trying to make a dynamic form with different components named things like "txtModel1"-"txtModelN", so he could just update accordingly. Instead I think he got confused and churned out that masterpiece. I've already replaced half his code on this form, and it's all equally nonsensical.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
That one's common enough to get it's own name: http://en.wikipedia.org/wiki/Loop-switch_sequence

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Suspicious Dish posted:

Do you know why we couldn't get a "number of arguments passed to variadic functions" argument exposed? va_length? It has to be there for va_copy to work.

va_copy doesn't copy the actual arguments; it just copies the varargs iteration state. So in the traditional implementation where all arguments are on the stack and va_list is just a char*, it's literally just a pointer assignment.

Basically I'm saying that you can't have a va_length because that is not, in fact, passed as part of any variadic conventions that I know of.

bucketmouse
Aug 16, 2004

we con-trol the ho-ri-zon-tal
we con-trol the verrr-ti-cal
I've actually seen loop-switch before in the wild in a sane context. It was an image filter and it did it to get around having a metric assload of function calls.

Also regarding the hello world chat: I the only one who considers C++ streams a horror in the context of people learning to program?

raminasi
Jan 25, 2005

a last drink with no ice

bucketmouse posted:

I've actually seen loop-switch before in the wild in a sane context. It was an image filter and it did it to get around having a metric assload of function calls.

How on earth is
C++ code:
for (int i = 0; i < steps; ++i) {
    case 0:
        thing1();
        break;
    case 1:
        thing2();
        break;
    case 2:
        thing3();
        break;
    // blah blah blah
}
more sane than
C++ code:
thing1();
thing2();
thing3();

raminasi fucked around with this message at 04:37 on Dec 11, 2012

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
it would be clearer if it was representing something like a state machine, but good languages give you better ways of expressing those anyway. Do not use loop-switch.

hobbesmaster
Jan 28, 2008

Internet Janitor posted:

it would be clearer if it was representing something like a state machine, but good languages give you better ways of expressing those anyway. Do not use loop-switch.

Some of us actually have to write code where you have to implement stuff like duff's device for optimizations. If only every platform had compilers as good as x86...

bucketmouse
Aug 16, 2004

we con-trol the ho-ri-zon-tal
we con-trol the verrr-ti-cal
The case I was thinking of was vanilla C and, now that I think of it, very much a state machine.

Zamujasa
Oct 27, 2010



Bread Liar

McGlockenshire posted:

For anyone that doesn't get the horror, the w flag is "open this and truncate it before getting a lock," aka "I have no idea how to correctly perform file I/O" and/or "somebody kill the idiot that decided that flag was a good idea."

Also the fact that the entirety of the log is literally the word "log". That wasn't a joke or a victim of anonimization, that's really all that's there. :downs:

CCrew
Nov 5, 2007

I assure you there was no point for the loop switch. Glad to know it has a name, it came out of a programmer with 15 years of experience.

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

Internet Janitor posted:

it would be clearer if it was representing something like a state machine, but good languages give you better ways of expressing those anyway. Do not use loop-switch.

What superior alternatives are there to implementing a state machine to loop/switch/control-variable, and thinly veiled variations on this theme like loop/table-of-function-pointers/control-variable?

ToxicFrog
Apr 26, 2008


Otto Skorzeny posted:

What superior alternatives are there to implementing a state machine to loop/switch/control-variable, and thinly veiled variations on this theme like loop/table-of-function-pointers/control-variable?

The one that comes immediately to mind is each state is a function, state transitions are a tail call.

If you don't have tail call optimization you can use something like clojure's trampoline which is at least a bit nicer to use than a loop + external control variable.

Crazy Mike
Sep 16, 2005

Now with 25% more kimchee.

CCrew posted:

I assure you there was no point for the loop switch. Glad to know it has a name, it came out of a programmer with 15 years of experience.

What does it take to fire a programmer with 15 years of experience? Which straw broke that camel's back?

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
You could argue that almost any FSM implementation will compile into the equivalent of either a nest of gotos or a loop-switch, but there are definitely better ways of specifying them.

From the gamedev side of things, UnrealScript is slightly interesting, although it really provides Pushdown Automata rather than FSMs. GOOL had first-class FSM support in ways that could be added to Lisps.

In Forth you can make a DSL which resembles a state transition table, which has the significant advantage over many other approaches that at a glance you can tell the set of transition rules is exhaustive:
code:
4 WIDE FSM: <Fixed.Pt#>
\ input:  |  other?  |  num?   |  minus?  |   dp?     |
\ state:  ---------------------------------------------
   ( 0 )     DROP >0    EMIT >1   EMIT >1     EMIT >2
   ( 1 )     DROP >1    EMIT >1   DROP >1     EMIT >2
   ( 2 )     DROP >2    EMIT >2   DROP >2     DROP >2 ;
If your target language doesn't have support for this kind of metaprogramming you could use a tool like
Ragel to specify your FSM as a regular language.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
A loop switch sequence isn't the same as making a state machine because you're switching on the loop variable, not a state variable (well ok it's a trivial state machine that doesn't need a loop). Making a state machine with a loop is fine, implementing a known series of steps using a switch within a for loop isn't.

Presto
Nov 22, 2002

Keep calm and Harry on.

bucketmouse posted:

Also regarding the hello world chat: I the only one who considers C++ streams a horror in the context of people learning to program?
I think they're a horror in any context.

nielsm
Jun 1, 2009



One of these is definitely bad, the other is just kinda dumb:

C++ code:
for (int i = 0; i < 3; i++) {
  switch (i) {
    case 0:
      func1();
      break;
    case 1:
      func2();
      break;
    case 2:
      func3();
      break;
  }
}
C++ code:
for (int i = 0; i < 15; i++) {
  switch (i % 3) {
    case 0:
      func1();
      break;
    case 1:
      func2();
      break;
    case 2:
      func3();
      break;
  }
}
(The latter would effectivly become Duff's Device if you initialised i to something else than 0 and/or terminated at a non-multiple of 3.)

hobbesmaster
Jan 28, 2008

Presto posted:

I think they're a horror in any context.

You're going to have to elaborate on that one.

PrBacterio
Jul 19, 2000

Presto posted:

I think they're a horror in any context.
The same goes for C++ string handling, where it isn't even possible to convert a number into a string with standard C++ functions except by using a string stream. What ought to be a single line of code
C++ code:
std::string s = std::string( some_float_value );
thus becomes three or more:
C++ code:
std::ostringstream buffer;
buffer << some_float_value;
std::string s = buffer.str();
:gonk:

ToxicFrog
Apr 26, 2008


nielsm posted:

C++ code:
for (int i = 0; i < 15; i++) {
  switch (i % 3) {
    case 0:
      func1();
      break;
    case 1:
      func2();
      break;
    case 2:
      func3();
      break;
  }
}
(The latter would effectivly become Duff's Device if you initialised i to something else than 0 and/or terminated at a non-multiple of 3.)

Isn't that just equivalent to:

C++ code:
for (int i = 0; i < 5; i++) {
  func1();
  func2();
  func3();
}
?

Polio Vax Scene
Apr 5, 2009



Holy christ on a cracker

code:

		json = new Object();
		json.bc = new Object();
		json.bc.password = "XXX";
		json.bc.userId = "000XXX";
		json.bc.BackgroundSearchPackage = new Object();
		json.bc.BackgroundSearchPackage.ReferenceId = new Object();
		json.bc.BackgroundSearchPackage.ReferenceId.IdValue = new Object();
		json.bc.BackgroundSearchPackage.ReferenceId.IdValue._ = "204";
		json.bc.BackgroundSearchPackage.PersonalData = new Object();
		json.bc.BackgroundSearchPackage.PersonalData.PersonName = new Array();
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0] = new Object();
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].GivenName = new Object();
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].GivenName._ = "Test";
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].MiddleName = new Object();
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].MiddleName._ = "Test";
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].FamilyName = new Array();
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].FamilyName[0] = new Object();
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].FamilyName[0].primary = "true";
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].FamilyName[0]._ = "Last";
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].FamilyName[1] = new Object();
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].FamilyName[1].primary = "false"
		json.bc.BackgroundSearchPackage.PersonalData.PersonName[0].FamilyName[1]._ = "Lastname";

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

ToxicFrog posted:

The one that comes immediately to mind is each state is a function, state transitions are a tail call.

I would rather build a richards controller or register fsm with 74-series logic than do this

Internet Janitor posted:

From the gamedev side of things, UnrealScript is slightly interesting, although it really provides Pushdown Automata rather than FSMs.

Neat!

Internet Janitor posted:

In Forth you can make a DSL which resembles a state transition table, which has the significant advantage over many other approaches that at a glance you can tell the set of transition rules is exhaustive:
code:
4 WIDE FSM: <Fixed.Pt#>
\ input:  |  other?  |  num?   |  minus?  |   dp?     |
\ state:  ---------------------------------------------
   ( 0 )     DROP >0    EMIT >1   EMIT >1     EMIT >2
   ( 1 )     DROP >1    EMIT >1   DROP >1     EMIT >2
   ( 2 )     DROP >2    EMIT >2   DROP >2     DROP >2 ;
This is kind of neato too, although I wish the fetish for brevity were less prevalent

Internet Janitor posted:


If your target language doesn't have support for this kind of metaprogramming you could use a tool like
Ragel to specify your FSM as a regular language.

At work we've played around with tools that generate a table-of-function-pointers FSM from a visio-esque state diagram. So far they're either lovely or 5 figures per seat. At the end of the day, the downside to the 'classical' state machine implementation and some of the 'semi-classical' varieties is that it's somewhat more verbose than I'd like. Thankfully gcc will warn you if you've forgotten a case so long as your state variable is an enum, so at least one source of error is eliminated (the biggest source of error is still mis-specifying the transition table :saddowns:).

PrBacterio
Jul 19, 2000

Otto Skorzeny posted:

I would rather build a richards controller or register fsm with 74-series logic than do this
In a C-like language, sure. But in a language like Haskell or ML, that is actually a very convenient way to do this.

nielsm
Jun 1, 2009



ToxicFrog posted:

Isn't that just equivalent to:

C++ code:
for (int i = 0; i < 5; i++) {
  func1();
  func2();
  func3();
}
?

As I mentioned, and you even quoted, not if you let the counter run from a non-multiple of 3 to possibly another non-multiple of 3. (In that case, you are rapidly entering regular state machine territory.)

ToxicFrog
Apr 26, 2008


nielsm posted:

As I mentioned, and you even quoted, not if you let the counter run from a non-multiple of 3 to possibly another non-multiple of 3. (In that case, you are rapidly entering regular state machine territory.)

Yes, but as written it's not doing that and given that I'd call them both "definitely bad".

Posting Principle
Dec 10, 2011

by Ralp

PrBacterio posted:

The same goes for C++ string handling, where it isn't even possible to convert a number into a string with standard C++ functions except by using a string stream. What ought to be a single line of code
C++ code:
std::string s = std::string( some_float_value );
thus becomes three or more:
C++ code:
std::ostringstream buffer;
buffer << some_float_value;
std::string s = buffer.str();
:gonk:

C++11 to the rescue :eng101:

C++ code:
std::string s = std::to_string(some_float_value);

PrBacterio
Jul 19, 2000

Jerry SanDisky posted:

C++11 to the rescue :eng101:

C++ code:
std::string s = std::to_string(some_float_value);
Ah, so they've finally fixed that? Good. Now just to see how long it takes for compilers to arrive that support and correctly implement the new standard ...

hobbesmaster
Jan 28, 2008

PrBacterio posted:

Ah, so they've finally fixed that? Good. Now just to see how long it takes for compilers to arrive that support and correctly implement the new standard ...

http://msdn.microsoft.com/en-us/library/vstudio/ee404875(v=vs.100).aspx

Posting Principle
Dec 10, 2011

by Ralp
The current MSVC implementation is based on an older version of the standard that only specified those 3 overloads. I believe GCC supports all 9.

Adbot
ADBOT LOVES YOU

hobbesmaster
Jan 28, 2008

Jerry SanDisky posted:

The current MSVC implementation is based on an older version of the standard that only specified those 3 overloads. I believe GCC supports all 9.

MS doesn't do all the overloads in math.h either so at least its consistent.

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