Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Mon Nov 04, 2013 11:20 pm UTC

That sounds like -Wtype-limits. Wouldn't -Wno-type-limits fix it? Or don't you want to do that? (I know there are "diagnostic pragmas" in some versions, but that's probably an "ugly" option.)

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Mon Nov 04, 2013 11:37 pm UTC

Why not simply have g take int as a parameter?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Jplus
Posts: 1721
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: Coding: Fleeting Thoughts

Postby Jplus » Tue Nov 05, 2013 9:41 am UTC

Or why not make f templated, so you don't need g in the first place?
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

User avatar
Yakk
Poster with most posts but no title.
Posts: 11115
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Tue Nov 05, 2013 12:43 pm UTC

Force cast x to a 64 bit int before comparing it to INT_MIN or INT_MAX. Which doesn't work for bignum, but hey.

Fancier, write a "AssertIsInBounds( INT_MIN, x, INT_MAX )" that does both static type analysis and runtime asserts, but only does the runtime assert if the static type analysis fails (and uses tag dispatching to avoid even instantiating the runtime assert if the compile time check says it cannot help but pass). This is easier with constexpr support in std I'd guess.

Also be careful about blindly comparing signed and unsigned values. Converting a negative signed value to an unsigned one gives implementation determined results (but not vice versa, IIRC), and gcc on aggressive optimization settings exploits that. What do you think happens when you do if(INT_MIN <= 0ul), and are you certain?
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
jovialbard
Posts: 613
Joined: Thu Apr 18, 2013 11:08 pm UTC

Re: Coding: Fleeting Thoughts

Postby jovialbard » Tue Nov 05, 2013 10:25 pm UTC

Anyone else ever do this, or is it just me?

Code: Select all

/*
some code
/*/
some alternative code for quick and dirty testing
//*/


Now add a single / in front of the first line and parse it again.
I wrote a book exploring the nature of identity and loss of self! And another book about babies!
Also, vlog and short-story blog

User avatar
jaap
Posts: 2094
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

Re: Coding: Fleeting Thoughts

Postby jaap » Tue Nov 05, 2013 10:48 pm UTC

jovialbard wrote:Anyone else ever do this, or is it just me?

Code: Select all

/*
some code
/*/
some alternative code for quick and dirty testing
//*/


Now add a single / in front of the first line and parse it again.


Yes, I do.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Wed Nov 06, 2013 12:34 am UTC

troyp wrote:That sounds like -Wtype-limits. Wouldn't -Wno-type-limits fix it? Or don't you want to do that? (I know there are "diagnostic pragmas" in some versions, but that's probably an "ugly" option.)

You, sir, name? wrote:Why not simply have g take int as a parameter?

-Wtype-limits is probably a good idea most of the time, and I wouldn't want to change it anyway. We're the kind of people who build everything with -Wall and -Werror. :-) (I guess we do also use -Wno-shadow -Wno-non-virtual-dtor -- though upon review I don't like that second one, and I'd add -Wextra if I didn't have to update who knows how many thousands of lines.)

Jplus wrote:Or why not make f templated, so you don't need g in the first place?

That example is simplified, and g does a lot more. Theoretically, f could be made templated, but it wouldn't be even close to worth it as it would have cascade effects over a lot of code for... basically no benefit. (We and I are also somewhat skiddish about templates because we (ab)use them a lot and our compile times are through the roof as a result.)

Yakk wrote:Force cast x to a 64 bit int before comparing it to INT_MIN or INT_MAX. Which doesn't work for bignum, but hey.
Good enough. :-) I like it.

This is easier with constexpr support in std I'd guess.
Hah, yeah right. I know at least some of our stuff is still built and tested with GCC 4.2, so I think C++11 will be a ways off, sadly.

Also be careful about blindly comparing signed and unsigned values.
In-context, the template parameter is always signed.

(Actually now that I think about it, it's not quite that you'd call g<int> or g<long> and risk g<unsigned>; you'd call g<8>, g<16>, g<32> or g<64>, and then that eventually goes through some typedef like blah<8/16/32/64>::type which is either int8 or int16 or int32 or int64, which makes it a lot harder I think to accidentally violate that.)

jovialbard wrote:Anyone else ever do this, or is it just me?

Code: Select all

/*
some code
/*/
some alternative code for quick and dirty testing
//*/


Now add a single / in front of the first line and parse it again.
No offense, but...

Image

:-)

What's wrong with an #if?

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Wed Nov 06, 2013 5:12 am UTC

EvanED wrote:What's wrong with an #if?
I just have "comment selection" and "uncomment selection" hotkeyed personally.

User avatar
skeptical scientist
closed-minded spiritualist
Posts: 6142
Joined: Tue Nov 28, 2006 6:09 am UTC
Location: San Francisco

Re: Coding: Fleeting Thoughts

Postby skeptical scientist » Wed Nov 06, 2013 6:23 am UTC

jovialbard wrote:Anyone else ever do this, or is it just me?

Code: Select all

/*
some code
/*/
some alternative code for quick and dirty testing
//*/


Now add a single / in front of the first line and parse it again.

That is so horrible that my preferred compiler won't even compile it.
I'm looking forward to the day when the SNES emulator on my computer works by emulating the elementary particles in an actual, physical box with Nintendo stamped on the side.

"With math, all things are possible." —Rebecca Watson

User avatar
phlip
Restorer of Worlds
Posts: 7569
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Wed Nov 06, 2013 6:47 am UTC

skeptical scientist wrote:That is so horrible that my preferred compiler won't even compile it.

What compiler? I can't think offhand of a language that uses C++-style comments that should reject it...

I occasionally use them in SQL, which has /*-style comments, but uses -- for single-line comments instead of //, so I use this form instead:

Code: Select all

/** /
option A
/*/
option B
/**/
adding or removing that space on the first line. I basically only do this while I'm rewriting a section of code... keeping the old code handy in case I need to refer back to it, or switch back to it to test something. It's gone before I commit.

Of course, then there's this old chestnut, which I still find entertaining:

Code: Select all

/*/*/0*/**/1

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Yakk
Poster with most posts but no title.
Posts: 11115
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Wed Nov 06, 2013 1:00 pm UTC

phlip wrote:
skeptical scientist wrote:That is so horrible that my preferred compiler won't even compile it.

What compiler? I can't think offhand of a language that uses C++-style comments that should reject it...

Any compiler worth its salt should say:
Warning: /* in comment.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
skeptical scientist
closed-minded spiritualist
Posts: 6142
Joined: Tue Nov 28, 2006 6:09 am UTC
Location: San Francisco

Re: Coding: Fleeting Thoughts

Postby skeptical scientist » Wed Nov 06, 2013 3:42 pm UTC

Yakk wrote:
phlip wrote:
skeptical scientist wrote:That is so horrible that my preferred compiler won't even compile it.

What compiler? I can't think offhand of a language that uses C++-style comments that should reject it...

Any compiler worth its salt should say:
Warning: /* in comment.

The internal compiler at my company instead says:
Error: /* in comment.
I'm looking forward to the day when the SNES emulator on my computer works by emulating the elementary particles in an actual, physical box with Nintendo stamped on the side.

"With math, all things are possible." —Rebecca Watson

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Fri Nov 08, 2013 4:57 am UTC

Ha, I just saw the C++14 draft standard...

Image

User avatar
Yakk
Poster with most posts but no title.
Posts: 11115
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Fri Nov 08, 2013 12:26 pm UTC

Eh, warnings, errors -- same thing under any reasonable compilation setting.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Fri Nov 08, 2013 2:55 pm UTC

My internal compiler segfaulted.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Diadem
Posts: 5654
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: Coding: Fleeting Thoughts

Postby Diadem » Tue Nov 12, 2013 9:28 am UTC

Yesterday I colleague of mine checked in code that broke the automated build. I didn't really have time to look at his code then. Today when I got to work he had checked in a few fixes, though it still doesn't build. Great. So I'm looking at what he did, and among many other horrors I found this gem (there's actually a few more multiplications in there, but I cut them out for the sake of brevity):

Code: Select all

double factor = // times_half
                    some_variable  // / some_other_variable
                                * third_variable * fourth_variable
#ifdef some_condition
                                * fifth_variable
#endif
                                ;

Ok so what the hell. That's some pretty terrible code. It doesn't end there though. Because today he changed it. The new version reads:

Code: Select all

double factor =    times_half
                    some_variable  // / some_other_variable
                                * third_variable * fourth_variable
#ifdef some_condition
                                * fifth_variable
#endif
                                ;

What the hell? How does that even compile? Guess what, times_half is secretly a preprocessor definition.

Code: Select all

#define times_half  (0.5)*
//#define times_half  (1.)*



I just want to stab my eyes out now. What the actual hell.

Honestly though, I don't know what to do now. I can't work with code like this.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

User avatar
Xenomortis
Not actually a special flower.
Posts: 1445
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Nov 12, 2013 9:35 am UTC

Revert the change and strangle him?

But this line is amazing

Code: Select all

//#define times_half  (1.)*
Image

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6568
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Nov 12, 2013 9:47 am UTC

Seriously, I don't get why anyone would do that; everyone knows macros should be all caps.
Summum ius, summa iniuria.

User avatar
jestingrabbit
Factoids are just Datas that haven't grown up yet
Posts: 5967
Joined: Tue Nov 28, 2006 9:50 pm UTC
Location: Sydney

Re: Coding: Fleeting Thoughts

Postby jestingrabbit » Tue Nov 12, 2013 11:47 am UTC

Wow, that is some impressively shitty code. I mean, shit man, wtf is going on inside that dudes head?
ameretrifle wrote:Magic space feudalism is therefore a viable idea.

User avatar
Diadem
Posts: 5654
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: Coding: Fleeting Thoughts

Postby Diadem » Tue Nov 12, 2013 3:21 pm UTC

Ok, so I'm not insane, am I, for finding that code horrible? Because I showed that code to one of my other coworkers and his reaction was basically "What's wrong with that?".
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

User avatar
Xenomortis
Not actually a special flower.
Posts: 1445
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Nov 12, 2013 3:42 pm UTC

I think it would be improved (at least be made more readable) by using inline comments (assuming your environment colours code).

Code: Select all


double factor 
= times_half some_variable /* / some_other_variable */ * third_variable * fourth_variable;
#ifdef some_condition
    factor *= fifth_variable;
#endif
 

But then again, I'm not sure you'd want to introduce that idea to him; who knows what he'll do with it.

jestingrabbit wrote:I mean, shit man, wtf is going on inside that dudes head?

The #define is stupid, but the rest of the code is actually laid out how you might write it on paper. Although it isn't consistent about it, which is also bad.
I mean, I don't think this:

Code: Select all


double factor 
= first_variable
               
* second_variable
               
* third_variable
               
// * fourth_variable
               * fifth_variable;
 

is terrible.
Image

User avatar
Diadem
Posts: 5654
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: Coding: Fleeting Thoughts

Postby Diadem » Tue Nov 12, 2013 3:54 pm UTC

Yeah putting multiplications below each other is fine, though his spacing is weird. It's mostly the crazy define and the #ifdef statement that get my goat. And putting every single term in the multiplication on its own line, except the commented out one, borders on deliberate obfuscation.

I think the problem is that my team members are mostly scientists, not programmers. Which is weird, because I'm not originally a programmer either - I studied physics and mathematics and only switched careers about a year ago. So how come I'm the only one who seems to have heard of the idea of coding standards?
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

User avatar
Xenomortis
Not actually a special flower.
Posts: 1445
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Nov 12, 2013 3:57 pm UTC

You read things on the internet?
I bet your coworkers don't post on forums like this one.
Image

speising
Posts: 2347
Joined: Mon Sep 03, 2012 4:54 pm UTC
Location: wien

Re: Coding: Fleeting Thoughts

Postby speising » Tue Nov 12, 2013 4:09 pm UTC

just found this gem:

Code: Select all

bool bNextDay = actualTime.Date > dtScanDay.Date ? true : false;

User avatar
jestingrabbit
Factoids are just Datas that haven't grown up yet
Posts: 5967
Joined: Tue Nov 28, 2006 9:50 pm UTC
Location: Sydney

Re: Coding: Fleeting Thoughts

Postby jestingrabbit » Tue Nov 12, 2013 5:47 pm UTC

Diadem wrote:It's mostly the crazy define and the #ifdef statement that get my goat.


This. The way I learnt, macros are nearly as bad as goto. And using them here... multiplying by a half?? Really???? I mean, sure, whatever compiles compiles, but... why macro when you can write straightforward, readable, non crazy making, code? It just seems like a bizarre choice out of nowhere.
ameretrifle wrote:Magic space feudalism is therefore a viable idea.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Nov 12, 2013 7:35 pm UTC

If this is in source control then the commented out values would throw me for a loop personally. I can't stand it when people check in code that isn't intended to be used. Particularly with no reason at least left as an actual comment.

Also, I can only wonder what the #if does, I hope it's not compile conditional or something.

User avatar
Diadem
Posts: 5654
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: Coding: Fleeting Thoughts

Postby Diadem » Tue Nov 12, 2013 7:52 pm UTC

jestingrabbit wrote:
Diadem wrote:It's mostly the crazy define and the #ifdef statement that get my goat.

This. The way I learnt, macros are nearly as bad as goto. And using them here... multiplying by a half?? Really???? I mean, sure, whatever compiles compiles, but... why macro when you can write straightforward, readable, non crazy making, code? It just seems like a bizarre choice out of nowhere.

Well, giving a name to a constant you often use, instead of putting it in as a numeric literal, is not crazy. Judging from the commented out line, he has been changing the constant around, probably for debugging purposes. So putting it somewhere at the top of the file makes sense. Using a #define instead of a const float is of course deprecated in C++ (it's c-style coding), but this guy not being up to date on the latest developments doesn't surprise me.

The really insane part is making the multiplication operator part of the #define statement. That's the kind of thing you do when you're young and just learning about programming, and makes you think you're being awfully clever.

Xeio wrote:If this is in source control then the commented out values would throw me for a loop personally. I can't stand it when people check in code that isn't intended to be used. Particularly with no reason at least left as an actual comment.

Sadly, this doesn't even surprise me anymore. There's huge chunks of commented out code all over the place. Heck, I'm happy if code is properly commented out, instead of disabled via if (0) statements, or #if 0.
Also, I can only wonder what the #if does, I hope it's not compile conditional or something.

It is conditional compiling. In C and C++ #if or #ifdef are precompiler statements for conditional compilation. These conditions can be set in the code, or passed to the compiler on the command line, so they make it impossible to know what code does without looking at build scripts. There are legitimate uses (the most common one is probably "#ifdef debug" for debug output), but in general it's a good idea to not use it.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

speising
Posts: 2347
Joined: Mon Sep 03, 2012 4:54 pm UTC
Location: wien

Re: Coding: Fleeting Thoughts

Postby speising » Tue Nov 12, 2013 7:57 pm UTC

anyway, calling something times_half, that is never *0.5, and not even always 0.5*, is not exactly nice.

User avatar
Sizik
Posts: 1243
Joined: Wed Aug 27, 2008 3:48 am UTC

Re: Coding: Fleeting Thoughts

Postby Sizik » Tue Nov 12, 2013 9:45 pm UTC

speising wrote:just found this gem:

Code: Select all

bool bNextDay = actualTime.Date > dtScanDay.Date ? true : false;


I could see someone doing that in C, which doesn't have a native boolean type, so comparison results would have to be converted to however you're implementing a bool.
gmalivuk wrote:
King Author wrote:If space (rather, distance) is an illusion, it'd be possible for one meta-me to experience both body's sensory inputs.
Yes. And if wishes were horses, wishing wells would fill up very quickly with drowned horses.

speising
Posts: 2347
Joined: Mon Sep 03, 2012 4:54 pm UTC
Location: wien

Re: Coding: Fleeting Thoughts

Postby speising » Tue Nov 12, 2013 9:48 pm UTC

Sizik wrote:
speising wrote:just found this gem:

Code: Select all

bool bNextDay = actualTime.Date > dtScanDay.Date ? true : false;


I could see someone doing that in C, which doesn't have a native boolean type, so comparison results would have to be converted to however you're implementing a bool.


it's C#.
also, note the consistent variable naming :)

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Nov 12, 2013 9:51 pm UTC

