Jump to content

Could we make a thread specifically about bad practices?

darth_bubbles

I am learning programming and everywhere I look there's people saying "This is bad practice" and "This is not bad practice"

 

So in order to get rid of some confusion, let's post all the bad practices we can think of and why.

 

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Never use Go to's  for looping, loop exiting or instead of functions or anything.. that IS bad practice.

Java Programmer, AMD Fanboy and soon to be casemodder

Link to comment
Share on other sites

Link to post
Share on other sites

Masturbating infront of your parents is a bad practice

The BBQ: i7-4770 / 212x / Tri-X R9 290x 1075/1400 / MSI H87-G43 GAMING / EVGA G2 850W / Corsair Spec 03 / Samsung 840 EVO 250gb SSD / Toshiba 2TB HDD / 8gb Kingston DDR3 1600mhz

Peripherals: G710+ / G502 / Bose Companion 2 Series III / Audio Technica ATH-M40x / Sound Magic E50

Monitors: Dell U2414H 

Link to comment
Share on other sites

Link to post
Share on other sites

Go to's never use them, they work against the logic of a structural programming language.

Goto are fine in certain circumstances, such as error handling.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Using

break;

is a bad practice....or so every professor i've had likes to say--although It certainly has its merits. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Go to's never use them, they work against the logic of a structural programming language.

 

Go to has its uses in handling errors when you need to do clean up. Don't dismiss it entirely.

Just don't use it for looping, loop exiting or instead of functions or anything.. that IS bad practice.

Link to comment
Share on other sites

Link to post
Share on other sites

Go to has its uses in handling errors when you need to do clean up. Don't dismiss it entirely.

Just don't use it for looping, loop exiting or instead of functions or anything.. that IS bad practice.

okey thats what I meant

Java Programmer, AMD Fanboy and soon to be casemodder

Link to comment
Share on other sites

Link to post
Share on other sites

My fails:

Not knowing how to learn to code.

Not knowing how to write reusable code(waste of time later).

Link to comment
Share on other sites

Link to post
Share on other sites

Never use Go to's  for looping, loop exiting or instead of functions or anything.. that IS bad practice.

There are good uses for goto, they are simply rare.

Looping: use continue; instead (skips rest of loop, repeating loop cycle)

Loop exiting: break; unless you have nested loops. Here you can either use flags OR goto (last answer shows how to use flags)

Functions: just make a function

 

 

Using

break;

is a bad practice....or so every professor i've had likes to say--although It certainly has its merits. 

??? Interating through an array to select an object has a perfectly valid use for break; once you find it, provided it's not separated into a function.

