New answers tagged

0

My answer to the question “How do you approach a JavaScript (or any other dynamic language for that matter) project with ~2000 LOC?” I develop PDF form applications. I approach my JavaScript software development project (regardless of source code size) using Petri’s net elements and annotations. The method is not tied to any particular programming language ...


1

You use common sense. I might care a lot about whether the user is an administrator and add a method for that. I might care a lot about the user service object of the controller and use it a lot, for all the purposes that a user service object is designed to be used for. In that case I'd add a method for the user service object. I suspect that I don't ...


0

It looks a bit like global variables (bad, bad). Or a bunch of singletons, utility objects (bad, bad). But then modules are again considered fine. Normally one would have a hierarchical structure: application - dialogs x documents. "As you need them" is the most efficient way in general, so I would keep with that. However there might be modular ...


1

It's fine if it doesn't hog to many resources. Best way to know if it does is to do it and see what happens. This is a performance issue. Don't fix it until you see it. The number one reason not to allocate space upfront, objects in this case, is when you don't already know how much to allocate. This is why heap collections are prefered over arrays. ...


0

The road to Damascus was built in sections, and so shall yours. No one builds a 500 mile road all at once, all or nothing. Beware the Dunning-Kruger Effect Define data formats. Sometimes they're not obvious. These will always simplify the using code. Even small and simple structures. Data will be a primary way the software parts will communicate. And I ...


2

I would go for the clone method if the class involved was polymorphic (so that an instance of the right runtime type was created). Otherwise it's a matter of taste in my opinion.


3

Use copy constructors. Here's why: IClonable semantics are ambiguous. Microsoft never specified whether a clone should be a shallow or deep copy. You can specify custom behavior in your copy constructor, such as giving each copy its own unique ID or only copying some fields and not others. Further Reading Copy constructor vs Clone in C#


0

With respect to modifying records when the user presses "ok" in the dialog, you must compare it with the database record instance. You cannot rely on cloning the original HTTP get because someone may have modified the record before you and you get race conditions. If this is not the case, this has been covered by another thread and I am hoping it helps you ...


0

As a philosophical contrapunctus to the majority opinion, I must state that there are some of us, who appreciate the unsophisticated 19th century French peasant programmer and remembered his monotonous, everlasting lucidity, his stupefyingly sensible views of everything, his colossal contentment with truisms merely because they were true. "Confound it ...


0