Diadem wrote:It is conditional compiling. In C and C++ #if or #ifdef are precompiler statements for conditional compilation. These conditions can be set in the code, or passed to the compiler on the command line, so they make it impossible to know what code does without looking at build scripts. There are legitimate uses (the most common one is probably "#ifdef debug" for debug output), but in general it's a good idea to not use it.
I'm familiar with the conditionals, I just can't imagine any where this calc would need to change at compile time. Maybe as a config, but even then.

speising wrote:it's C#.
also, note the consistent variable naming :)
I hope that b isn't for bool, personally.

Then again I'm ok with that convention for controls... so... I dunno.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Wed Nov 13, 2013 1:23 am UTC

speising wrote:just found this gem:

Code: Select all

bool bNextDay = actualTime.Date > dtScanDay.Date ? true : false;

Not enterprisey enough.

(That post is still the funniest Daily WTF for me. Borrowing the similar description of PHP, it's a fractal of WTF. I was laughing continuously for about 5 minutes first time I saw it as I discovered each new layer in it's onion of WTFness. I would like to draw particular attention to the fact that the author clearly already knows about booleanValue, and yet still wrote that function and in the way that he did.)

Sizik wrote:
speising wrote:just found this gem:

Code: Select all

bool bNextDay = actualTime.Date > dtScanDay.Date ? true : false;


