Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Recently I have been wondering what convention of holding helper functions is clearer, or what are the pros and cons of each of them.
So far I've been creating simple functions, which I exported individually from a ts file from my /src/core/utils folder. I'm getting more and more often on projects, where a whole class of support functions is created and it's the class that is imported.
My stack is next.js (with typescript) without any additional webpack configuration and none of the dependencies have any real impact on project building.
My question:
What are the advantages of a class over functions?
Doesn't importing the whole class increase the size of the bundle?
I'm starting to think about transferring to classes, because the design of Formatters.formatRating to me seems to be more readable than importing the formatRating function, which does not know where it comes from until I check imports at the top of the file.
Class
export class Formatters {
public static formatRating = (rating: number) = rating.toFixed(1)
}
Function
export formatRating = (rating: number) = rating.toFixed(1)
I know that there is no simple answer. Functions seem more natural when choosing React, but I keep wondering about these classes.
Don't forget your third choice: Objects with function members. That's basically what your class with public static methods is, but you don't need to use class syntax for them.
What are the advantages of a class over functions?
Specifically, you'e asking about classes with only static methods. Classes where you intend to create instances and hold state have advantages over standalone functions, but those advantages wouldn't be relevant to what you're describing.
Without saying whether it's an advantage or not (which is opinion based), the chief differences I can see between using a class with static methods (or an object with function properties) and using individually-exported functions are:
You only have to import the class/object, not each function you want to use.
The name of the class/object can be useful for categorizing he kinds of functions it has, perhaps making the names of the functions themselves shorter (but the class/object name + function name will probably be at least as long).
Doesn't importing the whole class increase the size of the bundle?
It depends on how good the tree-shaking in your bundler is; specifically, whether it's smart enough to omit static methods that are never used. If it's smart enough to prune static methods, then it shouldn't matter. If it's "only" smart enough to prune unused exports, then it would matter — if there are functions/methods that aren't used. If you're using all (or nearly all) of them, they have to be in the bundle anyway.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have say the following View Model in my WPF application.
public class User : ViewModelBase
{
public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } }
public int Age { get { return _age; } set { _age = value; OnPropertyChanged("Age"); } }
}
Now this information will be stored and pulled from a NoSQL database. My question is, should I keep separate Class in my project for the purpose of storage and retrieval to and from a NoSQL database and once the information is retrieved, build View Model or just using the View Model with NoSQL would be fine, surely that approach will save a lot of hassle.
What is the recommended way in NoSQL community in this regard ?
there are many different approaches to this, and it doesn’t really matter if you store the object in an RDBMS, NoSQL or some webservice which can again do what it wants.
it is true, that it’s easier if you don’t have to create a viewmodel - up to a certain point. Often you will find that extending the model by additonal properties which are only valid for your WPF implementation, and binding those properties, is easier than creating templates with different conditions. One example I have in mind would be for instance Color-Codes on your view, or if you have additional texts you want to display.
Those information can’t be in the original Model, because it isn’t relevant for anything else than your client. We’ve solved it in one project by annotating these properties with [XmlIgnore] - this way they weren’t serialized when talking to the server and we didn’t have to create an additional ViewModel. You might find that something like this is enough for you.
On the other hand for Web Applications, when serializing data with JSON it’s also important that you don’t want to send too much information to the client. Here it’s hard (sometimes not really possible) to adjust some autogenerated class definitions in order to annotate them appropriately. In thise case your only choice is to create special ViewModels and copy the data from one to the other.
In the end there comes one more factor - You might want to (or maybe rather, should) split your DataAccess logic from the rest. This way, if you decide to use a different storage mechanism you only need to reimplement that part, and make sure that your new Models can be transformed into the established ViewModels, so you don’t need to change everything.
You have to decide now for yourself - how fixed is your DataAccess infrastructure, will there ever change anything and are Annotations enough in order to hide additional information necessary for WPF bindings from your datastore? Or do you need a seperation between the Model and a ViewModel
I've read the entire Swift book, and watched all the WWDC videos (all of which I heartily recommend). One thing I'm worried about is data encapsulation.
Consider the following (entirely contrived) example:
class Stack<T>
{
var items : T[] = []
func push( newItem: T ) {
items.insert( newItem, atIndex: 0 )
}
func pop() -> T? {
if items.count == 0 {
return nil;
}
return items.removeAtIndex( 0 );
}
}
This class implements a stack, and implements it using an Array. Problem is, items (like all properties in Swift) is public, so nothing is preventing anyone from directly accessing (or even mutating) it separate from the public API. As a curmudgeonly old C++ guy, this makes me very grumpy.
I see people bemoaning the lack of access modifiers, and while I agree they would directly address the issue (and I hear rumors that they might be implemented Soon (TM) ), I wonder what some strategies for data hiding would be in their absence.
Have I missed something, or is this simply an omission in the language?
It's simply missing at the moment. Greg Parker has explicitly stated (in this dev forums thread) that visibility modifiers are coming.
Given that there aren't headers, the standard Objective-C tricks won't work, and I can't think of another trick to limit visibility that doesn't involve lots of bending over backwards. Since the language feature has been promised I'm not sure it's worth any big investment.
On the bright side since this feature is in flux, now is a great time to file a radar and influence how it turns out.
Updated answer for future reference.
From Apple's documentation:
Access Levels
Swift provides three different access levels for
entities within your code. These access levels are relative to the
source file in which an entity is defined, and also relative to the
module that source file belongs to.
Public access enables entities to
be used within any source file from their defining module, and also in
a source file from another module that imports the defining module.
You typically use public access when specifying the public interface
to a framework.
Internal access enables entities to be used within any
source file from their defining module, but not in any source file
outside of that module. You typically use internal access when
defining an app’s or a framework’s internal structure.
Private access
restricts the use of an entity to its own defining source file. Use
private access to hide the implementation details of a specific piece
of functionality. Public access is the highest (least restrictive)
access level and private access is the lowest (or most restrictive)
access level.
As a matter of fact I was delighted Swift finally adopted static typing so conforming to the theory for code with optimal OO properties, still the fall of the headers breaks the very meniang of Object Orienting programming, namely encapsulation. A way out would be like for Eiffel to automaticaly extract the headers but without specifying which are the public interfaces and which the private ones, it would be wortheless. I am really lambasted at this move of Apple's.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This might not be the ideal place to ask this question, but I'm really at a dead end and don't know where else to ask (suggestions would be appreciated).
I'm trying to come up with a name for 4 new API's for my company's C Library.
These API's are being added to an existing set, so they have to match an already made pattern, which limits my choices.
What all of the API's (existing and new) do is allow the user to get/set the value string or nonstring variables used in a program.
The way mine are different is that they allow you to get the value using the name of the variable, as apposed to already existing methods.
Here is what I currently have:
VariableGetValueString
VariableSetValueString
VariableGetValue
VariableSetValue
The only problem with this, is that it does not make it clear that it uses the NAME of the variable. I cannot think of a non-cluttered sounding name that makes this clear to the customer.
Preferably, there should be nothing removed from the names, as it matches the patterns of the other API's (which do not explicitly state their retrieval methods in their names, though this one should, for extraneous reasons).
Any help is appreciated, and though I know there is no definitive answer, I will obviously accept the one that fits the best.
Sorry again if this is a poor place to ask the question, I would love suggestions of a more appropriate place if there is one.
EDIT:
Some existing API names are:
VariableGetTaskString
VariableGetTask
VariableGetGlobalString
VariableGetGlobal
Along those lines. Task and Global refer to the scope of the variable. They weren't named very well in the first place, which makes my job more difficult, but they cannot be changed because customers have grown used to them and the changes would break old programs. I didn't include these initially because of how little help they offer (in my opinion).
The parameters of each API will make it obvious to the customer what each one does, but it would be preferable for the name to do that as well. Thanks for your feedback.
EDIT 2:
Here is an example of a call into the API:
if(!VariableGetValueString(Handle handle, LPCSTR variableName, TaskID taskID, LPSRT value, DWORD bufferSizeinBytes)
{
//retrieve failed.
}
if(!VariableGetValue(Handle handle, LPCSTR variableName, TaskID taskID, PDWORD value)
{
//retrieve failed.
}
Hope thats clear enough. Feel free to keep asking for more, I'll edit this all day. Thanks for the continued support.
Here are some possibilities:
suggestion 1
UseVariableNameToGetTaskString(...);
UseVariableNameToGetTask(...);
UseVariableNameGetGlobalString(...);
UseVariableNameToGetGlobal(...);
suggestion 2
VariableGetTaskStringByVarName(...);
VariableGetTaskByVarName(...);
VariableGetGlobalStringByVarName(...);
VariableGetGlobalByVarName(...);
suggestion 3
VariableGetTaskStringByName(...);
VariableGetTaskByName(...);
VariableGetGlobalStringByName(...);
VariableGetGlobalByName(...);
How about:
NamedVariableGetValueString
NamedVariableSetValueString
NamedVariableGetValue
NamedVariableSetValue
so that the distinction NamedVariable means a variable specified by name, whereas just Variable means a variable specified by ID or whatever the old functions use.
How about:
getVariableName()
and
setVariableName(char* value)
I used to have one class for one file. For example car.cs has the class car. But as I program more classes, I would like to add them to the same file. For example car.cs has the class car and the door class, etc.
My question is good for Java, C#, PHP or any other programming language. Should I try not having multiple classes in the same file or is it ok?
I think you should try to keep your code to 1 class per file.
I suggest this because it will be easier to find your class later. Also, it will work better with your source control system (if a file changes, then you know that a particular class has changed).
The only time I think it's correct to use more than one class per file is when you are using internal classes... but internal classes are inside another class, and thus can be left inside the same file. The inner classes roles are strongly related to the outer classes, so placing them in the same file is fine.
In Java, one public class per file is the way the language works. A group of Java files can be collected into a package.
In Python, however, files are "modules", and typically have a number of closely related classes. A Python package is a directory, just like a Java package.
This gives Python an extra level of grouping between class and package.
There is no one right answer that is language-agnostic. It varies with the language.
One class per file is a good rule, but it's appropriate to make some exceptions. For instance, if I'm working in a project where most classes have associated collection types, often I'll keep the class and its collection in the same file, e.g.:
public class Customer { /* whatever */ }
public class CustomerCollection : List<Customer> { /* whatever */ }
The best rule of thumb is to keep one class per file except when that starts to make things harder rather than easier. Since Visual Studio's Find in Files is so effective, you probably won't have to spend much time looking through the file structure anyway.
No I don't think it's an entirely bad practice. What I mean by that is in general it's best to have a separate file per class, but there are definitely good exception cases where it's better to have a bunch of classes in one file. A good example of this is a group of Exception classes, if you have a few dozen of these for a given group does it really make sense to have separate a separate file for each two liner class? I would argue not. In this case having a group of exceptions in one class is much less cumbersome and simple IMHO.
I've found that whenever I try to combine multiple types into a single file, I always end going back and separating them simply because it makes them easier to find. Whenever I combine, there is always ultimately a moment where I'm trying to figure out wtf I defined type x.
So now, my personal rule is that each individual type (except maybe for child classes, by which a mean a class inside a class, not an inherited class) gets its own file.
Since your IDE Provides you with a "Navigate to" functionality and you have some control over namespacing within your classes then the below benefits of having multiple classes within the same file are quite worth it for me.
Parent - Child Classes
In many cases i find it quite helpful to have Inherited classes within their Base Class file.
It's quite easy then to see which properties and methods your child class inherits and the file provides a faster overview of the overall functionality.
Public: Small - Helper - DTO Classes
When you need several plain and small classes for a specific functionality i find it quite redundant to have a file with all the references and includes for just a 4-8 Liner class.....
Code navigation is also easier just scrolling over one file instead of switching between 10 files...Its also easier to refactor when you have to edit just one reference instead of 10.....
Overall breaking the Iron rule of 1 class per file provides some extra freedom to organize your code.
What happens then, really depends on your IDE, Language,Team Communication and Organizing Skills.
But if you want that freedom why sacrifice it for an iron rule?
The rule I always go by is to have one main class in a file with the same name. I may or may not include helper classes in that file depending on how tightly they're coupled with the file's main class. Are the support classes standalone, or are they useful on their own? For example, if a method in a class needs a special comparison for sorting some objects, it doesn't bother me a bit to bundle the comparison functor class into the same file as the method that uses it. I wouldn't expect to use it elsewhere and it doesn't make sense for it to be on its own.
If you are working on a team, keeping classes in separate files make it easier to control the source and reduces chances of conflicts (multiple developers changing the same file at the same time). I think it makes it easier to find the code you are looking for as well.
It can be bad from the perspective of future development and maintainability. It is much easier to remember where the Car class is if you have a Car.cs class. Where would you look for the Widget class if Widget.cs does not exist? Is it a car widget? Is it an engine widget? Oh maybe it's a bagel widget.
The only time I consider file locations is when I have to create new classes. Otherwise I never navigate by file structure. I Use "go to class" or "go to definition".
I know this is somewhat of a training issue; freeing yourself from the physical file structure of projects requires practice. It's very rewarding though ;)
If it feels good to put them in the same file, be my guest. Cant do that with public classes in java though ;)
You should refrain from doing so, unless you have a good reason.
One file with several small related classes can be more readable than several files.
For example, when using 'case classes', to simulate union types, there is a strong relationship between each class.
Using the same file for multiple classes has the advantage of grouping them together visually for the reader.
In your case, a car and a door do not seem related at all, and finding the door class in the car.cs file would be unexpected, so don't.
As a rule of thumb, one class/one file is the way to go. I often keep several interface definitions in one file, though. Several classes in one file? Only if they are very closely related somehow, and very small (< 5 methods and members)
As is true so much of the time in programming, it depends greatly on the situation.
For instance, what is the cohesiveness of the classes in question? Are they tightly coupled? Are they completely orthogonal? Are they related in functionality?
It would not be out of line for a web framework to supply a general purpose widgets.whatever file containing BaseWidget, TextWidget, CharWidget, etc.
A user of the framework would not be out of line in defining a more_widgets file to contain the additional widgets they derive from the framework widgets for their specific domain space.
When the classes are orthogonal, and have nothing to do with each other, the grouping into a single file would indeed be artificial. Assume an application to manage a robotic factory that builds cars. A file called parts containing CarParts and RobotParts would be senseless... there is not likely to be much of a relation between the ordering of spare parts for maintenance and the parts that the factory manufactures. Such a joining would add no information or knowledge about the system you are designing.
Perhaps the best rule of thumb is don't constrain your choices by a rule of thumb. Rules of thumb are created for a first cut analysis, or to constrain the choices of those who are not capable of making good choices. I think most programmers would like to believe they are capable of making good decisions.
The Smalltalk answer is: you should not have files (for programming). They make versioning and navigation painful.
One class per file is simpler to maintain and much more clear for anyone else looking at your code. It is also mandatory, or very restricted in some languages.
In Java for instance, you cannot create multiple top level classes per file, they have to be in separate files where the classname and filename are the same.
(C#) Another exception (to one file per class) I'm thinking of is having List in the same file as MyClass. Where I envisage using this is in reporting. Having an extra file just for the List seems a bit excessive.