I may want to google for the term "ABAC" (Access based access control) Definition (wikipedia): Attribute-based access control (ABAC) defines an access control paradigm whereby access rights are granted to users through the use of policies which combine attributes together. The policies can use any type of attributes (user attributes, resource attributes, ...


0

Are single-character constants better than literals? There are a lot of conflations floating around here. Let me see if I can tease them apart. Constants provide: semantics change, during development indirection Going down to a single character name only impacts the semantics. A name should be useful as a comment and clear in context. It should ...


1

See Luc's valid comment on your question re. URL templates. I would add... anytime you want a magic number that is neither 0 or 1 and is also not referring to a specific independent feature of the business domain, that is a code smell. Your issue here isn't that you're hardcoding a magic number so much as you're hardcoding something which is replicating ...


-1

OK there are two competing questions here. One is what is the 'official' 'best' way to program this and Two is that way actually more readable so here is my 'official' 'best' way public class ChannelInfo { public String ChannelId { get; set; } } public class PathExtractor : IPathExtractor { private const int ...


11

In layman's terms: If you will be extracting the channelID in several places, then you should create a function. Such a function, being cohesive, should read from the state. Part of that state would be the constant with a name like DELIMITERS_BEFORE_ID. I find that this: String channelId = channelIDdWithPath.split("/")[DELIMS_BEFORE_ID]; ...is more ...


-3

But when are we going to start using a different symbol than ',' to represent a comma? For localisation. In English-speaking countries, the symbol separating the whole and fractional parts of a decimal is ".", which we call "decimal point". In many other countries, the symbol is "," and is typically called the equivalent of "comma" in the local language. ...


1

If this was a class written as a part of an application by your fellow developer, this is almost certainly a bad idea. As others already pointed out, it makes sense to define constants such as SEPARATOR = ',' where you can change the value and the constant still makes sense but much less so constants whose name describes just their value. However, there are ...


9

Your should focus on what makes your code readable and the spirit of the guidelines rather than try to prove you can write bad code even when following the guidelines. A constant named NUMBER_OF_DELIMITERS_BEFORE_ID_IN_CHAT_CHANNEL_PATH is not very clear or understandable. So yes a magic number is probably not any worse than using such a badly named ...


1

The data belongs to the organization, not the application. While right now you're using Laravel exclusively, in the future there might be several applications interacting with the database. I'm currently working on an app that started in FoxPro, migrated to ASP classic, and was partially upgraded to ASP.NET. There are at least three applications interacting ...


3

Foreign keys in your database enables data integrity, as you can't delete a parent row if there is a child row in another table. While you can rely on the framework to handle data for you, the framework will not enable data integrity and you will eventually end up with orphan rows in your database. So, my advice is: design the database properly in order ...


1

Two points: I prefer methods like these because they help break up method trains like: myObject.getFoo().doBar().tooManyCalls() (i.e. Train Wrecks) However, If you find yourself writing several of these "shortcut" methods you may very well have a bigger/better refactoring that you can take advantage of. See Bob Martin's book "Clean Code" and code-smell ...


1

Observe that you are trying to make a list. So, refactor it as: String makeList(String[] items) In other words, factor out the logic instead of the data. Languages might be different in how they represent lists, but commas are always commas (that's a tautology). So if the language changes, changing the comma character won't help you -- but this will.


5

Is creating methods such as isUserAdmin() bad practice? It is, though it isn't so much that this method is bad practice, but rather using a parent class as a dumping ground for methods you want to share with the child objects but that don't have any particular relationship to each other in terms of behaviour is a bad idea. If the method was a core ...


20

Some answers seem like they want to contradict each other, like the one by Eric, and the one by Jarrod. But they are in perfect agreement. A constant must add meaning. Defining COMMA to be a comma doesn't add meaning, because we know that a comma is a comma. Instead we destroy meaning, because now COMMA might actually not be a comma anymore. If you use a ...


3

The one time I have seen such constants used effectively is to match an existing API or document. I've seen symbols such as COMMA used because a particular piece of software was directly connected to a parser which used COMMA as a tag in an abstract syntax tree. I've also seen it used to match a formal specification. in formal specifications, you'll ...


2

Tracing (software). FYI, we typically use the terms statements and expressions for programming language constructs and to describe lines of code in high-level language programs; we typically use the term instructions to describe what the hardware machine operations are. Another term is an execution trace, which would capture a flow of execution. An ...


3

In addition to all the fine answers here, I'd like to add as food for thought, that good programming is about providing appropriate abstractions that can be built upon by yourself and maybe others, without having to repeat the same code over and over. Good abstractions make the code easy to use on the one hand, and easy to maintain on the other hand. I ...


28

There are a few characters that are can be ambiguous or are used for several different purposes. For example, we use '-' as a hyphen, a minus sign, or even a dash. You could make separate names as: static const wchar_t HYPHEN = '-'; static const wchar_t MINUS = '-'; static const wchar_t EM_DASH = '-'; Later, you could choose to modify your code to ...


17

The idea that a constant COMMA is better than ',' or "," is rather easy to debunk. Sure there are cases where it makes sense, for example making final String QUOTE = "\""; saves heavily on the readibility without all the slashes, but barring language control characters like \ ' and " I haven't found them to be very useful. Using final String COMMA = "," is ...


131

The main appeal of using constants is they minimize maintenance when a change is needed. ABSOLUTELY NOT. This is not at all the reason to use constants because constants do not change by definition. If a constant ever changes then it was not a constant, was it? The appeal of using constants has nothing whatsoever to do with change management and ...


3

I've done some work writing lexers and parsers and used integer constants to represent terminals. Single-character terminals happened to have the ASCII code as their numeric value for simplicity's sake, but the code could have been something else entirely. So, I'd have a T_COMMA that was assigned the ASCII-code for ',' as its constant value. However, there ...


-1

Maybe. Single character constants are relatively hard to distinguish. So it can be rather easy to miss the fact that you're adding a period rather than a comma city + '.' + state whereas that's a relatively hard mistake to make with city + Const.PERIOD + state Depending on your internationalization and globalization environment, the difference ...


172

Tautology: It is very clear if you read the very first sentence of the question that this question is not about appropriate uses like eliminating magic numbers, it is about terrible mindless foolish consistency at best. Which is what this answer addresses Common sense tells you that const char UPPER_CASE_A = 'A'; or const char A = 'A' does not ...


61

No, that is dumb. What is not necessarily dumb is pulling things like that into named labels for localization reasons. For example, the thousands delimiter is a comma in America (1,000,000), but not a comma in other locales. Pulling that into a named label (with an appropriate, non-comma name) allows the programmer to ignore/abstract those details. But ...


5

Is such practice common, or it implies I am not competent? Those are not mutually exclusive. The pattern could be common, and you still might be incompetent. Your competence can be determined by measuring your performance against your goals. Are you meeting your goals? Is this pattern common? Unfortunately, yes. Many people dive into projects with no ...


-2

Start with a few use-cases Start coding Wait, what?? You are neglecting analysis and design part of the software development life-cycle. Analysis and design is not just about solving the immediate problem in hand, but also about asking (and answering) the right questions to predict what other needs will follow on later.


1

You already have some great answers but your question brings a few things to mind that I thought I would try to touch on. As you noted running into changes down the road I'd suggest thinking about how things impacted your project and how you could have minimized the impact with design/coding choices while at the same time generating a mental map of where ...


1

I'd like to add a few pointers 1) I personally found it incredible useful to start with visualizing stuff. Draw boxes, arrows, lines ... Never mind whatever modelling language you use. In the first place you are doing it FOR YOURSELF. It should help your flow of thoughts. 2) Find a sparring partner - grab a coffee and the flipchart/diagram etc from above ...


1

I think its safe to say you're not so far off from a better way of working, and you're not the only one in this boat. What I think you're missing is that, although you determine what you want, you don't stop to do the same process for how you'll do it. This step of stopping and thinking about how to tackle the problem overall is called design, its the step ...


68

The cycle you describe is normal. The way to improve things is not to avoid this cycle, but to streamline it. The first step is to accept that: It's near impossible to know everything on day one of a project. Even if you do somehow know everything, by the time you've finished the project then something (the client's requirements, the market they're in, the ...


14

This is normal. You can take one of two approaches: Welcome Change If you assume that you will get it wrong, you must build a code base that is open to change. Mostly this involves taking the code at the end of a book on refactoring, and building your code that way from the start (decomposability, good test coverage, ...). Avoid Change In this case ...


11

Software development has been described as a series of inherently "wicked" problems. Horst Rittel and Melvin Webber defined a "wicked" problem as one that could be clearly defined only by solving it, or by solving part of it*. This paradox implies, essentially, that you have to "solve" the problem once in order to clearly define it and then solve it ...


8

Yes, this is common, except maybe for the "rewrite most of the code" part. You'll never get all requirements right from the beginning, so it's important to deal with change well. That's what the concept of "code maintainability" is all about. Of course it also helps to spend some more time on getting the requirements and design right. First, think of what ...


0

You have to ask what you hope to achieve by this - if its only to get a list of string resources for your own benefit, you might as well just try to learn the system. You say that a lot of it is in the form of constants and variables so all you would gain from putting the data into an external file is to replace the original with more constants or variables (...


1

Interspersing large amounts of data with program logic is so bad that added complexity in your code base is a cheap price to pay for it. Make the transformation, and you'll probably be rewarded with additional of opportunities for refactoring to simplicity.


2

Ask your manager if the reports to each customer need to be different, and why? If your manager doesn't know, (or gives you some flim-flam about it's always been done this way) then ask if there are any opportunities to streamline/rationalize the business process, and thus make savings. You might need to ask the customers if they would like a standard format ...


4

Gain control of how report data is formatted. Excel simply isn't a data format. It has it's own presentation ideas. You can make this work and it sounds like a lot has already been built help you do this. So maybe you don't want to swim against the tide at this point but you're tied to a lot of things you don't want to be tied to. Different versions of ...



Top 50 recent answers are included