I could see someone doing that in C, which doesn't have a native boolean type, so comparison results would have to be converted to however you're implementing a bool.
Eh, I think even then it isn't great unless your bool type is dumb. If you define false as 0 true as 1, you don't need the explicit conversion for things to be correct (which is why doing otherwise is "dumb" :-)).

Xenomortis wrote:I think it would be improved (at least be made more readable) by using inline comments (assuming your environment colours code).
Yeah, that version really really depends on coloring, otherwise it's much worse. And while it's reasonable to assume editors will do so (at least absent specialized environments), a lot of other tools don't, like your_vcs diff or grep. I also think it's worse in other ways -- if that's a switch-able feature, then it's "harder" to switch with the inline comments because of two places you have to change. I much prefer the version at the end of that post.

But yeah, the layout is bad, the macro name is terrible (//#define times_half (1.)* is a pretty awesome line of code), putting the multiplication in the macro is pretty terrible, and the fact that the macro isn't UPPER_CAMEL_CASE is bad. (Edit skeptical scientist points out that I'm an idiot, or at least not paying attention, re "UPPER_CAMEL_CASE".)
Last edited by EvanED on Wed Nov 13, 2013 4:01 am UTC, edited 1 time in total.

User avatar
PM 2Ring
Posts: 3700
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Wed Nov 13, 2013 2:06 am UTC

Diadem wrote:

Code: Select all

double factor =    times_half
                    some_variable  // / some_other_variable
                                * third_variable * fourth_variable
#ifdef some_condition
                                * fifth_variable
#endif
                                ;

What the hell? How does that even compile? Guess what, times_half is secretly a preprocessor definition.

Code: Select all

#define times_half  (0.5)*
//#define times_half  (1.)*


That's pretty bad. Are those the actual variable names?

That macro is obscene. I can kind of understand using a macro if you're experimenting, but what's wrong with something like:

Code: Select all

#define SCALE_FACTOR 0.5 /* May need to be 1.0 */
#define SCALE(F) (SCALE_FACTOR * (F))
 

I agree that the some_other_variable comment is annoying, but putting #ifdef stuff in the middle of an expression is downright horrible. But if he insists on using #ifdef for this, he should do something like:

Code: Select all

#ifdef SOME_CONDITION
double factor = SCALE(some_variable)  // / some_other_variable
    * third_variable * fourth_variable;
#else
double factor = SCALE(some_variable)  // / some_other_variable
    * third_variable * fourth_variable * fifth_variable;
#endif
 


TIL: We can get coloured text in code tags!

User avatar
skeptical scientist
closed-minded spiritualist
Posts: 6142
Joined: Tue Nov 28, 2006 6:09 am UTC
Location: San Francisco

Re: Coding: Fleeting Thoughts

Postby skeptical scientist » Wed Nov 13, 2013 2:11 am UTC

speising wrote:anyway, calling something times_half, that is never *0.5, and not even always 0.5*, is not exactly nice.

There's another macro for that:

Code: Select all

#define times_half  (0.5)*
#define half_times  *(0.5)

Obviously.

* * *


EvanED wrote:UPPER_CAMEL_CASE

Code: Select all

enum cases {
  UPPER_CAMEL_CASE,
  allCapsWithUnderscores,
  lower_camel_case,
  LowerCaseWithUnderscores,
};
I'm looking forward to the day when the SNES emulator on my computer works by emulating the elementary particles in an actual, physical box with Nintendo stamped on the side.

"With math, all things are possible." —Rebecca Watson

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Wed Nov 13, 2013 4:00 am UTC

skeptical scientist wrote:
EvanED wrote:UPPER_CAMEL_CASE

Code: Select all

enum cases {
  UPPER_CAMEL_CASE,
  allCapsWithUnderscores,
  lower_camel_case,
  LowerCaseWithUnderscores,
};

Um, right. What was I thinking?

On an unrelated note, anyone know of a Python library that gives access to the symbols inside of an object file and .a library? Like nm but I don't have to parse crap?

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Wed Nov 13, 2013 10:18 pm UTC

EvanED wrote:Yeah, that version really really depends on coloring, otherwise it's much worse. And while it's reasonable to assume editors will do so (at least absent specialized environments), a lot of other tools don't, like your_vcs diff or grep.

You can usually get syntax colouring by piping into "source-highlight -f esc -s yourlanguage"

Of course, it's probably not worth it for diff, since you lose the more important diff colouring. (It shouldn't be hard to write a script that will add that back in as "background highlighting" - I don't know if anyone's done it.)

