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
Volte
Oct 4, 2004

woosh woosh
There's no need to refactor Q_rsqrt at all. The only things missing are comments that actually explain how it works. "Self-documenting code" doesn't mean that your code should literally have the explanations of how it works in the names of the variables and functions.

Adbot
ADBOT LOVES YOU

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

Ithaqua posted:

That seems like a great example of code that can easily be refactored to be a lot more descriptive about what the gently caress it's actually doing.

I'd like to see how you can refactor that code to be more descriptive about how it works, given that there are entire papers written about the technique.

Sure, better comments make a world of difference, but attempting to make this code self-documenting is probably a serious waste of time.

hobbesmaster
Jan 28, 2008

Ithaqua posted:

That seems like a great example of code that can easily be refactored to be a lot more descriptive about what the gently caress it's actually doing.

I wasn't very clear in the post, that is an example of the proper time to use stupid assembly tricks, xor swap isn't. And I was being very sarcastic about the comments.

As for what it is, thats the classic abusing IEEE 754 fast inverse square root. Its using one iteration of newton's method to approximate the inverse square root, which is used in a ton of lighting calculations. These days you'd "refactor" it to one SSE instruction, or more likely, a shader program. Didn't have either of those things in 1999.

fritz
Jul 26, 2003

Yeah, that code is clearly doing crazy floating point stuff, and the best thing to do is toss your hands up and let it do its own thing (or check that it's actually faster than 1/sqrt(x) on the hardware and if not change it).

tef
May 30, 2004

-> some l-system crap ->

Cocoa Crispies posted:

If your functions are taller than about ten (twenty is pushing it) lines that's the problem.

I can't recall where I read it but the adage I found to be more useful is the number of variables in a function, rather than the length. :v:

(ps, bikeshed should be blue, etc)

Zombywuf
Mar 29, 2008

tef posted:

I can't recall where I read it but the adage I found to be more useful is the number of variables in a function, rather than the length. :v:

Yes, but how many variables?

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker

tef posted:

I can't recall where I read it but the adage I found to be more useful is the number of variables in a function, rather than the length. :v:

(ps, bikeshed should be blue, etc)

Another red flag for me is seeing a a lot of repetitive code that could be easily factored away in a generic function or a shitload of functions that are always called in succession.

(ps, there are no programming discussions that don't end up in a bikeshed though.)

Opinion Haver
Apr 9, 2007

Zombywuf posted:

Yes, but how many variables?

Seven.

HORATIO HORNBLOWER
Sep 21, 2002

no ambition,
no talent,
no chance
The developer I replaced prefaced every single one of his variable names with "the." Sometimes he used semantic names ("theYear"), sometimes he used vague and unhelpful ones ("theResult"), sometimes he just concatenated it with the type name ("theJSONObject"). It's utterly maddening.

nielsm
Jun 1, 2009



HORATIO HORNBLOWER posted:

The developer I replaced prefaced every single one of his variable names with "the." Sometimes he used semantic names ("theYear"), sometimes he used vague and unhelpful ones ("theResult"), sometimes he just concatenated it with the type name ("theJSONObject"). It's utterly maddening.

Let's call it Long Reverse-Hungarian Notation.

Aionic Duck
Apr 21, 2010

HORATIO HORNBLOWER posted:

The developer I replaced prefaced every single one of his variable names with "the." Sometimes he used semantic names ("theYear"), sometimes he used vague and unhelpful ones ("theResult"), sometimes he just concatenated it with the type name ("theJSONObject"). It's utterly maddening.

I've had to maintain some code that has variable names like this. Boggles my damned mind with regards to why someone would think this was wise.

pseudorandom name
May 6, 2007

Tab completion is of the devil and must be opposed on all fronts.

Progressive JPEG
Feb 19, 2003

Volte posted:

There's no need to refactor Q_rsqrt at all. The only things missing are comments that actually explain how it works. "Self-documenting code" doesn't mean that your code should literally have the explanations of how it works in the names of the variables and functions.

I generally only trust the code with showing me what it's doing, and the comments to tell me why it needs to be done.

PS: Can we get back to the horrors or do we have another couple pages of nitpicking to get through

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

Aionic Duck posted:

I've had to maintain some code that has variable names like this. Boggles my damned mind with regards to why someone would think this was wise.

I'm pretty sure it's from Borland's books. I remember something very similar from both Turbo Pascal and Turbo C's examples. Delphi is probably similar.

Flobbster
Feb 17, 2005

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

trex eaterofcadrs posted:

I'm pretty sure it's from Borland's books. I remember something very similar from both Turbo Pascal and Turbo C's examples. Delphi is probably similar.

Apple uses a similar style in a lot of their code for parameter names, especially in the case of setter methods where you want to name the parameter something that doesn't collide with the instance variable of the same name:

code:
- (void) setFoo:(int) aFoo
{
    self.foo = aFoo;
}

PrBacterio
Jul 19, 2000

trex eaterofcadrs posted:

I'm pretty sure it's from Borland's books. I remember something very similar from both Turbo Pascal and Turbo C's examples. Delphi is probably similar.
Huh? I don't remember anything like that from my DOS Turbo Pascal / Delphi days. The usual naming convention was to prefix type names with a T, and then have variables which have no identity beyond that, e.g. callback parameters, just named after the generic noun, i.e. canvas: TCanvas, and so on.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

KARMA! posted:

Another red flag for me is seeing a a lot of repetitive code that could be easily factored away in a generic function or a shitload of functions that are always called in succession.

(ps, there are no programming discussions that don't end up in a bikeshed though.)

If you ever notice that you are writing the same thing again, write a new function or assign a variable. :colbert:

That said,
Agreed.

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

Zombywuf posted:

People look at me weird when I say function names should not include the word "and". Sometimes they have the decency to look embarrassed though.

I know, whenver I see testAndSet used I always think it's dying to be broken up into two separate functions.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Jonnty posted:

I know, whenver I see testAndSet used I always think it's dying to be broken up into two separate functions.

Amateur. TestAndSet should be three functions.

Amarkov
Jun 21, 2010
code:
public interface TestAndSet<T> {
    public static test(T obj, T val);
    public static set(T obj, T val);
}

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
You and your tight-coupling.

code:
public interface Testable<T> { public void test(T obj, T val); }
public interface Settable<T> { public void  set(T obj, T val); }
public interface Atomic<T> implements Testable<T>, Settable<T> { }

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

PrBacterio posted:

Huh? I don't remember anything like that from my DOS Turbo Pascal / Delphi days. The usual naming convention was to prefix type names with a T, and then have variables which have no identity beyond that, e.g. callback parameters, just named after the generic noun, i.e. canvas: TCanvas, and so on.

T* was the type, the actual instance was the* for instance variables where only 1 would exist at a time. I had to dig but I did find at least one official borland example for Turbo Vision of all things where they did this:
C++ code:
 //
  // About dialog box
  //  About dialog box features updating display of
  // the current time.
  //
  class TAboutTimeDialog : public TDialog
  {
      private:
      char CurrentTime[50];     // current time
      TInputLine *TimeDisplay;  // Inputline to display
      // the time

      protected:

      // Get the time string
      // from DOS and store in date_time
      void GetTimeString(char *date_time);

      public:

      TAboutTimeDialog(char *Title);

      // implement idle function for time updating
      void idle(void);

      virtual void handleEvent(TEvent &theEvent);
  };
Also I think I remember that the automated tools would make TApplication theApplication; or something like that by default. Drugs, alcohol and time have diluted those memories though, and I could be mistaken about that.

PrBacterio
Jul 19, 2000

trex eaterofcadrs posted:

T* was the type, the actual instance was the* for instance variables where only 1 would exist at a time. I had to dig but I did find at least one official borland example for Turbo Vision of all things where they did this:

Also I think I remember that the automated tools would make TApplication theApplication; or something like that by default. Drugs, alcohol and time have diluted those memories though, and I could be mistaken about that.
I don't know, as I've said I've never seen this phenomenon that you're talking about. It always used to be, say, canvas: TCanvas, I've never seen something like theCanvas: TCanvas, and I used to do quite a bit of Delphi, and before that, Turbo / Borland Pascal stuff back in the day. But then again I was talking about my Pascal / Delphi days, which is what was being talked about in this thread, I've never used Borland's C++ RAID tools, which is apparently where you took that example from.

Zombywuf
Mar 29, 2008

Jonnty posted:

I know, whenver I see testAndSet used I always think it's dying to be broken up into two separate functions.

I know right, I use low level locking operations all the time in my code.

Dicky B
Mar 23, 2004

How 2 program computers like a b0ss:

Maximum 10 lines in a function
Maximum 7 variables in a function
Never use "tmp" as a variable name
Never use the word "and" in a function name
Only have a single point of exit from a function
Never use goto
Kill yourself

Please memorize all of this retarded poo poo it will be in the exam.

pigdog
Apr 23, 2004

by Smythe
C coding makes one p. mad.

hobbesmaster
Jan 28, 2008

Dicky B posted:

How 2 program computers like a b0ss:

Maximum 10 lines in a function
Maximum 7 variables in a function
Never use "tmp" as a variable name
Never use the word "and" in a function name
Only have a single point of exit from a function
Never use goto
Kill yourself

Please memorize all of this retarded poo poo it will be in the exam.

So longjmp is cool?

Dicky B
Mar 23, 2004

On a more serious note I have to look at other people's code a lot, and it is difficult. The nature of this difficulty usually has nothing to do with the trivial details of their coding "style" such as the names of temporary variables and the lengths of functions. It is higher up, in the way the program is structured, how things are encapsulated etc.

You can throw as many silly blanket rules at that problem as you like and it's not going to help. Of course it's fun to blue bikeshed about this stuff. I'm guilty of it myself.

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

Suspicious Dish posted:

I have high hopes for Rust

Clay is more promising than Rust imo

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



Dicky B posted:

On a more serious note I have to look at other people's code a lot, and it is difficult. The nature of this difficulty usually has nothing to do with the trivial details of their coding "style" such as the names of temporary variables and the lengths of functions. It is higher up, in the way the program is structured, how things are encapsulated etc.

You can throw as many silly blanket rules at that problem as you like and it's not going to help. Of course it's fun to blue bikeshed about this stuff. I'm guilty of it myself.

It's always a good idea when you're writing code to mimic the conventions of the standard libraries for that language. Whatever your own preferences are. That'll at least make it easier for others to get a grasp on how your thing is structured.

For example, when I write Objective-C code, I try to use as many Cocoa conventions and patterns as apply to the project - delegates, notifications, key-value coding/observing, etc.

Progressive JPEG
Feb 19, 2003

Zombywuf posted:

I know right, I use low level locking operations all the time in my code.
FWIW std::atomic_flag in C++11 is pretty drat useful.

Here's a reaction to a prior horror which may soon end up becoming a horror itself (feel free to let me know if you did something like this and how it worked out):

My office of maybe 15 developers didn't have any sort of formatting standard across our C++ codebase, so depending on the component, some stuff was LikeReadingJava(), other stuff was more_like_plain_C(), with some things indented with tabs and others indented with 4sp. When editing something I usually just reconciled this by going with whatever the majority of the file/component used.

A few weeks ago we started fleshing out a basic formatting standard, and recently started enforcing the more significant bits (mainly indentation and such) through the "astyle" utility on all commits -- if a commit fails astyle then it doesn't get into the main tree until it's fixed and resubmitted. It's pretty clunky but there are at least no longer any individual files with a horrible mix of everyone's favorite style and indentation (yes, some files effectively had randomized indentation).

The Gripper
Sep 14, 2004
i am winner

Otto Skorzeny posted:

Clay is more promising than Rust imo
I like Clay, plus the guy behind it is probably the most level-headed, least idealistic dude I've ever seen doing language design. Deserves more support than it has currently, for sure.

Zombywuf
Mar 29, 2008

Dicky B posted:

On a more serious note I have to look at other people's code a lot, and it is difficult. The nature of this difficulty usually has nothing to do with the trivial details of their coding "style" such as the names of temporary variables and the lengths of functions. It is higher up, in the way the program is structured, how things are encapsulated etc.

You can throw as many silly blanket rules at that problem as you like and it's not going to help. Of course it's fun to blue bikeshed about this stuff. I'm guilty of it myself.

When I see a codebase full of 3000 line functions my preferred blanket rule is "Don't do that or I'll break your fingers." Good style doesn't fix bad code, but bad style makes it worse.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Otto Skorzeny posted:

Clay is more promising than Rust imo
Rust seems much more likely to catch on rather than languish in the graveyard of great languages with no libraries due to the Mozilla backing.

Freakus
Oct 21, 2000
Edit: nevermind, beaten.

Freakus fucked around with this message at 02:37 on Nov 26, 2012

Volte
Oct 4, 2004

woosh woosh

Zombywuf posted:

When I see a codebase full of 3000 line functions my preferred blanket rule is "Don't do that or I'll break your fingers." Good style doesn't fix bad code, but bad style makes it worse.
It is my belief that (possibly catalogued in some arcane Greek tome) there are some numbers between 10 and 3000.

Toady
Jan 12, 2009

The Art of Readable Code is a great book.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

hobbesmaster posted:

So longjmp is cool?

I've found it useful to implement exception handling in C libraries. It's a tool, and if it allows you to write cleaner code, it's worth using. This is the same reason to use goto, function pointers, anonymous functions (in languages more powerful than C), iterators, etc.

HORATIO HORNBLOWER
Sep 21, 2002

no ambition,
no talent,
no chance

Cocoa Crispies posted:

I've found it useful to implement exception handling in C libraries. It's a tool, and if it allows you to write cleaner code, it's worth using. This is the same reason to use goto, function pointers, anonymous functions (in languages more powerful than C), iterators, etc.

Exactly. The bottom line is that code should be expressive. It should communicate, it should tell a story. Arbitrary guidelines about style are popular because they're easy to teach. Teaching someone to be expressive isn't easy in natural language and it isn't any easier in programming languages.

Adbot
ADBOT LOVES YOU

Gism0
Mar 20, 2003

huuuh?
http://www.daniweb.com/software-development/cpp/threads/440954/cant-fix-error-in-my-program

I don't even know what the gently caress

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