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
Dicky B
Mar 23, 2004

Has anybody ever made that mistake more than once? Maybe if they code with their eyes closed.

If a conditional has no braces, it executes at most one line. It's an easy rule to learn. If you happen to add another line to a single-line if-statement then the error is right under your nose, because the problem area is at most three adjacent lines long. The above code sticks out like a sore thumb to me.

Adbot
ADBOT LOVES YOU

Hughlander
May 11, 2005

Optimus Prime Ribs posted:

Everyone has their preferred way to write code.
But why should I be penalised for not conforming to the professor's preference?

If you don't follow the company standards for style you're not getting through the code-review. May as well get used to it in college.

Volte
Oct 4, 2004

woosh woosh
Instead of thinking of the braces as belonging to the if statement, think of them as denoting a single compound statement (because that's what they do, in the grammar). A branch of an if-statement always contains a single statement, but that single statement may be a compound statement. It's not that hard to get used to verifying that the statement after an 'if' is of the correct form.

On the other hand, if the code style that you have to follow specifies that you need to use single-statement blocks, then that trumps all the other reasons you can think of for not doing it.

The Gripper
Sep 14, 2004
i am winner

Volte posted:

Instead of thinking of the braces as belonging to the if statement, think of them as denoting a single compound statement (because that's what they do, in the grammar). A branch of an if-statement always contains a single statement, but that single statement may be a compound statement. It's not that hard to get used to verifying that the statement after an 'if' is of the correct form.

On the other hand, if the code style that you have to follow specifies that you need to use single-statement blocks, then that trumps all the other reasons you can think of for not doing it.
I know someone that (when we were both younger) didn't really learn what was going on and had some broken code that he fixed up the only way he could think of:
code:
if (x == y)
   thing 1
if (x == y)
   thing 2
if (x == y)
   thing 3
...
if (x == y)
   thing n

Haystack
Jan 23, 2005





That's almost cute, in a pathetic sort of way.

That Turkey Story
Mar 30, 2003

Haystack posted:

That's almost cute, in a pathetic sort of way.

That's wh...

The Glumslinger
Sep 24, 2008

Coach Nagy, you want me to throw to WHAT side of the field?


Hair Elf
In a game engine I am using for a class, I found this shining gem:

code:
float GetTimeDeltaInSeconds()
{
      #if APIABSTRACTION_D3D9 || APIABSTRACTION_D3D11
		return (float)(DX9_Timer::TimeDifferenceInSeconds(m_before, m_now));
      #elif APIABSTRACTION_OGL
		return 1.0f / 30.0f;
      #endif
}
Well, I guess that explains that frame rate issues I was having :shepface:


In this engine's defense, it is actually really good, but how the gently caress does that one slip on by.

EDIT: Apparently it didn't and my team just never merged in the changelist with the fix :shepicide:

The Glumslinger fucked around with this message at 08:20 on Mar 25, 2012

Knyteguy
Jul 6, 2005

YES to love
NO to shirts


Toilet Rascal

Look Around You posted:

//PHP junk code

:stare:

Jesus christ what the gently caress. Why do people still use this garbage?

I'm more than a few pages late, and I'm not sure if you're being ironic but I think it's so popular because there's a low barrier to entry (it's pretty much universally supported at every web host) and the syntax is relatively easy to learn. This is excellent for commercial developers, and amateur coders alike; it runs everywhere!

There are also no includes, namespaces, classes, functions, etc. to get started either, so again it looks like a friendly language to learn for beginners. It also sort of was/is a natural next-step after learning html.

The discussion in this thread made me rethink using PHP for my future projects after seeing some of the :psyduck: things that PHP does like accepting concatenated variables (wtf?) so I might be looking into Ruby, or Mono + ASP for my dynamic web pages.

Anyway has PHP always been this terrible, or is this just a result of bloat over time?

Knyteguy fucked around with this message at 10:25 on Mar 25, 2012

Carthag Tuek
Oct 15, 2005

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



Not to get all Bret Victor up in here, but seriously why is it still necessary to even discuss brace-style & indentation?

Code is easily tokenizable; after all that is a criteria for compilation (and if it isn't tokenizable, modern compilers can usually figure out the syntax errors or make and educated guess - for display only mind you). Why doesn't every IDE just represent - by default - the code in whatever format the user likes? We don't need to actually look at the raw txt unless something is seriously broke. Make that the fallback; show the token stream indented as the user prefers by default.

Make indentation a as low level as choosing whether line endings are \n or whatever else.

Carthag Tuek fucked around with this message at 11:03 on Mar 25, 2012

tef
May 30, 2004

-> some l-system crap ->

Carthag posted:

Not to get all Bret Victor up in here, but seriously why is it still necessary to even discuss brace-style & indentation?

It's a topic everyone can have an opinion on. In the sense that there is no understanding or insight required to participate.

the colour of the bike shed should be chartreuse

Carthag Tuek
Oct 15, 2005

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



tef posted:

It's a topic everyone can have an opinion on. In the sense that there is no understanding or insight required to participate.

the colour of the bike shed should be chartreuse

That's exactly my point though, it's not important for the end result, and it's not important to you how I like to format my code. We should both just set up our dev environment to display the code how we like it and the underlying representation can be whatever (as long as its readable).

This way, if you like 8-space tabs and GNU-style braces, and I like 2-space tabs and banner braces, we can both do our own thing, and not care at all what the other does.

Also it makes diffs more readable.

Carthag Tuek
Oct 15, 2005

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



In short: Neither of us care, so hide the crap from us.

Look Around You
Jan 19, 2009

Knyteguy posted:

I'm more than a few pages late, and I'm not sure if you're being ironic but I think it's so popular because there's a low barrier to entry (it's pretty much universally supported at every web host) and the syntax is relatively easy to learn. This is excellent for commercial developers, and amateur coders alike; it runs everywhere!

There are also no includes, namespaces, classes, functions, etc. to get started either, so again it looks like a friendly language to learn for beginners. It also sort of was/is a natural next-step after learning html.

The discussion in this thread made me rethink using PHP for my future projects after seeing some of the :psyduck: things that PHP does like accepting concatenated variables (wtf?) so I might be looking into Ruby, or Mono + ASP for my dynamic web pages.

Anyway has PHP always been this terrible, or is this just a result of bloat over time?

Oh I know why it's popular. The low barrier for entry is actually part of the problem with PHP though. From what I've seen, a lot of the culture relies on cut-and-paste coding with little to no understanding of what it's actually doing or of the underlying mechanics.

Php coding style and common practices are pretty horrible too. It's commonly accepted to fully intersperse logic with presentation (that is, putting actual code that does stuff on data in the same files that output HTML), which is a pretty terrible practice.

Oh, and php just removed register_globals and magic quotes in this release. They also released it with 90 test failures (and 44 expected ones), some of which actually segfault, which will (probably) bring down your entire instance of PHP (and thus your site).

tef
May 30, 2004

-> some l-system crap ->

Carthag posted:

In short: Neither of us care, so hide the crap from us.

This entire thread is bike shedding. This is exactly where the crap should be.

GROVER CURES HOUSE
Aug 26, 2007

Go on...

tef posted:

This entire thread is bike shedding. This is exactly where the crap should be.

Bubblesort in production is special case of bikeshedding. :colbert:

baquerd
Jul 2, 2007

by FactsAreUseless

Hughlander posted:

If you don't follow the company standards for style you're not getting through the code-review. May as well get used to it in college.

Keep two autoformatting profiles, one to put code to a sane and sensible (i.e. what you like) format, one to put code to whatever crazy haphazard methods are used by the group.

Toady
Jan 12, 2009

I have a personal style, but I can read and write multiple styles without caring either way, certainly not enough to spend time maintaining multiple code formatting profiles.

If you're so used to your personal style that you have issues when reading or writing other common styles, you should consider exposing yourself to more external code. You should also experiment with other programming languages so that you're not attached to the syntax of your pet language. The algorithms and data structures are what matter.

The Gripper
Sep 14, 2004
i am winner

Toady posted:

I have a personal style, but I can read and write multiple styles without caring either way, certainly not enough to spend time maintaining multiple code formatting profiles.

If you're so used to your personal style that you have issues when reading or writing other common styles, you should consider exposing yourself to more external code. You should also experiment with other programming languages so that you're not attached to the syntax of your pet language. The algorithms and data structures are what matter.
I'm the same, but it'd probably irritate me if in a code review my code was rejected for not using braces on short if statements.

The only times I'm actually annoyed at people for forcing styles on me is when it's really frustrating to apply or contrived to the point of being detrimental. I worked with a guy that somehow got it passed as a company policy that any temporary variables used in counters had to be reused and defined at the top of every function. So every piece of code was like:
code:
int do_a_thing(int x) {
   int i,j,m,n = 0;
   
   for (i=0;i<x;i++){ ... }
}
If you got around it by putting your usage in an inner scope it'd be rejected. It was a pointless requirement that was just there to satisfy one person.

I've also worked with someone that required all variables go out of scope at the very last point they're used, and at the end of a function no variables or objects that aren't the return value/arguments were allowed to be in scope, so things ended up as:
code:
int whatever(int z) {
   int result;
   {
      int t2;
      {
         for (int i=0;i<100;i++) { t2 += i; }
      }
      result = other_call(t2)
   }
   return result;
}
A contrived example using simple expressions, imagine this applied to large blocks of code. Everything had the result variable defined first, and at the return result; line no other variables were allowed to be in scope. Using return other_call(t2) would have been rejected as t2 isn't out of scope at the point the function returns.

This also wasn't just someones personal preference, it was applied during code reviews and some people actually had code retroactively rejected when the policy culprit happened to notice it later.

Edit; you may have guessed, I work and have worked in some of the worst imaginable software dev houses.

The Gripper fucked around with this message at 00:56 on Mar 26, 2012

Toady
Jan 12, 2009

Those are arguably uncommon styles (I hope).

The Gripper
Sep 14, 2004
i am winner

Toady posted:

Those are arguably uncommon styles (I hope).
They're probably outside the realm of "style preference" as other posters here are discussing (braces, spacing, etc.), but I figured I'd give a non-hypothetical example of the kind of things I think people can have strong opinions about without being diagnosed spergy by the world at large.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
I'm genuinely curious about the rationale behind those policies. The "at the end of a function no variables or objects that aren't the return value/arguments were allowed to be in scope" policy sounds like a hilarious misunderstanding of the language and/or the result of a hilarious battle with some lovely compiler.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
I can imagine someone wanting that in C++ due to destructors "interfering" with the control flow. The generalization would be "break, continue, goto, and return should never have to run any destructors before reaching their destination." It's then easier to apply that rule to all locals instead of making it only apply to types with nontrivial destructors.

I mean, I think that would be a deeply problematic sign of a programmer who refuses to accept the C++ "say what you mean and learn to use a debugger" philosophy, but tons of those people exist.

I have no idea what you'd want this rule in a language without destructors, unless, yeah, it's the legacy of some awesome compiler bug.

rjmccall fucked around with this message at 02:06 on Mar 26, 2012

The Gripper
Sep 14, 2004
i am winner

pokeyman posted:

I'm genuinely curious about the rationale behind those policies. The "at the end of a function no variables or objects that aren't the return value/arguments were allowed to be in scope" policy sounds like a hilarious misunderstanding of the language and/or the result of a hilarious battle with some lovely compiler.
Both of those policies were extensions of personal preference, they had no actual constructive reasoning behind them. As far as I know they were just solutions to software development versions of workplace drama; rather than find a good way to pacify the person (or outright saying "stop doing this"), just fold and tell everyone else to deal with it. Instead of telling people which shelves of the fridge are for which food, they're telling people some other equally as unimportant rule to follow.

The Gripper fucked around with this message at 02:55 on Mar 26, 2012

biochemist
Jun 2, 2005

Who says we don't have backbone?
Over 1200 lines of this on a site I'm supposed to transfer to new hosting. I shouldn't have ever looked at the code and just moved it.

code:
                                <div style="top:159pt">
					<span class="css-gen13">
						<span class="css-gen14">worry. All you have to do is bask in the compliments.</span>
					</span>
				</div>
				<div style="top:176pt">
					<span class="css-gen13">
						<span class="css-gen14">One simple order takes care of everything. • </span>
						<span class="css-gen10 css-gen14">Price on request</span>
						<span class="css-gen14">.</span>
					</span>
				</div>
				<div style="left:123pt;top:410pt">
					<span class="css-gen19">
						<span class="css-gen5">The All-day Menu.</span>
					</span>
				</div>
				<div style="left:113pt;top:422pt">
					<span class="css-gen19">
						<span class="css-gen5">Everything You Need For</span>
					</span>
				</div>
				<div style="left:108pt;top:450pt">
					<span class="css-gen20">
						<span class="css-gen5">Breakfast</span>
					</span>
				</div>
				<div style="left:110pt;top:478pt">
					<span class="css-gen20">
						<span class="css-gen5">&amp; Breaks,</span>
					</span>
				</div>
				<div style="left:110pt;top:506pt">
					<span class="css-gen20">
						<span class="css-gen5">Lunch &amp;</span>
					</span>
				</div>
				<div style="left:119pt;top:534pt">
					<span class="css-gen20">
						<span class="css-gen5">Dinner</span>
					</span>
				</div>
			</div>

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I wonder what would happen if somebody invented a What You See is What You Get tool for C or Java or something.

lamentable dustman
Apr 13, 2007

ðŸÂ†ðŸÂ†ðŸÂ†

Suspicious Dish posted:

I wonder what would happen if somebody invented a What You See is What You Get tool for C or Java or something.

Netbeans has a drag and drop editor for working with Swing and it creates some horrible looking java code but the IDE does it's best to keep it hidden from the developer. IIRC, generated code is auto collapsed in the class and read-only.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Visual Studio's autogenerated code from the GUI designer is verbose, but usually perfectly readable (although it's hidden away in a separate file so you usually don't have to look at it).

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

biochemist posted:

Over 1200 lines of this on a site I'm supposed to transfer to new hosting. I shouldn't have ever looked at the code and just moved it.

Yay, McAlister's

Suspicious Dish
Sep 24, 2011

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

lamentable dustman posted:

Netbeans has a drag and drop editor for working with Swing and it creates some horrible looking java code but the IDE does it's best to keep it hidden from the developer. IIRC, generated code is auto collapsed in the class and read-only.

Plorkyeran posted:

Visual Studio's autogenerated code from the GUI designer is verbose, but usually perfectly readable (although it's hidden away in a separate file so you usually don't have to look at it).

Yeah, arguably that's data with a mustache on. I was thinking of code with logic and that sort of fun stuff. Something like this.

Carthag Tuek
Oct 15, 2005

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



Suspicious Dish posted:

Yeah, arguably that's data with a mustache on.

The interface builder in XCode doesn't even generate code, it just serializes the objects you set up, so it literally is data. You can set up stuff in code too, but usually there's no need unless you have some complex stuff going on.

Suspicious Dish posted:

I was thinking of code with logic and that sort of fun stuff. Something like this.

Just had a look, it looks like if you try to do anything more advanced than very very simple stuff, it'll turn into a world of poo poo in 5 seconds.

shrughes
Oct 11, 2008

(call/cc call/cc)
This entire post: http://blog.feabhas.com/2012/03/on-the-evolution-of-programming-style/

pseudorandom name
May 6, 2007

I'm disappointed he didn't wrap it in a lambda and pass it to std::async().

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Is this where we rewrite it to not suck while making embarrassing mistakes?

code:
#include <iostream>
#include <cmath>
#include <tuple>

template<class Number>
std::tuple<Number, Number> roots(Number a, Number b, Number c) {
    if (a == 0.0) throw std::invalid_argument("a has value 0.0!");
    Number rad = sqrt((b* b) - 4 * a * c);
    return { (-b + rad) / 2*a, (-b - rad) / 2*a };
}

int main() {
    double root1, root2;
    std::tie(root1, root2) = roots(1.0, 6.0, 8.0);
    std::cout << "Root1 : " << root1 << std::endl;
    std::cout << "Root2 : " << root2 << std::endl;
}
(didn't bother to check if this actually compiles to maximize the chance of an embarrassing mistake)

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
Edit: nevermind

HappyHippo fucked around with this message at 21:49 on Mar 26, 2012

evensevenone
May 12, 2001
Glass is a solid.

Suspicious Dish posted:

Yeah, arguably that's data with a mustache on. I was thinking of code with logic and that sort of fun stuff. Something like this.



There's Labview, which is about as horrible as it seems. The total number of man-hours wasted by mechanical and industrial engineers dragging teensy little lines around on a screen would probably put a man on Mars.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
I've never seen a drag and drop programming language that doesn't turn into a bundle of poo poo almost immediately when doing anything non-trivial. See SSIS for a prime example.

raminasi
Jan 25, 2005

a last drink with no ice
I took an electronic arts class in college in which in which I did my final project in Max/MSP and I blew my instructor's mind by actually skeleton-ing out the program before diving in and stringing connections around willy-nilly the way musicians using Max always do.

PDP-1
Oct 12, 2004

It's a beautiful day in the neighborhood.

evensevenone posted:

There's Labview, which is about as horrible as it seems. The total number of man-hours wasted by mechanical and industrial engineers dragging teensy little lines around on a screen would probably put a man on Mars.

LabView is a compound horror in that it doesn't just produce an unmaintainable mess of rainbow colored spaghetti code, it produces an unmaintainable mess of rainbow colored spaghetti coded that is very likely in control of a large dangerous hunk of machinery.

Fun LabView Stuff:
1) A LabView VI code block can operate either like an object with it's own state, or as some kind of globally public method with stored state that can be modified by anyone calling the code. There's no visual indication of which mode the VI is in, and the mode is set by a checkbox in an obscure settings panel. An accumulator VI that sums up all of the data sent into it over time may behave very oddly if it's used in two places.

2) Speaking of state - shift registers, if not initialized on first use, can store their values between different executions of the code so long as you don't shut down the IDE between runs. The bug you have now may be due to something you did the last time you ran the code.

3) Some LabView hardware controllers spin up their own internal threads, and if you put two such controllers in the same program they'll deadlock each other a lot. LabView can detect deadlocks and silently spins up new threads so your program doesn't crash (or even generate an error message), but then it leaves the old deadlocked threads in place. You know you've hit this problem when your system slows to a crawl after five minutes and the system monitor shows 8,624 active threads.

4) LabView's motor controllers can operate in two regimes of stability: (1) If you want a motor to turn clockwise, power up the coils that turn it clockwise. This is the sane mode. (2) Power up both the coils that turn the motor clockwise and counter-clockwise to 100% so the opposing torques hold the shaft in place. If you want to turn clockwise, power down the counter-clockwise coils to 90% so the clockwise coils begin to win. This is the "Hey do you smell that hot electronics smell? *touches motor and burns off outer skin layer*" mode.

Vanadium
Jan 8, 2005

Labview sounds like a really good idea the more distance I get between it and myself, thanks for reminding me the execution isn't quite up to scratch.

Adbot
ADBOT LOVES YOU

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
The idea of controlling industrial machinery on anything but a PLC sounds insane.

PLC programming is a horror of it's own, but not in a "this is dangerous" sort of way.

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