edit: also, pygmentize. (Or, if you don't mind paging the output, just use vimpager which will use any syntax highlighting you have available in vim.)

edit: Actually, that reminds me of something...
PM 2Ring wrote:TIL: We can get coloured text in code tags!

Pygments actually has a BBCode formatter (as well as a bunch of others). I should set up a script for emacs to colorize code for the board automatically and copy it to clipboard).

btw, the command to color with pygmentize is "pygmentize -f terminal256 -O style=native -g" or "pygmentize -f terminal256 -O style=native -l yourlanguage"
The command to color with pygmentize into bbcode and copy to clipboard is "pygmentize -f bbcode -O style=native -O -g| xsel -ib". It didn't work in a code block when I just checked (worked outside one), but then neither did using the button, so maybe it's just the preview is broken.

edit: testing with a short C file... no, doesn't work even after submitting. @Xenomortis, PM2Ring, how did you get colours? Oh wait, I see...it's built in syntax colouring. That's handy, but I was hoping we could do arbitrary colouring of code (I've tried it before and failed). What languages does it work for? Is it only PHP?

Nyktos
Posts: 138
Joined: Mon Mar 02, 2009 4:02 pm UTC

Re: Coding: Fleeting Thoughts

Postby Nyktos » Thu Nov 14, 2013 3:35 am UTC

phpBB's builtin syntax highlighting is just PHP and HTML as far as I'm aware. Maybe it does JS if you put <script> tags around it.

User avatar
phlip
Restorer of Worlds
Posts: 7569
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Thu Nov 14, 2013 4:13 am UTC

I'm pretty sure it's just PHP... it just uses php's builtin syntax highlighter.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Diadem
Posts: 5654
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: Coding: Fleeting Thoughts

Postby Diadem » Fri Nov 15, 2013 1:20 pm UTC

PM 2Ring wrote:That's pretty bad. Are those the actual variable names?

The 'times_half' macro is called like that, all the variables aren't. I anonymized the code. The variable names were mostly reasonable. Surprisingly enough, because this colleague loves obscure names.

From the same source, I just found this in the code:

Code: Select all

const double cl = .636619772;

I think that's supposed to be 2 / Pi, but it scares me a bit.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

User avatar
Xenomortis
Not actually a special flower.
Posts: 1445
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Fri Nov 15, 2013 1:25 pm UTC

Does he have

Code: Select all

#define 2pi *cl*

in there too?
Image


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 92 guests