Questions tagged [coding-style]

Coding style is a set of guidelines that helps readability and understanding of the source code.

Coding style is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers to read and understand source code conforming to the style, and help to avoid introducing errors.

1056 questions
86
votes
9 answers

Should you always pass the bare minimum data needed into a function in cases like this

Let's say I have a function IsAdmin that checks whether a user is an admin. Let's also say that the admin checking is done by matching user id, name and password against some sort of rule (not important). In my head there are then two possible…
76
votes
15 answers

Is defining a variable to name a method argument a good practice?

For the sake of readability I often find myself defining temporary variables while calling functions, such as the following code var preventUndo = true; doSomething(preventUndo); The shorter version of this to this would be, doSomething(true); But…
72
votes
10 answers

Why are "if elif else" statements virtually never in table format?

if i>0 : return sqrt(i) elif i==0: return 0 else : return 1j * sqrt(-i) VS if i>0: return sqrt(i) elif i==0: return 0 else: return 1j * sqrt(-i) Given the above examples, I don't understand why I virtually never see…
horta
  • 840
60
votes
6 answers

Illusory code duplication

The usual instinct is to remove any code duplication that you see in the code. However, I found myself in a situation where the duplication is illusory. To describe the situation in more details: I am developing a web application, and most views are…
Mael
  • 2,345
59
votes
17 answers

Single statement if block - braces or no?

Which is better/more generally accepted? This: if(condition) { statement; } Or: if(condition) statement; I tend to prefer the first one, because I think it makes it easier to tell what actually belongs in the if block, it saves others from…
55
votes
10 answers

Doesn't "if (0 == value) ..." do more harm than good?

This is one of the things that I hate most when I see it in someone else's code. I know what it means and why some people do it this way ("what if I accidentally put '=' instead?"). For me it's very much like when a child goes down the stairs…
mojuba
  • 5,623
54
votes
11 answers

Is the 80 character limit still relevant in times of widescreen monitors?

on a widescreen monitor one can easily see more than 80 characters at a time, without scrollbars. even linus torvalds sees the 80 character limit as outdated. so, is the 80 character limit still relevant in times of widescreen monitors?
Lesmana
  • 1,559
45
votes
3 answers

Coding Style for Visually Impaired Programmer

I am visually impaired. With glasses I see well enough to drive, but at the font size I'm comfortable working at I can only see about 15 lines of 100 characters at a time. This has affected my coding style. One thing I do is write shorter…
39
votes
13 answers

In languages that don't allow underscores in integer constants, is it a good practice to create a constant for 1 billion?

In languages that don't allow underscores in integer literals, is it a good idea to create a constant for 1 billion? e.g. in C++: size_t ONE_BILLION = 1000000000; Certainly, we shouldn't create constants for small numbers like 100. But with 9…
Martin C. Martin
  • 1,223
  • 1
  • 9
  • 9
36
votes
11 answers

Best practices: Clarity vs. confidence in code behavior

When reviewing code, I sometimes see blocks like this (I happen to be using a JavaScript since it is widely understood): if (!myVar || myVar === null || myVar === undefined) { ... } Those who know JavaScript decently well will immediately…
33
votes
14 answers

Pointless Code In Your Source

I've heard stories of this from senior coders and I've seen some of it myself. It seems that there are more than a few instances of programmers writing pointless code. I will see things like: Method or function calls that do nothing of…
Ali
  • 471
  • 4
  • 8
30
votes
15 answers

Is it a bad idea to list every function/method argument on a new line and why?

I work with someone who, every time they call a function they put the arguments on a new line e.g. aFunction( byte1, short1, int1, int2, int3, int4, int5 ) ; I find this very annoying as it means the code isn't very…
Daniel Ball
  • 409
  • 1
  • 4
  • 6
30
votes
13 answers

Is coding style in organizations an optional thing?

This programming style document has a general rule, that says : The rules can be violated if there are strong personal objections against them. This collides with the way I am thinking, and there are many articles saying that coding style is…
BЈовић
  • 14,031
  • 8
  • 62
  • 82
29
votes
10 answers

I can't program because the code I am using uses old coding styles. Is this normal to programmers?

I have my first real job as programmer, but I can't solve any problems because of the coding style used. The code here: Does not have comments Does not have functions (50, 100, 200, 300 or more lines executed in sequence) Uses a lot of if…
25
votes
9 answers

Naming of "safe" versus "fast" functions

Say that I have two functions that are essentially identical, where one validates its arguments while the other doesn't. The rationale: sometimes you want to be safe and sometimes you want to go fast. What's your preferred naming convention to…
1
2 3 4 5 6