111
votes
Why do so many standards for JSON API response formats contain a "success" property in the response body instead of just using HTTP status codes?
Many people take HTTP status code as “successful communication with the server”.
Now if a customer wants to buy a US$200 item and has only US$100 in their account, the JSON response will be “failure, ...
100
votes
Accepted
Is using parameter names that differ from type names only by casing considered a bad practice in C#?
Don't overthink this, Range range is fine. I use such kind of naming for more than 15 years in C#, and probably much longer in C++, and have never experienced any real drawbacks from it, quite the ...
100
votes
Accepted
How can I avoid always feeling like if I completely rebuilt my program from scratch I'd do it much better?
This is a very common experience
Most people I interact with, and I myself as well, feel like this. From what I can tell one reason for this is that you learn more about the domain and the tools you ...
80
votes
Accepted
Adding Units To Magic Numbers
The issue is not only with the lack of units, but the fact that it is not clear what three of those units represent. Do you only have three minutes to complete a task? Then the constant might be ...
68
votes
Accepted
Is it enough for methods to be distinguished just by argument name (not type)?
Sure there is a good reason to name it more explicitly.
It's not primarily be the method definition that should be self-explanatory, but the method use. And while findById(string id) and find(string ...
62
votes
Accepted
Why does software use the Win32 name?
Win32 is the customary name for the Windows API. This API specifies how applications can interface with the operating system. It is roughly comparable with the POSIX standard on Unix, but Win32 also ...
58
votes
Accepted
Why do so many standards for JSON API response formats contain a "success" property in the response body instead of just using HTTP status codes?
A few potential reasons why you may wish to do this are:
the fact that some HTTP clients treat anything other than 2xx as an "exception" to be thrown, which can hide differences between ...
45
votes
How can I avoid always feeling like if I completely rebuilt my program from scratch I'd do it much better?
Learn refactoring - the art of gradually improving code. We all learn all the time, so it is very common to realize that the code you have written yourself could be written in a better way.
But you ...
35
votes
Is it enough for methods to be distinguished just by argument name (not type)?
Advantages of FindById().
Future-proofing: If you start with Find(int), and later have to add other methods (FindByName(string), FindByLegacyId(int), FindByCustomerId(int), FindByOrderId(int), etc), ...
30
votes
Why does software use the Win32 name?
Because the Windows API is 30+ years old and has been around when PC's were 16-bit, then 32-bit came along, then Win32s, then win64. There is platform dependence in windows development, and you need ...
30
votes
Is it bad practice create "alias" variables to not use globals or arguments with long names in a method?
In general, creating local variables for readability is a good thing. A local variable gives a locally relevant name to something; that same thing might have a different name in a different context.
...
28
votes
Accepted
Why did JUnit declare setUp and tearDown in camelcase, even though each of them is a single word?
No, it shouldn't.
For the moment, there is still a difference between the noun "setup" and the verb "set up":
Hey Joe, can you set up the amp for me?
Dude, that's a sweet stereo ...
25
votes
Why would you ever want WET code?
No one is really promoting unnecessary code duplication. You don't usually have a choice between orthogonal (DRY) and duplicate (WET) code. “WET” is a bit of a strawman for DRY-proponents to argue ...
24
votes
Accepted
How to name a Boolean variable that represents either of two options?
Don't use a Boolean. Use an enum. E.g TransactionType with instances Buy and Sell.
That is unambiguous and far easier to understand.
If you want to persist the data efficiently, the boolean can be a ...
23
votes
Accepted
Is it bad practice to create blocks of code?
Blocks are perfectly reasonable if you're using them to scope some resource. Files, network connections, memory allocations, database transactions, whatever. In those cases, the block is actually part ...
23
votes
Accepted
How to introduce new language features in a legacy source code?
When in Rome do as the Romans do.
It's nice if an entire code base follows one consistent style. However that will trap it's style in the past.
If you're enchanted by some new fangled style the ...
21
votes
Positional arguments vs options in a command-line interface
Your question is a false dichotomy.
Most places I’ve seen use both. Position based parameters for required parameters, and options based parameters for optional parameters. Kinda makes sense, yeah?
19
votes
Does it make sense to use meaningless named constants?
No this is utterly pointless. Don't just extract literals to named constants without good reasons.
But do consider ways to explain why the a value has to be doubled in that context. That could ...
18
votes
Accepted
How to quantify Code Quality
At this point I want to want to device strategies to reward good code.
You cannot. Goodhart's Law will quickly come into play, and your objective metrics will become the things that your developers ...
18
votes
How to introduce new language features in a legacy source code?
I am working with C# since version 1.1 (around 2003), and I have first-hand experience how we introduced new language features into our grewing code base over the years. We simply introduced them ...
18
votes
Adding Units To Magic Numbers
Please always use all relevant information:
int MAXIMUM_TASK_DURATION_MINUTES = 3;
Can't remember how often I had some strange undocumented API and was wondering if it is minutes, seconds or millis.
18
votes
Why do so many standards for JSON API response formats contain a "success" property in the response body instead of just using HTTP status codes?
There are inherent shortcomings in trying to fit a nuanced, complete API into the limitations of HTTP. The above examples provide some good points to why that's the case.
Here's another scenario we ...
17
votes
Accepted
Not prohibited, but disallowed -- which http error code should I return?
That sounds like a straightforward 403
You are implementing a rule to ban access to a resource for an unauthorised user - that's a 403
17
votes
How to name a method which may or may not perform an action depending on a condition?
You're trapped in a structural way of thinking. A name should abstract away implementation details. It shouldn't become a short hand for them.
IfBirthdayBuyCake();
is a terrible name. It exposes ...
16
votes
Is using parameter names that differ from type names only by casing considered a bad practice in C#?
I do this all the time, it is giving me great piece of mind. If it is an argument passed to a constructor that needs to be assigned to a member, my member would be named range as well and the ...
16
votes
Accepted
Is it a good practice to create new variable to see the output of a function?
If you compare the assembly/bytecode/IL/etc output of two functions like that, you should find them both to be the same. All but the most bush-league of compilers will optimize out the extra value.
...
16
votes
Is OOP really beneficial for enterprise-scale business software compared to procedural languages like ABAP or COBOL?
I strongly suggest you critically review your pain points and reflect how much of this is attributable to OOP inherently, versus inexperience stemming from what is clearly a workplace that is not ...
15
votes
Method naming: to vs as vs get
The term as is often used to denote that the method casts the item from one type to another, without creating a new object.
The term to often indicates that the method creates a new representation of ...
14
votes
Where did the notion of "one return only" come from?
One return makes refactoring easier. Try to perform "extract method" to the inner body of a for loop that contains a return, break or continue. This will fail as you have broken your control flow.
...
Community wiki
14
votes
How to eliminate a if-else block which is used to determine behaviour according to a boolean input?
You say, "... this implementation only moves the if-else from inside to outside" as if that's no big deal but in fact the second example moves the if check closer to the code that knows what the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
coding-standards × 557coding-style × 150
naming × 58
java × 53
c# × 46
programming-practices × 42
conventions × 39
c++ × 32
code-quality × 32
php × 29
clean-code × 24
naming-standards × 23
python × 20
.net × 20
c × 18
architecture × 17
object-oriented × 16
comments × 15
variables × 14
design × 13
javascript × 13
language-agnostic × 12
code-reviews × 11
readability × 11
standards × 11