Coding style is a set of guidelines that helps readability and understanding of the source code.
15
votes
12answers
2k views
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 constants, 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 ...
13
votes
8answers
606 views
Should I write compact code or code with lots of spaces?
I have two friends that have completely different schools of thought on how to lay out their code.
The first says that code should be well-indented and use lots of spaces and to name variables ...
3
votes
7answers
178 views
Ensuring that headers are explicitly included in CPP file
I think it's generally good practice to #include the header for any types used in a CPP file, regardless of what is already included via the HPP file. So I might #include <string> in both my HPP ...
1
vote
2answers
127 views
Any suggestions on how to over-rely on Google? [closed]
I don't know whether you Google a lot while you coding. I discover my coding practise always rely on Googling. One of the reason why is I don't familiar with the framework, So, I will do How to XXX in ...
1
vote
5answers
167 views
Is it a good practice to write a method that gets something and checks the value? [duplicate]
Occassinally I have to write methods like this:
string GetReportOutputDirectoryAndMakeSureExist()
{
string path = Path.Combine ( ... ) //whatever logic
if(!Directory.Exists(path)) ...
2
votes
3answers
228 views
Programming Style in Large Scale C++ Applications
Recently I've been browsing source code of large applications written in C++ to learn a bit but I couldn't help but notice that most if not all use a lot of IFDEFs and class-less functions (where they ...
17
votes
6answers
1k views
Why the recent shift to removing/omitting semicolons from Javascript?
It seems to be fashionable recently to omit semicolons from Javascript. There was a blog post a few years ago emphasising that in Javascript, semicolons are optional and the gist of the post seemed to ...
4
votes
3answers
267 views
What are the downsides of mixing tabs and spaces?
The advantage of using tabs for indentation is that people can configure their editor to use the tab width they are comfortable with. The only argument against this seems to be that people don't want ...
1
vote
3answers
282 views
Code Base with awful code conventions, follow them?
With no time to refactor, when you are working on legacy code, with the most awful conventions, what is the best practice?
Will trying to follow better coding style would improve readability or ...
7
votes
2answers
331 views
What does Uncle Bob mean by 'noun phrase names'?
I am reading Clean Code by Uncle Bob. Because I am not a native-English speaker, I couldn't understand following statement:
Classes and objects should have noun or noun phrase names like
...
3
votes
3answers
541 views
When are chained assignments (i.e. a=b=c) bad form?
I'm working on a VB.Net WinForms project and found myself writing code like this:
this.Fizz.Enabled = this.Buzz.Enabled = someCondition;
I couldn't decide whether that was bad code or not. Are ...
1
vote
1answer
50 views
Helper Methods Placement
Here's a question that's always bugged me. I'm going to use java as an example because I've almost never run into a problem in java where I didn't need to use helper methods in its class structure.
...
42
votes
12answers
2k views
Coding style (do more, then simplify) [duplicate]
I'm a CS student and I have been coding for a few months shy of a year now, and I seem to have developed what I think may be a "bad" habit and I'm wondering if anyone does the same (or whether it's a ...
12
votes
8answers
2k views
Why are people so strongly opposed to #region tags in methods?
I hear a lot about keeping methods short and I've heard a lot of programmers say that using #region tags within a method is a sure sign that it is too long and should be refactored into multiple ...
7
votes
3answers
202 views
Dynamic typing function arguments - how to keep readability high?
Dynamic typing newbie here, hoping for some wizened words of wisdom.
I'm curious if there is a set of best practices out there for dealing with function arguments (and let's be honest, variables in ...
1
vote
2answers
154 views
Programming by Intention, Depth-First or Breadth-First?
Say I have the following graph of dependencies between procedures/functions/methods:
o
/ \
v e
/ \ / \
r f l w
That is, function o first calls function v, and then ...
0
votes
4answers
227 views
Forward declaration vs include
Reduce the number of #include files in header files. It will reduce build times. Instead, put include files in source code files and use forward declarations in header files.
I read this in here. ...
2
votes
1answer
107 views
Less code or less operation
Sometimes I hesitate between "More code to avoid unnecessary operations" and "less code but with redundant operations". Let me just take an example (Win32 API):
I try to paint some controls manually ...
0
votes
2answers
151 views
unique_ptr and references - coding style question
I'm pretty sure this is a question purely about aesthetics but I wanted to get all your opinions on it before I start proliferating this type of code in my codebase. Consider the following code:
...
5
votes
2answers
230 views
Is it OK to have a method return different types based on a parameter?
I'm looking for a reference to clean coding styles that I can pass to a team member.
In particular, the rule that a method should not change its return type based on an input parameter. If you need ...
20
votes
8answers
3k views
What is the ideal length of a method?
In object-oriented programming, there is no exact rule on the maximum length of a method , but I still found these two qutes somewhat contradicting each other, so I would like to hear what you think.
...
8
votes
6answers
921 views
Should my code be DRY or readable if it can't be both?
I'm writing Ruby code for a simple encryption exercise and have frequently run across this dilemma (the exercise is a solitaire cipher if you must know). It is a question of whether I should pad out ...
59
votes
19answers
10k views
How important is it to reduce the number of lines in code?
I am a Software developer who works on J2SE (core java).
Often during our code reviews we are asked to reduce the number of lines in our code.
It's not about removing redundant code, it's about ...
31
votes
9answers
6k views
Does the usage of LINQ and Lambda Expressions lead to less readable code?
I'm having a discussion with a co-worker on Linq, I'll copy here:
Co-Worker: Lets be honest here. Linq
syntax sucks. It's confusing and
non-intuitive.
Me: oh come on, more
confusing ...
1
vote
2answers
223 views
Choosing between words with different spellings for function names
A question has been bothering me for a while: when developing international projects, it is common sense to use English as the reference language since it is the language that the most people ...
2
votes
3answers
466 views
Microsoft's coding standards for ASP.NET controls
I cannot find any naming standards/conventions in MSDN for naming ASP.NET controls.
One of the following standards tends to be used by programmers:
lblAddress
AddressLabel
Address
According to ...
2
votes
2answers
153 views
Are there standard style guides for PHP?
I've been working in a very small team for PHP development and we've been using an ad-hoc, "Invented Here" style guide. Now that our team and codebase is growing, we would like to use a more ...
6
votes
3answers
482 views
Programming standards and principles to become better programmer [closed]
I am a c# developer.
I have always been interested in increasing my skills and knowledge and trying to pickup new technology.
However now I want to enhance my knowledge in Programming standards and ...
29
votes
8answers
3k views
How can I learn to effectively write Pythonic code?
Doing a google search for "pythonic" reveals a wide range of interpretations. The wikipedia page says:
A common neologism in the Python community is pythonic, which can have a wide range of ...
9
votes
4answers
682 views
C# return variables
In a debate regarding return variables, some members of the team prefer a method to return the result directly to the caller, whereas others prefer to declare a return variable that is then returned ...
1
vote
4answers
380 views
Is using `continue`, `break` in non-`switch` loops and `?:` bad practice? [duplicate]
Back in college I've been told that using break; and continue; outside switch statements (e.g. to escape for or while loops) is wrong, bad practice and bad habits at the same time because it only ...
24
votes
21answers
2k views
What are your programming idiosyncrasies? [closed]
I noticed that I have a peculiar habit of finishing every line with a space. It carries over from my prose writing where a paragraph can have multiple sentences and so it is very common to follow a ...
5
votes
10answers
1k views
Have you changed your coding style recently? It wasn't hard wasn't it? [closed]
I've used to write code in C-like languages using the Allman style, regarding the position of braces.
void foo(int bar)
{
if(bar)
{
//...
}
else
return;
//...
}
Now the last two ...
32
votes
15answers
3k views
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:
...
8
votes
1answer
199 views
is there any elegant way to analyze an engineer's process?
Plenty of sentiment exists that measuring commits is inappropriate.
Has any study been done that tries to draw in more sources than commits - such as:
browsing patterns
IDE work (pre-commit)
idle ...
5
votes
3answers
210 views
Is relying on implicit argument conversion considered dangerous?
C++ has a feature (I cannot figure out the proper name of it), that automatically calls matching constructors of parameter types if the argument types are not the expected ones.
A very basic example ...
46
votes
15answers
3k views
Working on someone else's code
I have hardly a year's experience in coding. After I started working, most of the time I would be working on someone else's code. Either adding new features over the existing ones or modifying the ...
1
vote
5answers
113 views
Where to read from file?
In Java: I have a class Students. I would like to read a list of students from a file. Which one is more elegant:
Reading and constructing the list in main(), and creating a Student object with the ...
4
votes
4answers
264 views
In which order should I do comparisons? [duplicate]
I'm a strong proponent of writing if statements like this:
variable == constant
Because to me it just makes sense, it is more readable than the inverted:
constant == variable
Which seems to be ...
-4
votes
1answer
88 views
Check Javascript coding practice and overall performance [closed]
I have a Edit/Details form which has 4 user related fields. On click of Save, I save the edited fields to local storage (if supported) and display the same values in the Details view.
Below is the ...
1
vote
4answers
352 views
Switching from ActionScript to JavaScript, tips for writing code?
I am quite comfortable with using actionscript3 and flash. I also have some experience with Java. But recently, I started learning JavaScript and node.js. I ultimately want to make a 3d game in ...
4
votes
3answers
341 views
Space between negative sign and variable name
I tried doing a Google search, as well as searching this Stack Exchange site but could not find a question relating directly to this.
The PEP 8 -- Style Guide for Python Code has lots of good style ...
7
votes
7answers
976 views
Prevent developers from using constants
I have one one software system which allows developers to specify an ID or name to create NodeReferences. Both work fine, but ID's are not guaranteed to be the same across different environments. I've ...
11
votes
7answers
631 views
Is there a way to support different coding styles in a development team
The Problem: Two developers => three opinions on indention, braces on new line or not etc.
We usually work with three or four people on our projects and each of those has it's own code style. I know, ...
2
votes
3answers
134 views
About the usage of assertions [duplicate]
I stumbled upon an article named Programming With Assertions.
And beside the mechanism of turning on and off assertions after compile time, I don't get it. Why were assertions introduced on language ...
8
votes
3answers
219 views
Is a single object to be preferred over multiple variables?
It was quite hard to put what I meant into a title, but it's easy to put into code.
C++
Is this
int offset_x = 10;
int offset_y = 40;
...
element.move(offset_x, offset_y);
To be preferred over ...
15
votes
12answers
2k views
int* i; or int *i; or int * i; [closed]
What is your favorite method to declare a pointer?
int* i;
or
int *i;
or
int * i;
or
int*i;
Please explain why.
see also: http://www.stroustrup.com/bs_faq2.html#whitespace
6
votes
3answers
238 views
Throwing an exception inside finally
Static code analyzers like Fortify "complain" when an exception might be thrown inside a finally block, saying that Using a throw statement inside a finally block breaks the logical progression ...
59
votes
6answers
3k views
Should the variable be named Id or ID?
This is a bit pedantic, but I've seen some people use Id as in:
private int userId;
public int getUserId();
and others use:
private int userID;
public int getUserID();
Is one of these a better ...
60
votes
39answers
21k views
Should curly braces appear on their own line? [closed]
Should curly braces be on their own line or not? What do you think about it?
if (you.hasAnswer()) {
you.postAnswer();
} else {
you.doSomething();
}
or should it be
if (you.hasAnswer())
{
...