for(int i=0;i<array.size();i++)  if(array[i].name == nameIAmLookingFor){    arrayObject = &array[i];    break; //saves unnecessary loops, much cleaner than a goto  }//work with arrayObject here
Link to comment
Share on other sites

Link to post
Share on other sites

Link to comment
Share on other sites

Link to post
Share on other sites

Trusting user input

CPU: Intel 3770k Cooling: Corsair H100i Motherboard: MSI Z77 MPower Memory: 2x4gb Corsair 1600 ddr3 GPU: Sapphire 6950 2gb with reference Cooler PSU: XFX pro 650w Case: Corsair Carbide 500r Storage: Crucial M4 128GB, 2x Samsung 1TB, 1x Seagate 2TB

CPU: Pentium 4 Motherboard: Gigabyte GA-8SGXL Memory: 2GB ddr Storage: 250GB IDE OS: FreeBSD

Link to comment
Share on other sites

Link to post
Share on other sites

Using

break;

is a bad practice....or so every professor i've had likes to say--although It certainly has its merits. 

but it's good in switch statements, right?

Link to comment
Share on other sites

Link to post
Share on other sites

Trusting your OOP professor who saying its good practice to use comments every one or 2 lines, using a lot of white lines, and for example when using an if statement:

if(variable<something){[some code]}

Putting the { on a next line.

Because all of the above things improve readability in his opinion....

Of course you have to use comments but come on... every 1 or 2 lines, bs. Using a lot of white lines and putting every curly bracket on a new line... Just NOPE

(btw this is mainly focused on Java)

Build log "Whiplash" : http://linustechtips.com/main/topic/158477-the-hero/

Whiplash: 4790k@4,4Ghz|Maximus VII Hero|4x4Gb Red/Black HyperX fury 1866Mhz|R9 290 Tri-X|Modded 450D|Sleeved cables on a M12II evo 850W|M500 480Gb| BenQ XL2411T@144Hz

Laptop: 4700MQ|16Gb@1600Mhz|Quadro 1100M|1080P|128Gb SSD|500Gb 7200RPM hdd

Link to comment
Share on other sites

Link to post
Share on other sites

In c++, using a struct of 100+ elements and totally about 800kB

Link to comment
Share on other sites

Link to post
Share on other sites

In c++, using a struct of 100+ elements and totally about 800kB

What's wrong with that?

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

What's wrong with that?

First, it's c++, not c, it's better to use class,

the names must be very hard to remember. it's better to nest struct  grouping some elements.

then one have to using pointer to void pass large size of data to functions, and allocate in stack.

Link to comment
Share on other sites

Link to post
Share on other sites

First, it's c++, not c, it's better to use class,

the names must be very hard to remember. it's better to nest struct  grouping some elements.

then one have to using pointer to void pass large size of data to functions, and allocate in stack.

 

Why is it better to use class? Functionally, struct and class are the same.

Link to comment
Share on other sites

Link to post
Share on other sites

 

There are good uses for goto, they are simply rare.

Looping: use continue; instead (skips rest of loop, repeating loop cycle)

Loop exiting: break; unless you have nested loops. Here you can either use flags OR goto (last answer shows how to use flags)

Functions: just make a function

 

 
 

??? Interating through an array to select an object has a perfectly valid use for break; once you find it, provided it's not separated into a function.

for(int i=0;i<array.size();i++)  if(array[i].name == nameIAmLookingFor){    arrayObject = &array[i];    break; //saves unnecessary loops, much cleaner than a goto  }//work with arrayObject here

Your loop would work, but it is very bad practice. Both of these solutions would be  better:

 

for (int i = 0; i < array.size() && arrayObject == null; i++) {   if (array[i].name == nameIAmLookingFor) {      arrayObject = &array[i];   }}		int i = 0;for (; i < array.size() && array[i].name != nameIAmLookFor; i++);arrayObject = &array[i];
Link to comment
Share on other sites

Link to post
Share on other sites

 

Your loop would work, but it is very bad practice. Both of these solutions would be  better:

for (int i = 0; i < array.size() && arrayObject == null; i++) {   if (array[i].name == nameIAmLookingFor) {      arrayObject = &array[i];   }}		int i = 0;for (; i < array.size() && array[i].name != nameIAmLookFor; i++);arrayObject = &array[i];

Let's agree to disagree,

This loop is literally 5 lines long there's nothing a coder can miss out on. I would follow your style if I was to work with a large, screen-height+ loop with multiple exit points, but here? No reason, the code looks clean regardless.

I have heard that it's apparently slower to use break than check for another flag, but I wonder how does that stack up in scenario of even just 100 objects scanned (100 checks) vs 1 break.

 

Lastly, to make people think I'm an absolutely terrible coder, you could use well-named goto to break out of a large loop with multiple exits or a nested loop! *makes scary ghost sounds*

Link to comment
Share on other sites

Link to post
Share on other sites

First, it's c++, not c, it's better to use class,

the names must be very hard to remember. it's better to nest struct  grouping some elements.

then one have to using pointer to void pass large size of data to functions, and allocate in stack.

 

A C++ struct is not the same as a C struct. A C++ struct is basically the same as a class... only struct objects are copied instead of referenced when reassigned.

Link to comment
Share on other sites

Link to post
Share on other sites

A C++ struct is not the same as a C struct. A C++ struct is basically the same as a class... only struct objects are copied instead of referenced when reassigned.

What do you mean by that? What is referenced when reassinged? Everything is copied by default in both c and c++ afaik, via field-to-field copy or copy constructor (just like classes)

The only difference i knew is that structs have public members by default, while classes default to private

Link to comment
Share on other sites

Link to post
Share on other sites

What do you mean by that? What is referenced when reassinged? Everything is copied by default in both c and c++ afaik, via field-to-field copy or copy constructor (just like classes)

The only difference i knew is that structs have public members by default, while classes default to private

 

Perhaps I am getting confused with C# then, sorry.

In C#, IIRC, a struct will be copied when assigned to another variable, where as with a class a reference will be assigned.

 

Therefore 

 

Struct a;Class b;Struct c = a; // c is a copy of a, it is not the same objectClass d = b; // d and b are references to the same object
Link to comment
Share on other sites

Link to post
Share on other sites

 

Perhaps I am getting confused with C# then, sorry.

In C#, IIRC, a struct will be copied when assigned to another variable, where as with a class a reference will be assigned.

 

Therefore 

 

Struct a;Class b;Struct c = a; // c is a copy of a, it is not the same objectClass d = b; // d and b are references to the same object

Right, that's a C# and Java thing because all variables of class types (as opposed to ints and floats) are just pointers.

In C++ both cases will just call your operator= function.

 

edit: Those will call the copy constructor. If they were already declared and being reassigned it will call operator=.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Some ASP.Net / General Web Programming things not to do:

 

  • Do not use control adapters
  • Do not set a control’s style properties in the code-behind, use CSS
  • Do not use a Browser Capability list, instead use client-side feature detection such as Modernizr
  • Do not depend on request validation to protect against XSS attacks, use input validation / sanitation
  • Do not use <%= %>, use <%: %>
  • Do not use Cookieless Forms Authentication
  • Do not Set EnableViewStateMac = false.
  • Do not use Medium Trust or any other level as a security boundary [http://msdn.microsoft.com/en-us/library/tkscy493%28v=VS.71%29.aspx]
  • Do not use to appSettings disable security fixes [http://msdn.microsoft.com/en-us/library/hh975440.aspx]
  • Do not use UrlPathEncode, use UrlEncode
  • Avoid using PreSendRequestHeaders & PreSendRequestContent, they can cause issues with async requests
  • Avoid writing async void methods for Page lifecycle events like Page_Load. Use Page.RegisterAsyncTask()
  • Avoid using “fire and forget work” such as timers and ThreadPool.QUWI. Use a Window Service or Worker Role.
  • Avoid reading Request.Form or Request.InputStream before the HandlerExecute event. If you need to, use Request.GetBufferlessInputStream() or GetBufferedInputStream().
  • Try to avoid using EnableViewState, instead use ViewStateMode = “Disabled”.

Tips taken from Scott Hanselman's talk at NDC 2013: http://vimeo.com/68390507

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×